[問題] 動態表格排序問題

作者: 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