[问题] 动态表格排序问题

楼主: IRS404 (LL)   2019-08-07 11:38:54
Question:
我想要做的动作是这样的:
从网络上下载json 填入表格 然后对表格其中一行Column做排序
javascript code:
$(function(){
$ajax({
type:'GET';
url:
datatype:'json';
headers: GetAuthorizationHeader(),
success: function (Data) {
for (var i = 0; i < Data.A.length; i++) {
var A1 =Data.A.B1;
var A3 =Data.A.B2;
var A4 =Data.A.B3;
var B1 =Data.A.B4;
var A5 =Data.A.B5;
var B2 =Data.A.B6;
$('tbody ').append('<tr><th>'+A1+' </th><th>'+A3+' </th><th>'+B1+'
</th><th>'+B2+' </th><th>'+A4+' </th><th>'+A5+'</th></tr>');
}
sortTable();
}
});
});
到这边基本上都没问题,json的资料都抓到表格当中了
这时我想对表格的column做排序,但是这个sortTable()方法似乎没有动作
没法将抓取json资料填入新生成的表格做排序,不知道是什么原因
我用的算法程式是这个
function sortTable() {
var table, rows, switching, i, x, y, shouldSwitch;
table = document.getElementById("table-column-toggle");
switching = true;
/*Make a loop that will continue until
no switching has been done:*/
while (switching) {
//start by saying: no switching is done:
switching = false;
rows = table.rows;
/*Loop through all table rows (except the
first, which contains table headers):*/
for (i = 1; i < (rows.length - 1); i++) {
//start by saying there should be no switching:
shouldSwitch = false;
/*Get the two elements you want to compare,
one from current row and one from the next:*/
x = rows[i].getElementsByTagName("th")[1];
y = rows[i + 1].getElementsByTagName("th")[1];
//check if the two rows should switch place:
if (x > y) {
//if so, mark as a switch and break the loop:
shouldSwitch = true;
break;
}
}
if (shouldSwitch) {
/*If a switch has been marked, make the switch
and mark that a switch has been done:*/
rows[i].parentNode.insertBefore(rows[i + 1], rows[i]);
switching = true;
}
}
}

Links booklink

Contact Us: admin [ a t ] ucptt.com