Skip to content

Commit 1b7e36c

Browse files
authoredOct 17, 2024··
fix(Table): checkbox not checked while using props by (#2401)

File tree

1 file changed

+29
-7
lines changed

1 file changed

+29
-7
lines changed
 

‎src/runtime/components/data/Table.vue

+29-7
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,13 @@
99
<thead :class="ui.thead">
1010
<tr :class="ui.tr.base">
1111
<th v-if="modelValue" scope="col" :class="ui.checkbox.padding">
12-
<UCheckbox :model-value="indeterminate || selected.length === rows.length" :indeterminate="indeterminate" v-bind="ui.default.checkbox" aria-label="Select all" @change="onChange" />
12+
<UCheckbox
13+
:model-value="indeterminate || selected.length === rows.length"
14+
:indeterminate="indeterminate"
15+
v-bind="ui.default.checkbox"
16+
aria-label="Select all"
17+
@change="onChange"
18+
/>
1319
</th>
1420

1521
<th v-if="$slots.expand" scope="col" :class="ui.tr.base">
@@ -73,7 +79,13 @@
7379
<template v-for="(row, index) in rows" :key="index">
7480
<tr :class="[ui.tr.base, isSelected(row) && ui.tr.selected, $attrs.onSelect && ui.tr.active, row?.class]" @click="() => onSelect(row)">
7581
<td v-if="modelValue" :class="ui.checkbox.padding">
76-
<UCheckbox v-model="selected" :value="row" v-bind="ui.default.checkbox" aria-label="Select row" @click.capture.stop="() => onSelect(row)" />
82+
<UCheckbox
83+
:model-value="isSelected(row)"
84+
v-bind="ui.default.checkbox"
85+
aria-label="Select row"
86+
@change="onChangeCheckbox($event, row)"
87+
@click.capture.stop="() => onSelect(row)"
88+
/>
7789
</td>
7890

7991
<td
@@ -129,7 +141,7 @@ import { table } from '#ui/ui.config'
129141
const config = mergeConfig<typeof table>(appConfig.ui.strategy, appConfig.ui.table, table)
130142
131143
function defaultComparator<T> (a: T, z: T): boolean {
132-
return a === z
144+
return JSON.stringify(a) === JSON.stringify(z)
133145
}
134146
135147
function defaultSort (a: any, b: any, direction: 'asc' | 'desc') {
@@ -284,7 +296,7 @@ export default defineComponent({
284296
return props.by(a, z)
285297
}
286298
287-
function isSelected (row) {
299+
function isSelected (row: TableRow) {
288300
if (!props.modelValue) {
289301
return false
290302
}
@@ -306,7 +318,7 @@ export default defineComponent({
306318
}
307319
}
308320
309-
function onSelect (row) {
321+
function onSelect (row: TableRow) {
310322
if (!$attrs.onSelect) {
311323
return
312324
}
@@ -338,7 +350,16 @@ export default defineComponent({
338350
}
339351
}
340352
341-
function getRowData (row: Object, rowKey: string | string[], defaultValue: any = '') {
353+
function onChangeCheckbox (checked: boolean, row: TableRow) {
354+
if (checked) {
355+
selected.value.push(row)
356+
} else {
357+
const index = selected.value.findIndex((item) => compare(item, row))
358+
selected.value.splice(index, 1)
359+
}
360+
}
361+
362+
function getRowData (row: TableRow, rowKey: string | string[], defaultValue: any = '') {
342363
return get(row, rowKey, defaultValue)
343364
}
344365
@@ -386,6 +407,7 @@ export default defineComponent({
386407
emptyState,
387408
// eslint-disable-next-line vue/no-dupe-keys
388409
loadingState,
410+
onChangeCheckbox,
389411
openedRows,
390412
isSelected,
391413
onSort,
@@ -397,4 +419,4 @@ export default defineComponent({
397419
}
398420
}
399421
})
400-
</script>
422+
</script>

0 commit comments

Comments
 (0)
Please sign in to comment.