jQWidgets Forums
Forum Replies Created
Viewing 2 posts - 1 through 2 (of 2 total)
-
Author
-
August 3, 2015 at 2:37 pm in reply to: Can't get custom sort to work with json data Can't get custom sort to work with json data #74452
I got it to work. It was a simple solution. Setting data = $(“#jqxgrid”).jqxGrid(‘source’).loadedData in the first line of the custom sort function works fine. The issue was value1 = String(value1.innerHTML).toLowerCase(); doesn’t work. value1 is not a dom element. Here is what I did instead:
value1 = String(value1).toLowerCase(); value1= value1.substring(value1.indexOf('">')+2,value1.indexOf('</'));
Here is my full custom sort script, for anybody trying to do a custom sort on a JSON datasource without going back to the server:
var customsortfunc = function (column, direction) { data=$("#jqxgrid").jqxGrid('source').loadedData; var sortdata = new Array(); if (direction == 'ascending') direction = true; if (direction == 'descending') direction = false; if (direction != null) { for (i = 0; i < data.length; i++) { sortdata.push(data[i]); } } else sortdata = data; var tmpToString = Object.prototype.toString; Object.prototype.toString = (typeof column == "function") ? column : function () { return this[column] }; if (direction != null) { sortdata.sort(compare); if (!direction) { sortdata.reverse(); } } source.localdata = sortdata; $("#jqxgrid").jqxGrid('databind', source); Object.prototype.toString = tmpToString; } var compare = function (value1, value2) { //value1 = String(value1.innerHTML).toLowerCase(); value1 = String(value1).toLowerCase(); value1= value1.substring(value1.indexOf('">')+2,value1.indexOf('</')); value2 = String(value2).toLowerCase(); value2 = value2.substring(value2.indexOf('">')+2,value2.indexOf('</')); try { var tmpvalue1 = parseFloat(value1); if (isNaN(tmpvalue1)) { if (value1 < value2) { return -1; } if (value1 > value2) { return 1; } } else { var tmpvalue2 = parseFloat(value2); if (tmpvalue1 < tmpvalue2) { return -1; } if (tmpvalue1 > tmpvalue2) { return 1; } } } catch (error) { var er = error; } return 0; };
July 31, 2015 at 4:31 pm in reply to: Can't get custom sort to work with json data Can't get custom sort to work with json data #74381I don’t want to do the sorting on the server. What other options do I have. Should I just Ajax my JSON data first and process it as local data so I can do my custom sorting client side?
-
AuthorPosts
Viewing 2 posts - 1 through 2 (of 2 total)