-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsort_table.js
More file actions
120 lines (106 loc) · 3.26 KB
/
sort_table.js
File metadata and controls
120 lines (106 loc) · 3.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
$(document).ready(function(){
$.getJSON("json_data.json",
function (data) {
var tr;
// This loop creates and appends table rows. Each line is creating
// a new row according to whatever is called by data[i].xxx
// See notes below for more
for (var i = 0; i < data.length; i++) {
tr = $('<tr/>');
// *You will have to change each </td> row for this to work
// Change each method after data[i] to reflect your table headers
// for example, if you had a th called 'names', use data[i].names
tr.append("<td>" + data[i].carrierName + "</td>");
tr.append("<td>" + data[i].planName + "</td>");
tr.append("<td>" + "$" + data[i].copay + "</td>");
tr.append("<td>" + "$" + data[i].premium + "</td>");
tr.append("<td>" + "$" + data[i].annualLimit + "</td>");
$('table').append(tr);
}
});
function sortTable(table, col, reversed) {
var tb = table.tBodies[0], // ignores thead
tr = Array.prototype.slice.call(tb.rows, 0), // put rows into array
i;
reversed = -((+reversed) || -1);
rows = convertRows(tr);
if(typeof rows[0].data[col] === "string"){
rows.sort(function(rowA, rowB){
return rowA.data[col].localeCompare(rowB.data[col]);
})
}else{
rows.sort(function(rowA, rowB){
if(rowA.data[col] < rowB.data[col]) return -1;
if(rowA.data[col] > rowB.data[col]) return 1;
return 0;
})
}
if(reversed === -1){
rows = rows.reverse();
}
rows.forEach(function(row){
tb.appendChild(row.element);
})
}
function sortByValue(rows, col){
if(typeof rows[0].data[col] === "string"){
rows.sort(function(rowA, rowB){
return rowA.data[col].localeCompare(rowB.data[col]);
})
}else{
rows.sort(function(rowA, rowB){
if(rowA.data[col] < rowB.data[col]) return -1;
if(rowA.data[col] > rowB.data[col]) return 1;
return 0;
})
}
}
function Row(tr){
return {
element: tr,
data: convertToValues(tr)
}
}
// convert rows into arrays of strings
function convertRows(rows){
return rows.map(Row);
}
// convert row into array of strings
function convertToValues(row){
// [$(string), $(string), etc., etc.]
// => [string, string, float, float]
var array = [];
for(var i = 0; i < row.cells.length; i++){
array[i] = convertToValue(row.cells[i].textContent.trim());
}
return array;
}
function convertToValue(string){
if(string.substr(0,1) === "$"){
return parseInt(string.substr(1));
}
return string;
}
function sortAsString(rows, col){
return rows.sort(function(a, b){
return a.localeCompare(b);
})
}
function makeSortable(table) {
var th = table.tHead, i;
th && (th = th.rows[0]) && (th = th.cells);
if (th) i = th.length;
else return; // if no `<thead>` then do nothing
while (--i >= 0) (function (i) {
var dir = 1;
th[i].addEventListener('click', function () {sortTable(table, i, (dir = 1 - dir))});
}(i));
}
function makeAllSortable(parent) {
parent = parent || document.body;
var t = parent.getElementsByTagName('table'), i = t.length;
while (--i >= 0) makeSortable(t[i]);
}
window.onload = function () {makeAllSortable();
};
});