vincjo / datatables

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

Feed data and pagination from backend api #30

Closed kefahi closed 1 year ago

kefahi commented 1 year ago

Thank you for the amazing and elegant work!

My use case, involves having a headless CMS that provides api to access, paginate, filter and sort the data. The number of data rows is in the millions and hence can not be pre-fetched.

I was wondering if it is easy to connect datatables with arbitrary backend api (so the data is not all readily available) where we can retrieve the data for each page and apply any necessary filtration / sorting. All done for a backend api.

Should I, with my case above, implement/extend DataHander class?

0xChupaCabra commented 1 year ago

+1 i need to load lot of data and it would be nice to see an example of server side pagination

vincjo commented 1 year ago

Currently this lib can't help at all in the case where the data processing logic applies on the server side (searching, filtering, paging, sorting...) I'm working on it.

In any case, if you are looking for something operational right away, there are surely more advanced tools than this one

vincjo commented 1 year ago

We need an handler that can trigger requests to fetch the data once table state changes. The major concern will be to provide something fairly universal because there are a lot of different API designs.

Maybe by using a derived store to get the table state:

const params = handler.getState()
// params: 
{ 
    search: 'john', 
    page: { number: 2, offest: 40 }, 
    rowsPerPage: 20, 
    sort: [ { key: 'first_name', direction: 'asc' }, {key: 'last_name', direction: 'asc'} ] 
}

// users?per_page=20&offset=40&search=john

const response = await fetch(`/users?per_page=${params.rowsPerPage}&offset={params.page.offset}&search=${params.search}`)
PercivalFigaro commented 1 year ago

+1, this feature would be much appreciated. Will be working with millions of table entries, so front-end pagination and operations really isn't an option

vincjo commented 1 year ago

Made a draft implementation with some examples using public APIs. https://vincjo.fr/datatables/remote/examples

We must define an api-specific reload() function which is triggered at each change of state.

handler.onChange( (state) => reload(state) )

The reload function is executed using the following method:

handler.invalidate()

More details here: https://vincjo.fr/datatables/remote/basic-usage