This is a migrated thread and some comments may be shown as answers.

Grid column sortable.compare not run when sorting from dataSource

5 Answers 457 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Bridge24
Top achievements
Rank 1
Iron
Bridge24 asked on 07 Jun 2019, 02:00 PM

Look at this dojo

https://dojo.telerik.com/@foxontherock/imObiluK

"fld3" is a special field based on array, and the "sort" is configured to sort sort by array.length.

That's why we used the column attribute sortable.compare (function).

It works fine when we click on the grid header, from single or multiple sortable configuration.

But, when we sort it from code, using the kgrid.dataSource.sort(...) method, the custom compare function is not executed.

There's a grid databinding, The up-down arrow on top of the grid column is moving according to the "direction", but the data doesn't move.

In the dojo, sort on fld3 column, and switch the "direction" attribute in the "json textbox" between "asc" and "desc", and click the "set" button.

You'll see the arrow moving, but not the data. 
And the fld4 timestamp column is not changing, proof that the compare function is not executed.

Thank you

5 Answers, 1 is accepted

Sort by
0
Bridge24
Top achievements
Rank 1
Iron
answered on 08 Jun 2019, 12:49 PM

Hmmm after a few more readings, I think that I know your response... let me guess.

The "Sorting.compare" option is send to the datasource from the grid when the user click the columns, but is not used anywhere else.  So, I need to send this same parameter to the datasource directly... 

Ok, but if I need to set it only once and make it easy?  

I have 2 suggestions for you.

1. add a sort "method" to the grid, that will call the sort of the data Source with the column configuration.

2. Make it very global and reusable for all component:
Allow us to set it directly in the "model", like you can see in this code sample:

dataSource: {
 data: data,
 schema: {
  model: {
   id: "id",
   fields: {
    fld2: {
     parse: function(e){return "fixed custom"}                   
    },
    fld3: { // SUGGESTION TO CONFIGURE COMPARE GLOBALLY IN MODEL
     compare: function(a, b){return -1 /* ... */ }
    }
...

https://dojo.telerik.com/@foxontherock/umuHoFOr

 

0
Viktor Tachev
Telerik team
answered on 11 Jun 2019, 09:01 AM
Hi,

Your observations for the sort.compare function are correct. When calling the dataSource sort method there is no compare function specified and since the underlying field is array the DataSource does not know how to order the data. 

With that said, you can pass a compare function to the sort method and it will be used for sorting the data. In the current setup I suggest defining a function that will be used for comparing the records. Then reference that function in the columns.sortable.compare and sort.compare options.

For your convenience I have updated the dojo sample to illustrate the approach:


Give it a try and let me know how it works for you.


Regards,
Viktor Tachev
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
0
Bridge24
Top achievements
Rank 1
Iron
answered on 11 Jun 2019, 12:43 PM

Thank you for your suggestion, that's exactly what we did finally... we create a "sort" function, and before sending it to the datasource, we loop the columns and add the "custom" compare functions.

That said, I still think that you should allow us to configure the "customCompare" function in the datasource "model / schema", to allow it to be inherited to all datasource needs...

Sometimes, we take the grid datasource and I send it to the scheduler, or any other kendo ui component. 

Or we reorder the data before sending it to a workbook for export.

Having the datasource knowing all the customcompare configuration for each field would be great!

Thank you.

0
Bridge24
Top achievements
Rank 1
Iron
answered on 12 Jun 2019, 02:13 PM

For those who read this thread with the same issue...

Please note that if you're using a custom compare function for grouping, you also need to do the same "hack" if you apply grouping directly in the dataSource, and not from a field drag-drop on the grid.

Also, if you configure the grid with the dataSource + grouping, you need to set the custom compare (for sort and grouping ) at both places, in "columns" and in dataSource.sort / dataSource,groups.

If telerik allow us to configure customcompare functions for sorting and grouping in the kendo schema model, it will always work as expected without any "hack", and will also work in all others UI components, not just in grid.

0
Viktor Tachev
Telerik team
answered on 13 Jun 2019, 10:53 AM
Hi,

Thank you for the feedback. 

When grouping is applied sorting a column will apply the order inside each of the Groups. In case there is a custom sort.compare function it will be used to apply the sorting.

If you would like to customize the order of the groups themselves a group.compare function will be necessary


Regards,
Viktor Tachev
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Tags
Grid
Asked by
Bridge24
Top achievements
Rank 1
Iron
Answers by
Bridge24
Top achievements
Rank 1
Iron
Viktor Tachev
Telerik team
Share this question
or