jQuery UI Widgets › Forums › Grid › Grid custom sort function
This topic contains 7 replies, has 3 voices, and was last updated by Peter Stoev 10 years, 9 months ago.
-
Author
-
In v2.8.3, the direction parameter passed to the custom sort function is different than what is shown in the documentation and in the sort example code.
If I click on the header to sort, the direction parameter has one of three values: true, false, null
If I selected an option on the drop menu for sorting, the direction parameter has one of three values: “ascending”, “descending”, null
If I set sortdirection on the data source object, e.g.,
{id: ‘cid’, localdata: this.data, datafields: dataFields, sort: this._customsort, sortcolumn: “c167”, sortdirection: true}
it passes whatever I set sortdirection to.When “sortby” is called, the direction parameter is set to whatever is specified, e.g., grid.jqxGrid(‘sortby’, ‘c167’, ‘ascending’) sets the direction parameter to ‘ascending’.
If “getstate” is called on the grid, followed by a subsequent “setstate”, the direction parameter is one of two values: “asc”, “desc”. The custom sort function is not called after “setstate” if the grid is not sorted when “getstate” is called.
And there was another instance that I can’t reproduce when the direction parameter was set to an object: {ascending: true, descending: false}, {ascending: false, descending: true}, null
The documentation needs to be updated, or the code changed so the direction parameter is consistent no matter where the custom sort function is called from.
So this is what I did to handle all the options:
if (!_.isUndefined(direction)) {
if (!(direction===true || direction===false || direction === null)) {
if (_.isString(direction)) {
if (direction == ‘asc’ || direction == ‘ascending’) direction = true;
if (direction == ‘desc’ || direction == ‘descending’) direction = false;
}
else if (direction.ascending && direction.ascending===true)
direction = true;
else if (direction.descending && direction.descending===true)
direction = false;
else
direction = null;
}
}
else
direction = null;Hello dfleming,
We recommend you to use the latest version of jQWidgets (3.2.1). Many issues have been fixed since the release of version 2.8.3 (more than ten months ago, at the time of writing).
Best Regards,
DimitarjQWidgets team
http://www.jqwidgets.com/Has this issue been fixed? Although I’d like to use the latest version, when I tried to use it a few weeks ago many other problems appeared with the grid. So we’re stuck with using v2.8.3 until I have time to work through the other problems.
Hi dfleming,
There are breaking changes since the release of 2.8.3, because there are multiple changes in jQWidgets and especially in jqxGrid. You can check the Release History page: http://www.jqwidgets.com/jquery-widgets-documentation/documentation/releasehistory/releasehistory.htm. For example, now we validate whether the users try to set a property, initialize a widget or call a method from invalid jQuery Selector(when there’s no such html element in the DOM). If that validation fails, an exception would be raised which will break your app until you fix your code.
Best Regards,
Peter StoevjQWidgets Team
http://www.jqwidgets.comHere’s a sample of the problem, using v3.2.1 and the sample custom sort function in your documentation online.
http://jsfiddle.net/donrfleming/U4mbJ/8/
The grid when first loaded should be sorted in descending order, as the source object is set up with these three members, per your documentation:
sort: customsortfunc,
sortdirection: ‘desc’,
sortcolumn: ‘productname’When the grid loads, the custom sort function is called with direction==’desc’ by invoking getstate,loadstate. That’s not recognized in your sample sort function, but is not null, so the function sorts in ascending order.
The same problem exists with jqxGrid(‘sortby’, ‘firstname’, ‘desc’) because desc is not handled so the grid is sorted in ascending order.
Click on a column header and it will sort properly, because direction set to true,false, or null is handled correctly.
Sort using the sort drop menu to see direction set to “ascending”, “descending”, or null.
Hi dfleming,
The custom sorting can be implemented through a custom function called when the Sort order is changed either by UI or API call and that is something which is custom which should be implemented by you, not by us. I am sorry, if the parameter is not what you expect, but when you pass “desc” as parameter and in your function’s implementation you check for “descending” parameter, not for “desc”, that would be the result. According to me, if you call “sortby” with parameter “desc” in your implementation, you should check for that parameter value, too.
Best Regards,
Peter StoevjQWidgets Team
http://www.jqwidgets.com/Hi Peter,
I’m not saying the code needs to be changed, although it would be nice if jqxGrid would call the custom sort function with a consistent value for direction.
Instead, these are the values that it is called with, without my code ever calling ‘sortby’:
true, false, null, ‘asc’, ‘desc’, ‘ascending’, ‘descending’, {ascending: true, descending: false}, {ascending: false, descending: true}
If the documentation had said that, or your example custom function had handled those options, I would have no problem.
The documentation instead just says ‘asc’ or ‘desc’ can be used for the sort direction.
I’m just asking that the documentation explains that those values are used for direction.
Hi dfleming,
Ok, you got a point. We will improve the documentation about the custom implementation of the sort function. We will improve our example.
Best Regards,
Peter StoevjQWidgets Team
http://www.jqwidgets.com/ -
AuthorPosts
You must be logged in to reply to this topic.