Files
nairobi/app/assets/javascripts/table_sortable.js
Senén Rodero Rodríguez 99f8bb4491 Fix collission with sortable.js script
TableSortable and Sortable javascripts were using the same CSS
class name to define completely different and separated
behaviours causing unexpected errors. Now `sortable.js` script
will use `.sortable` class and `table_sortable.js` will use
`.table-sortable` instead.
2020-08-05 10:28:13 +02:00

34 lines
968 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);