Skip to content
This repository has been archived by the owner on Sep 30, 2024. It is now read-only.

Commit

Permalink
fix(DataTable): remove selection when row is not exist anymore
Browse files Browse the repository at this point in the history
  • Loading branch information
tewshi authored Mar 27, 2024
1 parent 2a31845 commit d567b5a
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 9 deletions.
12 changes: 9 additions & 3 deletions example/src/views/DataTableView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -782,6 +782,10 @@ function toggleRow(row: any, expanded: any[] | undefined) {
else
expanded?.push(row);
}
function removeRow(table: DataTableProps, id: number) {
table.rows = table.rows.filter(row => row.id !== id);
}
</script>

<template>
Expand Down Expand Up @@ -833,15 +837,17 @@ function toggleRow(row: any, expanded: any[] | undefined) {
<template #header.text.address.city>
city custom header
</template>
<template #item.action>
<template #item.action="{ row }">
<RuiButton
icon
variant="text"
size="sm"
@click="removeRow(table, row.id)"
>
<RuiIcon
name="more-fill"
color="primary"
name="delete-bin-line"
size="14"
color="error"
/>
</RuiButton>
</template>
Expand Down
6 changes: 6 additions & 0 deletions src/components/forms/checkbox/Checkbox.vue
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,12 @@ watch(indeterminate, (val) => {
input.value = 'on';
}
});
watch(modelValue, (val) => {
const input = get(el);
if (input && input.checked !== val)
input.checked = val;
});
</script>

<template>
Expand Down
22 changes: 16 additions & 6 deletions src/components/tables/DataTable.vue
Original file line number Diff line number Diff line change
Expand Up @@ -826,11 +826,10 @@ function resetCheckboxShiftState() {
* toggles a single row
* @param {boolean} checked checkbox state
* @param {string} value the id of the selected row
* @param {number} index the index of the selected row
* @param {boolean} userAction whether the select triggered by user manually
*
*/
function onSelect(checked: boolean, value: string, index: number, userAction = false) {
function onSelect(checked: boolean, value: string, userAction = false) {
if (get(shiftClicked) && userAction)
return;
Expand Down Expand Up @@ -875,7 +874,7 @@ function onCheckboxClick(event: any, value: string, index: number) {
const valueToApply = isSelected(lastSelectedData[id]);
if (lastIndex === index) {
onSelect(!valueToApply, value, index);
onSelect(!valueToApply, value);
}
else {
const from = Math.min(lastIndex, index);
Expand All @@ -884,7 +883,7 @@ function onCheckboxClick(event: any, value: string, index: number) {
for (let i = from; i <= to; i++) {
const currSelectedData = tableData[i];
if (isRow(currSelectedData) && !isDisabledRow(currSelectedData[id]))
onSelect(valueToApply, currSelectedData[id], i);
onSelect(valueToApply, currSelectedData[id]);
}
}
Expand All @@ -898,6 +897,13 @@ function onCheckboxClick(event: any, value: string, index: number) {
}
}
function deselectRemovedRows() {
get(selectedData)?.forEach((key: TableRow[TableRowKey]) => {
if (isSelected(key) && !get(visibleIdentifiers).includes(key))
onSelect(false, key, true);
});
}
function scrollToTop() {
const { top } = useElementBounding(table);
const { top: scrollerTop } = useElementBounding(scroller);
Expand Down Expand Up @@ -971,7 +977,11 @@ watch(search, () => {
resetCheckboxShiftState();
});
watch(sorted, setInternalTotal);
watch(sorted, (items) => {
if (!get(multiPageSelect))
deselectRemovedRows();
setInternalTotal(items);
});
onMounted(() => {
setInternalTotal(get(sorted));
Expand Down Expand Up @@ -1159,7 +1169,7 @@ onMounted(() => {
color="primary"
class="select-none"
hide-details
@input="onSelect($event, row[rowAttr], index, true)"
@input="onSelect($event, row[rowAttr], true)"
@click="onCheckboxClick($event, row[rowAttr], index)"
/>
</td>
Expand Down

0 comments on commit d567b5a

Please sign in to comment.