I am writing custom sort on column. [sortById(a,b){} ]
How can I access column configuration inside my custom sort function to implement my sort logic?
Custom sort function take two parameter a and b where a: row and b: adjacent row.
How do I come to know function get called by which column if function is used in more than one column? ie How to access column id inside the custom sort function?
Currently - you can’t access that info directly. The only way is to bind the extra info to the sorting function like next
var sorter = function(id){
var config = $$("datatable").getColumnConfig(id);
return function(a, b){
return some_logic(config, a, b);
}
}
//and set sorting functions like next
$$("datatable").config.columns[2].sort = sorter("title");
$$("datatable").config.columns[3].sort = sorter("year");
Can you share why do you need to access config from the sorting function ?
cool stuff sir! Can you show working example. In my case $$(“datatable”) is undefined .
Hi,
$$(“datatable”) is just a reference to an existing datatable with the given ID.
webix.ui({ view:"datatable", id:"datatable", ...});
$$("datatable") // gets datatable object
thanks work! but I notice wired thing. When I have all the record in column blank my sorter function return 0 still my grid column is sorted randomly. Requested to put some light here.
Note to test the functionality I have created sorter method that return 0; only. Expected out put is unsorted column but I found randomly sort column. More specifically neworder.sort(sorter); produce randomly sorted list even when my sorter function return 0 every time.
This is caused by the nature of native javascript sorting mechanic, it uses fastsort algorithm, which doesn’t guarantee that order of equal items will be preserved.
You can use something like next
if (a.some == b.som) return a.id > b.id ? 1 : -1;
Which will sort items with equal value by their id ( so their order will be always the same )
Thanks work!