Discuss anything related to web development but no technical support questions


Post by lxxrxns »

Hi there,

I have a resourceStore that I want to sort by a custom number (called "resourceOrder" in my database/array). I have the following code:

resourceStore : {
    readUrl        : 'php/schedule/resource/read.php',
    sorters        : [{ field: 'resourceOrder', ascending: true }, { field: 'name', ascending: true}],
    ...
}

This seemed to work fine (when using <10 resources), however, today I found out that when using more than 10 resources, for example 14 (numbered from 1 to 14), it sorts in the following way:

1
10
11
12
13
14
2
3
4
etc.

It seems to sort the number "alphabetically"
How can I tell scheduler to treat the "resourceOrder" field as an integer/number when sorting on it?

NotE: I realise I can divert to numbering like this: 001, 002, 003, or A, B, C etc. but this is not an ideal solution, because the resourceOrder is numeric in other parts of my (server-sided) code.

Thanks for the help!

Laurens


Post by marcio »

Hey Laurens,

You can use fn as a custom function and then compare the number. https://bryntum.com/products/scheduler/docs/api/Core/data/mixin/StoreSort#typedef-Sorter

sorters        : [{ field: 'resourceOrder', fn: (valueA, valueB) => { // do comparison here }}]

Best regards,
Márcio


Post by lxxrxns »

Hi marcio, thanks for the quick response.

Which values would I have to compare? I'm assuming you mean some kind of function that compares different rows with eachother and returns a number based on their relative values for "resourceOrder", but I wouldn't know how to write this exactly...

Thanks for the help!


Post by marcio »

Hey,

It would be something like this

    resourceStore : {
        sorters : [{
            field : 'resourceOrder',
            fn    : (recordA, recordB) => {
                return recordA.resourceOrder - recordB.resourceOrder;
            }
        }]
    }

Best regards,
Márcio


Post by lxxrxns »

Hi marcio,

That seems to work for when resourceOrders are set indeed, but it also creates an order when resourceOrders are in fact empty in my database (which is when I want to sort by 'name'):

sorters        : [{ field: 'resourceOrder', ascending: true }, { field: 'name', ascending: true}],

Is there a way to make your fn : (recordA, recordB) => {} function only return a value if resourceOrder is in fact set (not NULL)?

Thanks again for the help!!


Post by marcio »

Hey,

The function works just like a JS sort logic, so, before the return, you should return 0 to maintain the order of the records (in that case, ignore the resourceOrder sort function).

https://developer.mozilla.org/pt-BR/docs/Web/JavaScript/Reference/Global_Objects/Array/sort

Best regards,
Márcio


Post by lxxrxns »

Thanks a lot!


Post Reply