React-admin: Multiple sort fields

Created on 2 Dec 2018  路  3Comments  路  Source: marmelab/react-admin

Is your feature request related to a problem? Please describe.
Sometimes I need my initial sort like sort prop of List to include 2 fields. Thus I can sort on a date and some flag, say most important flagged things come first.

Describe the solution you'd like
It could be nice if react-admin supports multiple sort items down to dataProvider.

Describe alternatives you've considered
None

Additional context
None

Most helpful comment

I think this feature could be really useful and the code would not be much more complex.
Why not having this functionality in the core instead of relying on some hacks like the previous message?

All 3 comments

We won't support it in the core. I suggest you write your own Datagrid to support that.

I solved it with
<List sort={{ field: 'year,name', order: 'DESC,ASC' }} ..... />

and adding something to your dataProvider that can handle it. The way it should handle the sort parameters though depends on how your API expects to receive them in the query. This is an extract from my dataProvider implementation concerning sort.

const convertRESTRequestToHTTP = (type, resource, params = {}) => {
    // ...
    switch (type) {
        case GET_LIST:
        case GET_MANY_REFERENCE: {
            const { page, perPage } = params.pagination || {};
            const { field, order } = params.sort || {};
            // ...
            const query = {};
            if (field) {
                const fieldParts = field.split(',');
                const orderParts = order.split(',');
                query.order = fieldParts
                    .map(
                        (field, index) =>
                            `${field}.${orderParts[index].toLowerCase()}`
                    )
                    .join(',');
            }
            // ...

It's not foolproof (order may be missing), but I'm the only one using this dataProvider so I should be safe for a while.

I think this feature could be really useful and the code would not be much more complex.
Why not having this functionality in the core instead of relying on some hacks like the previous message?

Was this page helpful?
0 / 5 - 0 ratings