Add a "select all" option for `addSelectedRows` plugin
See original GitHub issueThis is probably my favourite Svelte libraries, it’s so useful and versatile! Thank you so much for making this.
I do have one small gripe, which is that there isn’t a “select all” option for the addSelectedRows
plugin. This can be hacked in like so:
// on init
let tableColumns = [];
let selectAll = writable(false);
if (selectable) {
tableColumns.push(table.display({
id: 'selected',
header: () => createRender(Checkbox, { // renders the "select all"
isSelected: selectAll
}),
cell: ({ row }, { pluginStates }) => {
const { isSelected } = pluginStates.select.getRowState(row);
return createRender(Checkbox, {
isSelected
});
},
plugins: {
sort: {
disable: true
}
}
}));
}
// ... continue initialising columns
and then subscribing to the newly created selectAll
to cascade the changes to all other checkboxes:
const { selectedDataIds } = pluginStates.select;
if (selectable) {
selectAll.subscribe(val => {
if (!val) selectedDataIds.clear();
else {
let rows = $pageRows;
let newSelected = {};
for (const row of rows) {
newSelected[row.id] = true;
}
$selectedDataIds = { ...newSelected };
}
});
}
This works fine, but could run in to issues when trying to do things the other way around - ie. trying to select the “select all” option when all rows are selected. You could probably do something like:
selectedDataIds.subscribe(val => {
if (val.length !== $pageRows.length) return;
let newValue = true;
for (const rowVal of val) {
if (!rowVal) {
newValue = false;
break; // exit early
}
}
$selectAll = newValue;
});
But you may run in to issues w/ constantly cascading changes between the 2 subscribe
functions.
It’d be nice if there was a sane default for a select all option, especially if it were performant - I’m not too worried about the performance of the library, but for my use case actually these headless tables take up the bulk of the application so I do get slightly wary about having to implement some of these things.
Thanks again for the library, however, it’s saved me countless hours over the last month (and will continue to do so)!
Issue Analytics
- State:
- Created a year ago
- Reactions:1
- Comments:5 (4 by maintainers)
Top GitHub Comments
Sure thing, I’l look into providing a
selectAll
method on the data IDs store that’s bound to the internal row state.@MellenIO I’ve added this to the plugin in v0.13.3.
You can find its documentation here.