Reputation: 79
I have table, and I need order (sort) th columns by available indexes (only VanilaJS not jQuery). How I can do it ? Please see examples http://jsfiddle.net/mcqueen/AXF2Y/3/.
Upvotes: 1
Views: 474
Reputation: 48761
I would do it like this:
var positions = [1, 4, 2, 0, 5, 3],
tr = document.getElementById('table-id').tHead.rows[0],
cells = [].slice.call(tr.cells);
positions.forEach(function(pos, i) {
tr.insertBefore(cells[pos], tr.cells[i]);
});
Helps to make an Array out of the NodeList since a NodeList updates as you change the DOM.
Upvotes: 1