Alex
Alex

Reputation: 79

InsertBefore sort row in table

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

Answers (1)

I Hate Lazy
I Hate Lazy

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]);
});

http://jsfiddle.net/ybr6E/

Helps to make an Array out of the NodeList since a NodeList updates as you change the DOM.

Upvotes: 1

Related Questions