Files
nairobi/app/assets/javascripts/table_sortable.js
Javi Martín 953fc7ddb0 Update deprecated jQuery syntax
These methods had already been superseeded by other methods since jQuery
1.7 or 1.8, and jQuery 3.0 will deprecate them.
2019-11-07 15:58:49 +01:00

34 lines
967 B
JavaScript

(function() {
"use strict";
App.TableSortable = {
getCellValue: function(row, index) {
return $(row).children("td").eq(index).text();
},
comparer: function(index) {
return function(a, b) {
var valA, valB;
valA = App.TableSortable.getCellValue(a, index);
valB = App.TableSortable.getCellValue(b, index);
if ($.isNumeric(valA) && $.isNumeric(valB)) {
return valA - valB;
} else {
return valA.localeCompare(valB);
}
};
},
initialize: function() {
$("table.sortable th").on("click", function() {
var rows, table;
table = $(this).parents("table").eq(0);
rows = table.find("tbody tr").toArray().sort(App.TableSortable.comparer($(this).index()));
this.asc = !this.asc;
if (this.asc) {
table.append(rows);
} else {
table.append(rows.reverse());
}
});
}
};
}).call(this);