Skip to content

Commit 070d2f8

Browse files
rdjanuarbenjamincanac
andauthoredOct 24, 2024··
fix(Table): indeterminate checkbox with pagination (#2439)
Co-authored-by: Benjamin Canac <canacb1@gmail.com>

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed
 

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

+19-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
<tr :class="ui.tr.base">
1111
<th v-if="modelValue" scope="col" :class="ui.checkbox.padding">
1212
<UCheckbox
13-
:model-value="indeterminate || selected.length === rows.length"
13+
:model-value="isAllRowChecked"
1414
:indeterminate="indeterminate"
1515
v-bind="ui.default.checkbox"
1616
aria-label="Select all"
@@ -276,7 +276,23 @@ export default defineComponent({
276276
}
277277
})
278278
279-
const indeterminate = computed(() => selected.value && selected.value.length > 0 && selected.value.length < props.rows.length)
279+
const getStringifiedSet = (arr: TableRow[]) => new Set(arr.map(item => JSON.stringify(item)))
280+
281+
const totalRows = computed(() => props.rows.length)
282+
283+
const countCheckedRow = computed(() => {
284+
const selectedData = getStringifiedSet(selected.value)
285+
const rowsData = getStringifiedSet(props.rows)
286+
287+
return Array.from(selectedData).filter(item => rowsData.has(item)).length
288+
})
289+
290+
const indeterminate = computed(() => {
291+
if (!selected.value || !props.rows) return false
292+
return countCheckedRow.value > 0 && countCheckedRow.value < totalRows.value
293+
})
294+
295+
const isAllRowChecked = computed(() => countCheckedRow.value === totalRows.value)
280296
281297
const emptyState = computed(() => {
282298
if (props.emptyState === null) return null
@@ -411,6 +427,7 @@ export default defineComponent({
411427
emptyState,
412428
// eslint-disable-next-line vue/no-dupe-keys
413429
loadingState,
430+
isAllRowChecked,
414431
onChangeCheckbox,
415432
openedRows,
416433
isSelected,

0 commit comments

Comments
 (0)
Please sign in to comment.