vincjo / datatables

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

Suggestion: Add an API function to the get all rows #66

Closed mattpurlandncc closed 9 months ago

mattpurlandncc commented 9 months ago

I have a situation where I need to get all the rows to be used for a total calculation on a field. The reason why I believe an API function is required is because if the data is filtered by the API then I have no way of getting those filtered rows (I'd have to separately do that manually), so something like:

handler.getAllRows()

Would return all rows provided by the handler with the filtering applied (and maybe sorting? Whatever manipulation is being applied by the API)

vincjo commented 9 months ago

I agree, it makes sense if you need to extend API capabilities. Just released in 1.12.5: handler.getAllRows() returns filtered rows, as you suggested.

But I'd recommand to use the calculation helper if that meets your needs:

<script>
     export let handler
     const sum = handler.createCalculation('field').sum()
</script>

<p>Total: {$sum}</p>

Live examples: https://vincjo.fr/datatables/calculation

mattpurlandncc commented 9 months ago

That helper looks really helpful (saves me using lodash), so thanks for pointing it out - and thanks for adding the new function so quickly!