Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to insert row at end of table with HTMLTableElement.insertRow

Tags:

Background and Setup

I'm trying to create a table from tabular data in javascript, but when I try inserting rows into a table element, it inserts in the opposite order from what I expect and the opposite order of what you get from using appendChild. (Here's a jsfiddle with a live demo of this).

Say I have some data like this:

var data = [     {"a": 1, "b": 2, "c": 3},     {"a": 4, "b": 5, "c": 6},     {"a": 7, "b": 8, "c": 9} ], keys = ["a", "b", "c"]; 

If I try using the insertRow() and insertCell() methods for creating tables, iterating over the data and keys above. I get the reverse order of what I expect:

InsertRows pic

Generated by using the following code:

// insert a table into the DOM var insertTable = insertDiv.appendChild(document.createElement("table")),     thead = insertTable.createTHead(), // thead element     thRow = thead.insertRow(), // trow element     tbody = insertTable.createTBody(); // tbody element   thRow.insertCell().innerText = "#"; // add row header  // insert columns for (var i = 0, l = keys.length; i < l; i ++) {     var k = keys[i];     thRow.insertCell().innerText = k; }  // insert rows of data for (var i = 0, l = data.length; i < l; i ++) {     var rowData = data[i],         row = tbody.insertRow();     row.insertCell().innerText = i;     for (var j = 0, l = keys.length; j < l; j ++) {         var elem = rowData[keys[j]];         row.insertCell().innerText = elem;     } } 

Whereas, if I create a table using document.createElement and appendChild, I get the order I would expect:

AppendChild pic

Generated by:

// now do the same thing, but using appendChild var byChildTable = document.createElement("table"); byChildTable.setAttribute("class", "table"); thead = byChildTable.appendChild(document.createElement("thead")); thRow = thead.appendChild(document.createElement("tr"));  // create table header thRow.appendChild(document.createElement("td")).innerText = "#"; for (var i = 0, l = keys.length; i < l; i ++) {     var key = keys[i];     thRow.appendChild(document.createElement("td")).innerText = key; }  // insert table data by row tbody = byChildTable.appendChild(document.createElement("tbody")); for (var i = 0, l = data.length; i < l; i ++) {     var rowData = data[i],         row = tbody.appendChild(document.createElement("tr"));     row.appendChild(document.createElement("td")).innerText = i;     for (var j = 0, l = keys.length; j < l; j ++) {         var elem = rowData[keys[j]];         row.appendChild(document.createElement("td")).innerText = elem;     } } 

Question

How do I control the order it puts the elements in the DOM? Is there any way to change this? I guess otherwise I could iterate in reverse order, but even that way, you still can't insert with table.insertRow(). It's just a bummer because the insertRow() and insertCell() methods seem much clearer than table.appendChild(document.createElement("tr")) etc.

like image 203
Jeff Tratner Avatar asked Apr 07 '13 18:04

Jeff Tratner


People also ask

How do you add a row in the end of the table using JavaScript?

The insertRow() method creates an empty <tr> element and adds it to a table. The insertRow() method inserts the new row(s) at the specified index in the table. Note: A <tr> element must contain one or more <th> or <td> elements. Tip: Use the deleteRow() method to remove a row.

How do you add a row at the end of a table in HTML?

The HTMLTableElement. insertRow() method inserts a new row ( <tr> ) in a given <table> , and returns a reference to the new row. Note: insertRow() inserts the row directly into the table.

How do you insert 3 rows before the last row of a table?

Click in a cell above or below where you want to add a row. Under Table Tools, on the Layout tab, do one of the following: To add a row above the cell, click Insert Above in the Rows and Columns group. To add a row below the cell, click Insert Below in the Rows and Columns group.


1 Answers

The .insertRow and .insertCell methods take an index. In absence of the index, the default value is -1 in most browsers, however some browsers may use 0 or may throw an error, so it's best to provide this explicitly.

To to an append instead, just provide -1, and an append will be done

var row = table.insertRow(-1); var cell = row.insertCell(-1); 

https://developer.mozilla.org/en-US/docs/DOM/table.insertRow

like image 92
5 revs, 3 users 77%user1106925 Avatar answered Sep 22 '22 01:09

5 revs, 3 users 77%user1106925