I want to sort table rows of the value array [1][2][3]..etc
to be ..[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("" var bt = "");'>Screenshoot</button><br><button type='button' class='btn btn-success' onclick='tampilqr("" var btn = "");'>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]
how to do it?
You can either use 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);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.