Created
July 22, 2015 06:28
-
-
Save sergiilagutin/4fceec903b4d47f63b17 to your computer and use it in GitHub Desktop.
Custom sorting using datatables.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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