/**
*table:传入的表格
*n:按tr中的第几个索引进行排序
*comparator:排序规则,不传按照文本默认的规则进行排序
*/
function sortrows(table, n, comparator) {
var tbody = table.tBodies[0];
console.log(tbody);
var rows = tbody.rows;
rows = Array.prototype.slice.call(rows,0);
rows.sort(function (row1, row2) {
var cell1 = row1.cells[n],
cell2 = row2.cells[n],
val1 = cell1.textContent || cell1.innerText,
val2 = cell2.textContent || cell2.innerText;
console.log(cell1);
if(comparator){
return comparator(val1,val2);
}
if(val1 < val2)return -1;
else if(val1>val2)return 1;
else return 0;
});
rows.forEach(function (v) {
tbody.appendChild(v);
});
}
/**
*table:传入的表格
*/
function makeSortable(table) {
var ths = table.tHead.rows[0].cells;
ths = Array.prototype.slice.call(ths,0);
ths.forEach(function (v,k) {
v.onclick = function () {
alert(k);
sortrows(table,k);
}
});
}
js 表格排序
最新推荐文章于 2025-06-19 09:11:18 发布