Skip to content

Instantly share code, notes, and snippets.

@sergiilagutin
Created July 22, 2015 06:28
Show Gist options
  • Save sergiilagutin/4fceec903b4d47f63b17 to your computer and use it in GitHub Desktop.
Save sergiilagutin/4fceec903b4d47f63b17 to your computer and use it in GitHub Desktop.
Custom sorting using datatables.js
// sort data in column using string representation on numeric values
// natural order: 1, 3, 2, 0
$(function () {
$.fn.dataTable.ext.order['my-sort'] = function (settings, col) {
return this.api().column(col, {order: 'index'}).nodes().map(function (td, i) {
var text;
switch ($(td).text()) {
case "0":
text = "zero";
break;
case "1":
text = "one";
break;
case "2":
text= "two";
break;
case "3":
text= "three";
break;
}
return text;
});
};
$(".my-table").dataTable({
"columns": [
null,
{"orderDataType": "my-sort", type: "string"}
]
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment