Skip to content

Commit

Permalink
fix(Table): fixed row deletion bug on deselect (#425)
Browse files Browse the repository at this point in the history
Co-authored-by: Benjamin Canac <canacb1@gmail.com>
  • Loading branch information
johnpuaoi and benjamincanac authored Jul 18, 2023
1 parent 0ea1f31 commit 46b444a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
4 changes: 1 addition & 3 deletions docs/content/4.data/1.table.md
Original file line number Diff line number Diff line change
Expand Up @@ -327,9 +327,7 @@ const selected = ref([people[1]])
You can use the `by` prop to compare objects by a field instead of comparing object instances. We've replicated the behavior of Headless UI [Combobox](https://headlessui.com/vue/combobox#binding-objects-as-values).
::

### Clickable

Add a `select` listener on your Table to make the rows clickable. The function will receive the row as the first argument.
You can alsso add a `select` listener on your Table to make the rows clickable. The function will receive the row as the first argument.

You can use this to navigate to a page, open a modal or even to select the row manually.

Expand Down
24 changes: 22 additions & 2 deletions src/runtime/components/data/Table.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<thead :class="ui.thead">
<tr :class="ui.tr.base">
<th v-if="modelValue" scope="col" class="ps-4">
<UCheckbox :checked="indeterminate || selected.length === rows.length" :indeterminate="indeterminate" @change="selected = $event.target.checked ? rows : []" />
<UCheckbox :checked="indeterminate || selected.length === rows.length" :indeterminate="indeterminate" @change="onChange" />
</th>

<th v-for="(column, index) in columns" :key="index" scope="col" :class="[ui.th.base, ui.th.padding, ui.th.color, ui.th.font, ui.th.size, column.class]">
Expand Down Expand Up @@ -210,6 +210,25 @@ export default defineComponent({
attrs.onSelect(row)
}
function selectAllRows () {
props.rows.forEach((row) => {
// If the row is already selected, don't select it again
if (selected.value.some((item) => compare(toRaw(item), toRaw(row)))) {
return
}
onSelect(row)
})
}
function onChange (event: any) {
if (event.target.checked) {
selectAllRows()
} else {
selected.value = []
}
}
return {
// eslint-disable-next-line vue/no-dupe-keys
ui,
Expand All @@ -225,7 +244,8 @@ export default defineComponent({
emptyState,
isSelected,
onSort,
onSelect
onSelect,
onChange
}
}
})
Expand Down

1 comment on commit 46b444a

@vercel
Copy link

@vercel vercel bot commented on 46b444a Jul 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

ui – ./

ui-nuxtlabs.vercel.app
ui.nuxtlabs.com
ui-git-dev-nuxtlabs.vercel.app

Please sign in to comment.