0

I am fetching a table using jquery in the following way:

var table = $.fn.dataTable.fnTables(true);

I want to append a row to the table after the last row, I tried:

$("tr", table).after('<tr>...</tr>');

It appends after the first row and not last.

4
  • stackoverflow.com/questions/14846506/… Commented Jun 20, 2017 at 23:15
  • you could do $("tr", table).last().after('<tr>...</tr>'); Commented Jun 20, 2017 at 23:20
  • Must be something specific to datatables: normal table works as expected: jsfiddle.net/ej7bsoz6 (adds it after every row) Commented Jun 20, 2017 at 23:40
  • Can you create an minimal reproducible example? Maybe in jsfiddle? I tried briefly, but I don't use datatables much and it gives 'cannot read mData of undefined' Commented Jun 20, 2017 at 23:50

2 Answers 2

2

so it looks like your are using jquery datatables. so I would turn ordering off initially

var table = $('#example').DataTable({
aaSorting: []
});

then use the add row api for the datatable. to add the row to the bottom.

run the fiddle below. scroll to bottom and click add row.

var table = $('#example').DataTable({
aaSorting: []
});

$('#addRowBtn').click(function(event) {
  table.row.add(
   [
    "Ashton Andrew",
      "System Architect",
      "Tokyo",
      "45",
      "2011/04/25",
      "$56,000"
    ]
  ).draw();
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdn.datatables.net/1.10.15/js/jquery.dataTables.min.js"></script>
<link href="https://cdn.datatables.net/1.10.15/css/jquery.dataTables.min.css" rel="stylesheet"/>

<table id="example" class="display" cellspacing="0" width="100%">
  <thead>
    <tr>
      <th>Name</th>
      <th>Position</th>
      <th>Office</th>
      <th>Age</th>
      <th>Start date</th>
      <th>Salary</th>
    </tr>
  </thead>
  <tfoot>
    <tr>
      <th>Name</th>
      <th>Position</th>
      <th>Office</th>
      <th>Age</th>
      <th>Start date</th>
      <th>Salary</th>
    </tr>
  </tfoot>
  <tbody>
 
    <tr>
      <td>Garrett Winters</td>
      <td>Accountant</td>
      <td>Tokyo</td>
      <td>63</td>
      <td>2011/07/25</td>
      <td>$170,750</td>
    </tr>
    <tr>
      <td>Ashton Cox</td>
      <td>Junior Technical Author</td>
      <td>San Francisco</td>
      <td>66</td>
      <td>2009/01/12</td>
      <td>$86,000</td>
    </tr>
    <tr>
      <td>Cedric Kelly</td>
      <td>Senior Javascript Developer</td>
      <td>Edinburgh</td>
      <td>22</td>
      <td>2012/03/29</td>
      <td>$433,060</td>
    </tr>
    <tr>
      <td>Airi Satou</td>
      <td>Accountant</td>
      <td>Tokyo</td>
      <td>33</td>
      <td>2008/11/28</td>
      <td>$162,700</td>
    </tr>

  </tbody>
</table>

<button id="addRowBtn">
  add row

Sign up to request clarification or add additional context in comments.

5 Comments

why have you disabled sorting initially? And it doesnt look like you have enabled it again, but it works :o
if you don't turn off sorting initially it will default to sort on the first column. so inserting will follow the sorting rule. but I wanted to keep it so that if a user clicked one of the columns it would still sort. so that is why I turned it off initially.
Thanks. Will try it out. Could you help me out on this stackoverflow.com/questions/44665354/…
I get an error saying cannot reinitialize table. I need to pass no arguments to retreive the table.
bro living in 2034. thanks gpt gave wrong answer earlier
1

From the jQuery .append() documentation,

The .append() method inserts the specified content as the last child of each element in the jQuery collection.

Selecting the table and then calling .append('<tr>...</tr>'); should do what you want in this case.

2 Comments

It's appending to after my first row which is a row with the titles. Is it possible to append after last row? the variable table points to first row
its actually inserting in my thead

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.