Skip to content

Commit ece3bcd

Browse files
committed
fix: track state of visible components
Some components would lose their state after applying search filter. This bug is partially solved.
1 parent 4828bd0 commit ece3bcd

File tree

2 files changed

+21
-6
lines changed

2 files changed

+21
-6
lines changed

src/components/DataTable.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,10 +193,20 @@ export default defineComponent({
193193
* The data filtered by search text
194194
*/
195195
dataFiltered() {
196-
const { data, searchableColumns, search } = this
196+
const { searchableColumns, search } = this
197+
198+
// assign key to track row
199+
const data = this.data.map((value, index) => {
200+
return {
201+
...(value as Object),
202+
_key: index,
203+
};
204+
})
205+
197206
if (isNullable(search)) {
198207
return data
199208
}
209+
200210
return data.filter(function(row: any) {
201211
return searchableColumns.some(function(column: Column) {
202212
return column.searchFunction(row, search, column.key)

src/components/Table/Table.vue

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,17 @@
4040
</tr>
4141

4242
<!-- NON-EMPTY BODY -->
43-
<tr v-for="(data, i) in dataDisplayed" :key="i">
44-
<td v-for="(column, j) in columns" :key="j">
45-
<component :is="column.component"
46-
v-bind="{ data, ...column.componentProps }"
47-
@userEvent="emitUserEvent" />
43+
<tr v-for="data in dataDisplayed" :key="data._key">
44+
<keep-alive>
45+
<td v-for="(column, j) in columns"
46+
:key="'c' + data._key + 'c' + j"
47+
>
48+
<component
49+
:is="column.component"
50+
v-bind="{ data, ...column.componentProps }"
51+
@userEvent="emitUserEvent" />
4852
</td>
53+
</keep-alive>
4954
</tr>
5055
</tbody>
5156

0 commit comments

Comments
 (0)