New rows can be added to a DataTable very easily using the row.add()DT
API method. Simply call the API function
with the data that is to be used for the new row (be it an array or object). Multiple rows can be added
using the rows.add()DT
method (note the plural).
Note that in order to see the new row in the table you must call the draw()DT
method, which is easily done through the
chaining that the DataTables API makes use of.
This example shows a single row being added each time the button below is clicked upon.
Column 1 | Column 2 | Column 3 | Column 4 | Column 5 |
---|---|---|---|---|
Column 1 | Column 2 | Column 3 | Column 4 | Column 5 |
1.1 | 1.2 | 1.3 | 1.4 | 1.5 |
The Javascript shown below is used to initialise the table shown in this example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | $(document).ready(function() { var t = $('#example').DataTable(); var counter = 1; $('#addRow').on( 'click', function () { t.row.add( [ counter +'.1', counter +'.2', counter +'.3', counter +'.4', counter +'.5' ] ).draw(); counter++; } ); // Automatically add a first row of data $('#addRow').click(); } ); |
In addition to the above code, the following Javascript library files are loaded for use in this example: