Skip to content

Commit 9ae19ef

Browse files
committed
fix: component state messed up due to non-unique v-key
1 parent 50a9dd4 commit 9ae19ef

File tree

3 files changed

+31
-19
lines changed

3 files changed

+31
-19
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,12 @@ Nuxt automatically loads the files in the `plugins/` directory by default.
131131

132132
Only `data` e `columns` are required. Other props are optional.
133133

134+
`vKey` is not required but is **highly** recommend to set it if you plan to
135+
add or delete rows in the table!
136+
134137
| prop | type | default | description |
135138
| --------------------- | ------------------ | --------------------------------- | ------------------------------------------------------------------------------------------ |
139+
| allowedExports | `Array` | `["csv", "json", "txt"]` | Formats the user can export the data to. Allowed values: `csv`, `json`, `txt`, `xlsx` |
136140
| data | `Array` | - | Array of objects with the data to be displayed on the table |
137141
| columns | `Array` | - | Array of objects to specify how to render each column. Optional if `columnKeys` is set |
138142
| columnKeys | `Array` | - | Array of strings matching the object keys in `data`. Discarded if `columns` is set |
@@ -151,7 +155,7 @@ Only `data` e `columns` are required. Other props are optional.
151155
| sortingIndexComponent | `String`, `Object` | `VdtSortingIndex` | VueJS component for the sorting index on sortable columns |
152156
| sortingIconComponent | `String`, `Object` | `VdtSortingIcon` | VueJS component for the sorting icon on sortable columns |
153157
| footerComponent | `String`, `Object` | `null` | VueJS component for custom table footer |
154-
| allowedExports | `Array` | `["csv", "json", "txt"]` | Formats the user can export the data to. Allowed values: `csv`, `json`, `txt`, `xlsx` |
158+
| vKey | `String` | - | The `v-key`, the key in `data` used by Vue to track and distinguish array elements. |
155159

156160
### Columns
157161

src/components/DataTable.ts

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -65,22 +65,22 @@ export default defineComponent({
6565
type: [Object, String],
6666
default: null
6767
},
68-
perPageSizes: {
69-
type: Array,
70-
default: () => [10, 25, 50, 100, '*']
68+
isLoading: {
69+
type: Boolean,
70+
default: false,
7171
},
7272
lang: {
7373
type: String,
7474
default: "en"
7575
},
76-
isLoading: {
77-
type: Boolean,
78-
default: false,
79-
},
8076
loadingComponent: {
8177
type: [Object, String],
8278
default: () => "",
8379
},
80+
perPageSizes: {
81+
type: Array,
82+
default: () => [10, 25, 50, 100, '*']
83+
},
8484
showEntriesInfo: {
8585
type: Boolean,
8686
default: true
@@ -127,7 +127,11 @@ export default defineComponent({
127127
text: {
128128
type: Object,
129129
required: false
130-
}
130+
},
131+
vKey: {
132+
type: String,
133+
default: "",
134+
},
131135
},
132136

133137
data: () => {
@@ -196,7 +200,11 @@ export default defineComponent({
196200
const { searchableColumns, search } = this
197201

198202
// assign key to track row
199-
const data = this.data.map((value, index) => {
203+
const key = this.vKey;
204+
const data = this.data.map((value: any, index) => {
205+
if (key !== "" && value[key]) {
206+
index = value[key];
207+
}
200208
return {
201209
...(value as Object),
202210
_key: index,

src/components/Table/Table.vue

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,16 +41,16 @@
4141

4242
<!-- NON-EMPTY BODY -->
4343
<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" />
44+
<td
45+
v-for="(column, j) in columns"
46+
:key="data._key + '_' + j"
47+
>
48+
<component
49+
:is="column.component"
50+
:key="'__' + data._key + '_' + j"
51+
v-bind="{ data, ...column.componentProps }"
52+
@userEvent="emitUserEvent" />
5253
</td>
53-
</keep-alive>
5454
</tr>
5555
</tbody>
5656

0 commit comments

Comments
 (0)