vincjo / datatables

A toolkit for creating datatable components with Svelte
https://vincjo.fr/datatables
MIT License
475 stars 18 forks source link

Clear selected #133

Closed timk closed 1 month ago

timk commented 1 month ago

Is there a way to clear/de-select all selected rows?

The way I've worked around it is once updated all selected rows as a group, I can run selectAll() twice, as a way to clear the selection. This poses UI updates and unnecessary processing. The other way around it is to subscribe to changes then iterate through a shadow array to call select() on each row, this also seems not optimal.

async function handleClearSelection() {
    handler.selectAll()
    handler.selectAll()
}
vincjo commented 1 month ago

Sorry for the late response.

Actually you can access an array of selected rows as a writable store, by using handler.getSelected()

So the best way would be:

const selected = handler.getSelected()

function handleClearSelection() {
    $selected = []
}

If your function runs outside of a Svelte component:

function handleClearSelection() {
    handler.getSelected().set([])
}