在Vue.js中实现表格(table)的编辑功能,通常涉及到以下几个基础概念:
以下是一个简单的Vue 3示例,展示如何实现单元格编辑功能:
<template>
<table>
<tr v-for="(row, rowIndex) in tableData" :key="rowIndex">
<td v-for="(cell, cellIndex) in row" :key="cellIndex">
<span v-if="!editingCell" @click="editCell(rowIndex, cellIndex)">{{ cell }}</span>
<input v-else v-model="tableData[rowIndex][cellIndex]" @blur="finishEdit" />
</td>
</tr>
</table>
</template>
<script>
import { ref } from 'vue';
export default {
setup() {
const tableData = ref([
['John', 'Doe', 'john@example.com'],
['Jane', 'Doe', 'jane@example.com']
]);
const editingCell = ref(null);
function editCell(rowIndex, cellIndex) {
editingCell.value = { rowIndex, cellIndex };
}
function finishEdit() {
editingCell.value = null;
}
return {
tableData,
editingCell,
editCell,
finishEdit
};
}
};
</script>
v-model
正确绑定数据,以便实时更新。以上就是关于Vue.js表格编辑的基础概念、优势、类型、应用场景以及一个简单的实现示例。如果遇到具体问题,可以根据上述建议进行排查和解决。
领取专属 10元无门槛券
手把手带您无忧上云