dataTables contains its rows in an indexed array, and there are no API methods to add a new row to a specific index or change the index() row.
This really makes sense, since a typical dataTable is always sorted / ordered or filtered by data, not a static index. And when you get data from the server or want to transfer data to the server, you never use the static index() client.
But if you think about it, you can still order the rows and thereby insert a row with a specific index very simply using code, simply reordering the data. When a new row is added, replace the data from the last row (inserted row) with the second last row, then change the data from the second last row to the third last row and so on until you reach the index where you want to insert the row.
[0][1][2][3][4->][<-newRow] [0][1][2][3->][<-newRow][4] [0][1][2->][<-newRow][3][4]
Example: inserting a new row into the index on which the mouse is clicked:
$("#example").on('click', 'tbody tr', function() { var currentPage = table.page();
demo β http://jsfiddle.net/mLh08nyg/
source share