现在我需要一些方法来让我的表格上的所有单元格都可以编辑。我知道我可以像这个例子中那样做。但这只是每行一个单元格,我想知道如何使用自己的 v-model 为单行中的所有单元格实现这一点。我一直在寻找示例,但找不到任何示例。有谁知道我该怎么做?
我可以编辑所有这些,但是当我单击它时,它会打开所有单元格的编辑模式,而实际上我只想打开我单击的那个。
这就是我的代码现在的样子:
<template>
<a-table bordered :data-source="dataSource" :columns="columns" :pagination="false">
<template #title>
<div class="formLayoutCrud">
<p>{{this.title}}</p>
<input-multiple-button :name="'buttonOptions'" :value="'percentage'" :options="this.buttonOptions"> </input-multiple-button>
</div>
</template>
<template v-for="col in this.editableCells" #[col]="{ text, record }" :key="col">
<div class="editable-cell">
<div v-if="editableData[record.key]" class="editable-cell-input-wrapper">
<a-input v-model:value="editableData[record.key][col]" @pressEnter="save(record.key)" />
<check-outlined class="editable-cell-icon-check" @click="save(record.key)" />
</div>
<div v-else class="editable-cell-text-wrapper">
{{ text || ' ' }}
<edit-outlined class="editable-cell-icon" @click="edit(record.key, col)" />
</div>
</div>
</template>
</a-table>
</template>
<script>
import { reactive, ref } from 'vue';
import { CheckOutlined, EditOutlined } from '@ant-design/icons-vue';
import InputMultipleButton from '@/components/crudForm/InputMultipleButton.vue';
export default {
name: 'TableEditable',
props: {
title: String,
buttonOptions: Array,
editableCells: Array,
dataSrc: Array
},
components: {
CheckOutlined,
EditOutlined,
InputMultipleButton
},
setup() {
const columns = [
{
title: 'Mon',
dataIndex: 'monday',
slots: {
customRender: 'monday',
},
},
{
title: 'Tue',
dataIndex: 'tuesday',
slots: {
customRender: 'tuesday',
},
},
{
title: 'Wed',
dataIndex: 'wednesday',
slots: {
customRender: 'wednesday',
},
},
{
title: 'Thr',
dataIndex: 'thursday',
slots: {
customRender: 'thursday',
},
},
{
title: 'Fri',
dataIndex: 'friday',
slots: {
customRender: 'friday',
},
},
{
title: 'Sat',
dataIndex: 'saturday',
slots: {
customRender: 'saturday',
},
},
{
title: 'Sun',
dataIndex: 'sunday',
slots: {
customRender: 'sunday',
},
},
];
const dataSource = ref([
{
key: '0',
monday: '0',
tuesday: '0',
wednesday: '0',
thursday: '0',
friday: '0',
saturday: '0',
sunday: '0'
}
]);
const editableData = reactive({});
const edit = (key, teste) => {
console.log(teste)
editableData[key] = JSON.parse(JSON.stringify(dataSource.value.filter(item => key === item.key)[0]));
};
const save = key => {
console.log(key)
Object.assign(dataSource.value.filter(item => key === item.key)[0], editableData[key]);
delete editableData[key];
};
return {
columns,
dataSource,
editableData,
edit,
save
};
},
}
</script>
提前致谢!