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.

freedomn-m
  • 27,664
  • 8
  • 35
  • 57
user1692342
  • 5,007
  • 11
  • 69
  • 128

2 Answers2

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
Bryan Dellinger
  • 4,724
  • 7
  • 33
  • 79
  • why have you disabled sorting initially? And it doesnt look like you have enabled it again, but it works :o – user1692342 Jun 21 '17 at 01:07
  • 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. – Bryan Dellinger Jun 21 '17 at 01:09
  • Thanks. Will try it out. Could you help me out on this https://stackoverflow.com/questions/44665354/sorting-newly-added-column-values-in-datatable-not-working – user1692342 Jun 21 '17 at 01:16
  • I get an error saying cannot reinitialize table. I need to pass no arguments to retreive the table. – user1692342 Jun 21 '17 at 01:22
  • bro living in 2034. thanks gpt gave wrong answer earlier – Pablo Jun 09 '23 at 13:25
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.

J. Chen
  • 367
  • 1
  • 7