Backgrid sorting with undefined values -
i using backgrid. found 1 of columns has 'undefined' value. backgrid sorting doesn't work correctly when there undefined in column. looked source code.
onclick: function (e) { e.preventdefault();
var columnname = this.column.get("name"); if (this.column.get("sortable")) { if (this.direction() === "ascending") { this.sort(columnname, "descending", function (left, right) { var leftval = left.get(columnname); var rightval = right.get(columnname); if (leftval === rightval) { return 0; } else if (leftval > rightval) { return -1; } return 1; }); } else if (this.direction() === "descending") { this.sort(columnname, null); } else { this.sort(columnname, "ascending", function (left, right) { var leftval = left.get(columnname); var rightval = right.get(columnname); if (leftval === rightval) { return 0; } else if (leftval < rightval) { return -1; } return 1; }); } }
},
i changed code following , sorting works correctly (assume undefined less value):
onclick: function (e) { e.preventdefault();
var columnname = this.column.get("name"); if (this.column.get("sortable")) { if (this.direction() === "ascending") { this.sort(columnname, "descending", function (left, right) { var leftval = left.get(columnname); var rightval = right.get(columnname); if (leftval === undefined && rightval != undefined) { return 1; } if (leftval != undefined && rightval === undefined) { return -1; } if (leftval === rightval) { return 0; } else if (leftval > rightval) { return -1; } return 1; }); } else if (this.direction() === "descending") { this.sort(columnname, null); } else { this.sort(columnname, "ascending", function (left, right) { var leftval = left.get(columnname); var rightval = right.get(columnname); if (leftval === undefined && rightval != undefined) { return -1; } if (leftval != undefined && rightval === undefined) { return 1; } if (leftval === rightval) { return 0; } else if (leftval < rightval) { return -1; } return 1; }); } }
},
is there other way deal undefined value when sorting? thanks!
actually, upgraded backgrid latest 0.3.5 supports sortvalue property backgrid.column: http://wyuenho.github.io/backgrid/api/index.html#!/api/backgrid.column-cfg-defaults. can define function return empty string undefine column value make sorting work correctly. solve problem.
Comments
Post a Comment