简体   繁体   English

显示表格从JavaScript中的最后一个数组值开始[]?

[英]Displaying table start from last array values[] in javascript?

I want to sort table rows of the value array [1][2][3]..etc to be ..[3][2][1] 我想将值数组[1][2][3]..etc表行排序为..[3][2][1] how to do it? 怎么做?

Below I attach my code please help to fix it. 下面我附上我的代码,请帮助修复它。

 function showtbl() { var img = "<img src='logo/" var imgt = ".png'/>" var li = "<br /> Link: <a href='?apps=" var lin = "'>http://playstore.co.id/?apps=" var link = "</a><br /> Terbit: " var b = "<button type='button' class='btn btn-danger' onclick='tampilss(&quot;" var bt = "&quot;);'>Screenshoot</button><br><button type='button' class='btn btn-success' onclick='tampilqr(&quot;" var btn = "&quot;);'>Download</button>" var A1 = "ss-clothes" var A2 = "korselindo" var A3 = "real-hiphop-shop" var values = new Array(3); values[1] = [img + A1 + imgt, A1 + li + A1 + lin + A1 + link, b + A1 + bt + A1 + btn]; values[2] = [img + A2 + imgt, A2 + li + A2 + lin + A2 + link, b + A2 + bt + A2 + btn]; values[3] = [img + A3 + imgt, A3 + li + A3 + lin + A3 + link, b + A3 + bt + A3 + btn]; values.reverse(); // I has added this but values[3] is losing var myTable = document.getElementById("myTable"); // IE7 only supports appending rows to tbody var tbody = document.createElement("tbody"); // for each outer array row for (var i = 1; i < values.length; i++) { var tr = document.createElement("tr"); // for each inner array cell // create td then text, append for (var j = 0; j < values[i].length; j++) { var td = document.createElement("td"); var txt = document.createElement("span"); txt.innerHTML = values[i][j]; td.appendChild(txt); tr.appendChild(td); } // append row to table // IE7 requires append row to tbody, append tbody to table tbody.appendChild(tr); myTable.appendChild(tbody); } } showtbl(); 
 <table class="table table-striped" id="myTable"> <thead> <tr> <th>Logo</th> <th>Nama App</th> <th>Tindakan</th> </tr> </thead> </table> 

Above I attach my code please help to fix it. 在上面我附加了我的代码,请帮助修复它。

I want to sort table rows of the value array [1][2][3]..etc to be ..[3][2][1] 我想将值数组[1][2][3]..etc表行排序为..[3][2][1] how to do it? 怎么做?

You can either use Array.prototype.reverse 您可以使用Array.prototype.reverse

var values = new Array(3);
values[1] = [img + A1 + imgt, A1 + li + A1 + lin + A1 + link, b + A1 + bt + A1 + btn];
values[2] = [img + A2 + imgt, A2 + li + A2 + lin + A2 + link, b + A2 + bt + A2 + btn];
values[3] = [img + A3 + imgt, A3 + li + A3 + lin + A3 + link, b + A3 + bt + A3 + btn];
values.reverse();

Or you can modify the for loop so you loop through it in reverse 或者,您可以修改for循环,以便反向循环

for (var i = values.length - 1; i > 0; i--) {
    var tr = document.createElement("tr");

    // for each inner array cell
    // create td then text, append
    for (var j = 0; j < values[i].length; j++) {
        var td = document.createElement("td");
        var txt = document.createElement("span");
        txt.innerHTML = values[i][j];
        td.appendChild(txt);
        tr.appendChild(td);
    }

    // append row to table
    // IE7 requires append row to tbody, append tbody to table
    tbody.appendChild(tr);
    myTable.appendChild(tbody);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM