Skip to content

Instantly share code, notes, and snippets.

@bjrn
Created February 10, 2011 13:50
Show Gist options
  • Save bjrn/820532 to your computer and use it in GitHub Desktop.
Save bjrn/820532 to your computer and use it in GitHub Desktop.
a snippet that allows for having different sorters for each column
// Define some sorting functions
function NumericSorter(a, b) {
var x = a[sortcol], y = b[sortcol];
return sortdir * (x == y ? 0 : (x > y ? 1 : -1));
}
function RatingSorter(a, b) {
var xrow = a[sortcol], yrow = b[sortcol];
var x = xrow[3], y = yrow[3];
return sortdir * (x == y ? 0 : (x > y ? 1 : -1));
}
// Configure the columns
var columns = [
{id:"published", name:"Published", field:"published", sortable: true, sorter:NumericSorter, formatter:DateCellFormatter},
{id:"title", name:"Title", field:"title", sortable: true, sorter:NumericSorter, formatter:TitleCellFormatter},
{id:"publisher", name:"Publisher", field:"publisher", sorter:NumericSorter, sortable: true},
{id:"rating", name:"Rating", field:"rating", sortable: true, sorter:RatingSorter, formatter:RatingCellFormatter},
];
$(function() {
var grid = new Slick.Grid(".datatable", data_view.getItems(), columns, options);
grid.onSort.subscribe(function(e, args) {
sortdir = args.sortAsc ? 1 : -1;
sortcol = args.sortCol.field;
data_view.sort(args.sortCol.sorter, sortdir);
args.grid.invalidateAllRows();
args.grid.render();
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment