vincjo / datatables

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

[Suggestion] Add 3rd paramter type of Object to allow sorting using returned format of getSorted() #17

Closed mattpurlandncc closed 1 year ago

mattpurlandncc commented 1 year ago

I'm not sure if the title makes sense, but I'd like to be able to sort like the following:

handler.sort({
    identifier: 'column_name',
    direction: 'desc'
});

This is the same format as getSorted() and would allow me to pass these via the URL to the backend:

http://localhost?sort[identifier]=column_name&sort[direction]=desc

Which could be sent as a prop to the frontend so I can sort the data on page load

mattpurlandncc commented 1 year ago

Currently I have to do the following to apply the sorting on load:

if (sort) {
    if(sort.direction === 'asc') {
        handler.sortAsc(sort.identifier);
    } else {
        handler.sortDesc(sort.identifier);
    }
}

instead of the proposed:

if(sort) {
    handler.sort(sort);
}
vincjo commented 1 year ago

Ok. There's a new method handler.applySorting(), that could fit with your use case.

Bellow are the params :

handler.applySorting({
    orderBy: 'column_name',
    direction: 'asc'
})
MattPurland commented 1 year ago

@vincjo any chance it can be 'identifier' instead of 'orderBy'? That way it matches that of getSorted() output

vincjo commented 1 year ago

unfortunately, "identifier" = "orderBy.toString()", it can't be of type Function

"orderBy" can be a callback function, not "identifier"