| 1 |
|
| 2 |
|
| 3 |
(function($) { |
| 4 |
$.fn.stupidtable = function(sortFns) { |
| 5 |
return this.each(function() { |
| 6 |
var $table = $(this); |
| 7 |
sortFns = sortFns || {}; |
| 8 |
sortFns = $.extend({}, $.fn.stupidtable.default_sort_fns, sortFns); |
| 9 |
$table.data('sortFns', sortFns); |
| 10 |
$table.stupidtable_build(); |
| 11 |
|
| 12 |
$table.on("click.stupidtable", "thead th", function() { |
| 13 |
$(this).stupidsort(); |
| 14 |
}); |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
var $th_onload_sort = $table.find("th[data-sort-onload=yes]").eq(0); |
| 19 |
$th_onload_sort.stupidsort(); |
| 20 |
}); |
| 21 |
}; |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
$.fn.stupidtable.default_settings = { |
| 27 |
should_redraw: function(sort_info){ |
| 28 |
return true; |
| 29 |
}, |
| 30 |
will_manually_build_table: false |
| 31 |
}; |
| 32 |
$.fn.stupidtable.dir = {ASC: "asc", DESC: "desc"}; |
| 33 |
$.fn.stupidtable.default_sort_fns = { |
| 34 |
"int": function(a, b) { |
| 35 |
return parseInt(a, 10) - parseInt(b, 10); |
| 36 |
}, |
| 37 |
"float": function(a, b) { |
| 38 |
return parseFloat(a) - parseFloat(b); |
| 39 |
}, |
| 40 |
"string": function(a, b) { |
| 41 |
return a.toString().localeCompare(b.toString()); |
| 42 |
}, |
| 43 |
"string-ins": function(a, b) { |
| 44 |
a = a.toString().toLocaleLowerCase(); |
| 45 |
b = b.toString().toLocaleLowerCase(); |
| 46 |
return a.localeCompare(b); |
| 47 |
} |
| 48 |
}; |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
$.fn.stupidtable_settings = function(settings) { |
| 53 |
return this.each(function() { |
| 54 |
var $table = $(this); |
| 55 |
var final_settings = $.extend({}, $.fn.stupidtable.default_settings, settings); |
| 56 |
$table.stupidtable.settings = final_settings; |
| 57 |
}); |
| 58 |
}; |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
$.fn.stupidsort = function(force_direction){ |
| 64 |
var $this_th = $(this); |
| 65 |
var datatype = $this_th.data("sort") || null; |
| 66 |
|
| 67 |
|
| 68 |
if (datatype === null) { |
| 69 |
return; |
| 70 |
} |
| 71 |
|
| 72 |
var dir = $.fn.stupidtable.dir; |
| 73 |
var $table = $this_th.closest("table"); |
| 74 |
|
| 75 |
var sort_info = { |
| 76 |
$th: $this_th, |
| 77 |
$table: $table, |
| 78 |
datatype: datatype |
| 79 |
}; |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
if(!$table.stupidtable.settings){ |
| 84 |
$table.stupidtable.settings = $.extend({}, $.fn.stupidtable.default_settings); |
| 85 |
} |
| 86 |
|
| 87 |
sort_info.compare_fn = $table.data('sortFns')[datatype]; |
| 88 |
sort_info.th_index = calculateTHIndex(sort_info); |
| 89 |
sort_info.sort_dir = calculateSortDir(force_direction, sort_info); |
| 90 |
|
| 91 |
$this_th.data("sort-dir", sort_info.sort_dir); |
| 92 |
$table.trigger("beforetablesort", {column: sort_info.th_index, direction: sort_info.sort_dir, $th: $this_th}); |
| 93 |
|
| 94 |
|
| 95 |
$table.css("display"); |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
setTimeout(function() { |
| 100 |
if(!$table.stupidtable.settings.will_manually_build_table){ |
| 101 |
$table.stupidtable_build(); |
| 102 |
} |
| 103 |
var table_structure = sortTable(sort_info); |
| 104 |
var trs = getTableRowsFromTableStructure(table_structure, sort_info); |
| 105 |
|
| 106 |
if(!$table.stupidtable.settings.should_redraw(sort_info)){ |
| 107 |
return; |
| 108 |
} |
| 109 |
$table.children("tbody").append(trs); |
| 110 |
|
| 111 |
updateElementData(sort_info); |
| 112 |
$table.trigger("aftertablesort", {column: sort_info.th_index, direction: sort_info.sort_dir, $th: $this_th}); |
| 113 |
$table.css("display"); |
| 114 |
|
| 115 |
}, 10); |
| 116 |
return $this_th; |
| 117 |
}; |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
$.fn.updateSortVal = function(new_sort_val){ |
| 124 |
var $this_td = $(this); |
| 125 |
if($this_td.is('[data-sort-value]')){ |
| 126 |
|
| 127 |
$this_td.attr('data-sort-value', new_sort_val); |
| 128 |
} |
| 129 |
$this_td.data("sort-value", new_sort_val); |
| 130 |
return $this_td; |
| 131 |
}; |
| 132 |
|
| 133 |
|
| 134 |
$.fn.stupidtable_build = function(){ |
| 135 |
return this.each(function() { |
| 136 |
var $table = $(this); |
| 137 |
var table_structure = []; |
| 138 |
var trs = $table.children("tbody").children("tr"); |
| 139 |
trs.each(function(index,tr) { |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
var ele = { |
| 145 |
$tr: $(tr), |
| 146 |
columns: [], |
| 147 |
index: index |
| 148 |
}; |
| 149 |
|
| 150 |
$(tr).children('td').each(function(idx, td){ |
| 151 |
var sort_val = $(td).data("sort-value"); |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
if(typeof(sort_val) === "undefined"){ |
| 156 |
var txt = $(td).text(); |
| 157 |
$(td).data('sort-value', txt); |
| 158 |
sort_val = txt; |
| 159 |
} |
| 160 |
ele.columns.push(sort_val); |
| 161 |
}); |
| 162 |
table_structure.push(ele); |
| 163 |
}); |
| 164 |
$table.data('stupidsort_internaltable', table_structure); |
| 165 |
}); |
| 166 |
}; |
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
var sortTable = function(sort_info){ |
| 172 |
var table_structure = sort_info.$table.data('stupidsort_internaltable'); |
| 173 |
var th_index = sort_info.th_index; |
| 174 |
var $th = sort_info.$th; |
| 175 |
|
| 176 |
var multicolumn_target_str = $th.data('sort-multicolumn'); |
| 177 |
var multicolumn_targets; |
| 178 |
if(multicolumn_target_str){ |
| 179 |
multicolumn_targets = multicolumn_target_str.split(','); |
| 180 |
} |
| 181 |
else{ |
| 182 |
multicolumn_targets = []; |
| 183 |
} |
| 184 |
var multicolumn_th_targets = $.map(multicolumn_targets, function(identifier, i){ |
| 185 |
return get_th(sort_info.$table, identifier); |
| 186 |
}); |
| 187 |
|
| 188 |
table_structure.sort(function(e1, e2){ |
| 189 |
var multicolumns = multicolumn_th_targets.slice(0); |
| 190 |
var diff = sort_info.compare_fn(e1.columns[th_index], e2.columns[th_index]); |
| 191 |
while(diff === 0 && multicolumns.length){ |
| 192 |
var multicolumn = multicolumns[0]; |
| 193 |
var datatype = multicolumn.$e.data("sort"); |
| 194 |
var multiCloumnSortMethod = sort_info.$table.data('sortFns')[datatype]; |
| 195 |
diff = multiCloumnSortMethod(e1.columns[multicolumn.index], e2.columns[multicolumn.index]); |
| 196 |
multicolumns.shift(); |
| 197 |
} |
| 198 |
|
| 199 |
|
| 200 |
if (diff === 0) |
| 201 |
return e1.index - e2.index; |
| 202 |
else |
| 203 |
return diff; |
| 204 |
|
| 205 |
}); |
| 206 |
|
| 207 |
if (sort_info.sort_dir != $.fn.stupidtable.dir.ASC){ |
| 208 |
table_structure.reverse(); |
| 209 |
} |
| 210 |
return table_structure; |
| 211 |
}; |
| 212 |
|
| 213 |
var get_th = function($table, identifier){ |
| 214 |
|
| 215 |
var $table_ths = $table.find('th'); |
| 216 |
var index = parseInt(identifier, 10); |
| 217 |
var $th; |
| 218 |
if(!index && index !== 0){ |
| 219 |
$th = $table_ths.siblings('#' + identifier); |
| 220 |
index = $table_ths.index($th); |
| 221 |
} |
| 222 |
else{ |
| 223 |
$th = $table_ths.eq(index); |
| 224 |
} |
| 225 |
return {index: index, $e: $th}; |
| 226 |
}; |
| 227 |
|
| 228 |
var getTableRowsFromTableStructure = function(table_structure, sort_info){ |
| 229 |
|
| 230 |
var column = $.map(table_structure, function(ele, i){ |
| 231 |
return [[ele.columns[sort_info.th_index], ele.$tr, i]]; |
| 232 |
}); |
| 233 |
|
| 234 |
|
| 235 |
sort_info.column = column; |
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
return $.map(table_structure, function(ele) { return ele.$tr; }); |
| 240 |
|
| 241 |
}; |
| 242 |
|
| 243 |
var updateElementData = function(sort_info){ |
| 244 |
var $table = sort_info.$table; |
| 245 |
var $this_th = sort_info.$th; |
| 246 |
var sort_dir = $this_th.data('sort-dir'); |
| 247 |
var th_index = sort_info.th_index; |
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
$table.find("th").data("sort-dir", null).removeClass("sorting-desc sorting-asc"); |
| 252 |
$this_th.data("sort-dir", sort_dir).addClass("sorting-"+sort_dir); |
| 253 |
}; |
| 254 |
|
| 255 |
var calculateSortDir = function(force_direction, sort_info){ |
| 256 |
var sort_dir; |
| 257 |
var $this_th = sort_info.$th; |
| 258 |
var dir = $.fn.stupidtable.dir; |
| 259 |
|
| 260 |
if(!!force_direction){ |
| 261 |
sort_dir = force_direction; |
| 262 |
} |
| 263 |
else{ |
| 264 |
sort_dir = force_direction || $this_th.data("sort-default") || dir.ASC; |
| 265 |
if ($this_th.data("sort-dir")) |
| 266 |
sort_dir = $this_th.data("sort-dir") === dir.ASC ? dir.DESC : dir.ASC; |
| 267 |
} |
| 268 |
return sort_dir; |
| 269 |
}; |
| 270 |
|
| 271 |
var calculateTHIndex = function(sort_info){ |
| 272 |
var th_index = 0; |
| 273 |
var base_index = sort_info.$th.index(); |
| 274 |
sort_info.$th.parents("tr").find("th").slice(0, base_index).each(function() { |
| 275 |
var cols = $(this).attr("colspan") || 1; |
| 276 |
th_index += parseInt(cols,10); |
| 277 |
}); |
| 278 |
return th_index; |
| 279 |
}; |
| 280 |
|
| 281 |
})(jQuery); |