function sort(type, order) {
var table_tbody=document.getElementsByTagName("tbody")[0];
var table_tr = table_tbody.getElementsByTagName("tr");
var tempArr = [];
var temp_tr_arr = [];
var m = 0;
if (type === "id") {
m = 0;
} else if (type === "price") {
m = 1;
} else if (type === "sales") {
m = 2;
}
for (var i = 0; i < table_tr.length; i++) {
tempArr.push(table_tr[i].getElementsByTagName("td")[m].innerHTML);
temp_tr_arr.push(table_tr[i].cloneNode(true));
};
var tr_length = table_tr.length;
for (var x = 0; x < tr_length; x++) {
table_tbody.removeChild(table_tbody.getElementsByTagName("tr")[0]);
}
if (order === "asc") {
tempArr.sort(function(a, b) {
return a - b;
});
} else if (order === "desc") {
tempArr.sort(function(a, b) {
return b - a;
});
}
for (var k = 0; k < tempArr.length; k++) {
for (var vv = 0; vv < temp_tr_arr.length; vv++) {
if (tempArr[k] == temp_tr_arr[vv].getElementsByTagName("td")[m].innerHTML) {
table_tbody.appendChild(temp_tr_arr[vv]);
}
}
}
}
根据table的不同列排序
猜你喜欢
转载自blog.csdn.net/qq_30422457/article/details/81173661
今日推荐
周排行