how to add a new row in top of a json datatable with already existing records


Put new rows at the specific position of the jQuery datatable


I believe you need to use jQuery $.when function to execute callback when both Ajax calls were successful.

That way you get data always in the same order and there is no need to write functions to insert data at specific position.

Also, if needed, you can manipulate the final data before initializing the data table with it. For example, shown below are just two options, the possibilities are limitless.

To append data from call2 to data from call1:

var data = a1[0].data.concat(a2[0].data);

To insert data from call2 at position 2 of data from call1 (source):

var data = a1[0].data;
data.splice.apply(data, [2, 0].concat(a2[0].data));


See example below for code and demonstration.


I am trying to add a row in top of a datatable which shows sum of a column "amount".I am using json datatable(jquery plugin) list to display datatable. How can i add this row in top of datatable with existing rows?