I’m using Ant Design in Vue 3 and I have a table that I’m able to edit all the cells. The problem is: I want to automatically close the editable cell if the user opens a new one to edit. While I was researching, I noticed that this behaviour happens on Ant Design for React accordingly to the documentation.
My question is, how to do this for Vue 3? On their documentation for Vue, the Ant Design table doesn’t show this behaviour that I wanted and I have no idea how to do that. Thanks in advance. 🙂
This is how my code looks now:
JavaScript
x
118
118
1
<template>
2
<a-table bordered :data-source="tableData.data" :columns="tableData.columns" :pagination="false" class="tableEditable">
3
<template v-for="col in this.editableCells" #[col]="{ column, text, record }" :key="col">
4
<div class="editable-cell" :ref="(record.key, column.key)">
5
<div v-if="editableData[record.key + '|' + column.key] !== undefined" class="editable-cell-input-wrapper">
6
<a-input v-model:value="editableData[record.key + '|' + column.key]" @pressEnter="save(record.key, column.key)" type="number" />
7
<check-outlined class="editable-cell-icon-check" @click="this.save(record.key, column.key)" />
8
</div>
9
<div v-else class="editable-cell-text-wrapper">
10
{{ text || ' ' }}
11
<edit-outlined class="editable-cell-icon" @click="this.edit(record.key, column.key)" />
12
</div>
13
</div>
14
</template>
15
<template #buttonTable="{text, record}">
16
<div class="editableTableButtonOption">
17
{{text}}
18
<Popper arrow :locked="true">
19
<button class="statusButtonCrud synch" v-if="showEditableButton(record.key)"> {{this.buttonText}} </button>
20
<template #content="{close}">
21
<div class="popperOptions">
22
<li v-for="options in this.optionsToEdit" :key="options" class="popperOptionsList" v-on:click="this.emitOption(options.method), close();">
23
{{$filters.translate(options.title)}}
24
</li>
25
</div>
26
</template>
27
</Popper>
28
</div>
29
</template>
30
</a-table>
31
</template>
32
<script>
33
34
import { CheckOutlined, EditOutlined } from '@ant-design/icons-vue';
35
36
export default {
37
name: 'TableEditable',
38
props: {
39
editableCells: Array,
40
tableData: Object,
41
buttonText: String,
42
optionsToEdit: Array,
43
copyOptionsTable: Boolean
44
},
45
emits: ['change', 'editRow', 'editColumn', 'editAllCells'],
46
components: {
47
CheckOutlined,
48
EditOutlined
49
},
50
data(){
51
return {
52
editableData: {},
53
selectedRow: '',
54
selectedColumn: '',
55
valueSaved: ''
56
}
57
},
58
methods: {
59
edit(row, column) {
60
this.editableData[row + '|' + column] = this.tableData.data.filter((item) => row === item.key)[0][column];
61
},
62
save(row, column) {
63
let data = {this.tableData.data};
64
if (this.editableData[row + '|' + column] == '') {
65
data[row][column] = '0'
66
} else {
67
data[row][column] = this.editableData[row + '|' + column];
68
this.valueSaved = data[row][column]
69
}
70
delete this.editableData[row + '|' + column];
71
this.selectedRow = row;
72
this.selectedColumn = column;
73
this.$emit('change', data);
74
if (this.copyOptionsTable) {
75
this.addClass();
76
this.editedCell();
77
}
78
},
79
showEditableButton(row) {
80
if (this.copyOptionsTable && this.selectedRow == row) {
81
return true
82
}
83
},
84
emitOption(method) {
85
this.$emit(method, this.selectedRow, this.selectedColumn, this.valueSaved);
86
},
87
addClass() {
88
let columnsHTML = [document.querySelectorAll('.ant-table-thead > tr > th')];
89
let columnsData = this.tableData.columns;
90
for (let idx in columnsHTML) {
91
columnsHTML[idx].classList.add(columnsData[idx].dataIndex);
92
}
93
},
94
editedCell() {
95
this.removeCell();
96
let tableRow = [document.querySelectorAll('.ant-table-tbody > tr > td')];
97
let cellRef = this.$refs[this.selectedColumn];
98
cellRef.classList.add('editedCell');
99
for (let cell in tableRow) {
100
let div = tableRow[cell].querySelector('div')
101
if (div.classList.contains('editedCell')) {
102
tableRow[cell].classList.add('selectedCell')
103
}
104
}
105
},
106
removeCell(){
107
let tableRow = [document.querySelectorAll('.ant-table-tbody > tr > td')];
108
for (let cell in tableRow) {
109
tableRow[cell].classList.remove('selectedCell')
110
let cellDiv = tableRow[cell].querySelector('div');
111
cellDiv.classList.remove('editedCell');
112
113
}
114
}
115
},
116
}
117
</script>
118
Advertisement
Answer
It was simpler than I thought it would be. Just changed the edit method to this and now it’s working 🙂
JavaScript
1
7
1
edit(row, column) {
2
for (let idx in this.editableData) {
3
delete this.editableData[idx];
4
}
5
this.editableData[row + '|' + column] = this.tableData.data.filter((item) => row === item.key)[0][column];
6
},
7