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

时间:2015-09-22 18:20:33

标签: javascript arrays html5 datatable

我想将值数组[1][2][3]..etc的表行排序为..[3][2][1]怎么做?

下面我附上我的代码,请帮助修复它。



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();
&#13;
<table class="table table-striped" id="myTable">
        <thead>
            <tr>
                <th>Logo</th>
                <th>Nama App</th>
                <th>Tindakan</th>
            </tr>
        </thead>
      </table>
&#13;
&#13;
&#13;

上面我附上我的代码,请帮助修复它。

我想将值数组[1][2][3]..etc的表行排序为..[3][2][1]怎么做?

1 个答案:

答案 0 :(得分:0)

您可以使用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 (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);
}