Reputation: 683
I'm using DataTables to display my data. I'm using fnAddData to add new data to row. This function inserts new row at the last of table. What I want is when I'm doing add new row, it become first row on the table. How can I do this?
This is my code:
$('#example').dataTable().fnAddData( [
caption,
grade,
"<span class='edit'>Edit</span><span class='delete'>Delete</span>",
id,
kind,
oTable.fnGetData().length ]
);
Upvotes: 0
Views: 7733
Reputation: 3621
if is set "bSort": false, you can do something like this after your code
var my_array = $('#table').dataTable().fnGetNodes( );
var last_element = my_array[my_array.length - 1];
console.log(last_element);
$(last_element).insertBefore($('#table tbody tr:first-child'));
Upvotes: 2
Reputation: 401
If sorting is enabled then it will show up where the sort put its. So you would have to sort by the column that would bring it to the top.
If sorting is not enabled then it would be almost like: How to move a <tr> up or down within <table> via jQuery?
You could try the following if sorting is not enabled, I'm not sure this will work with the plugin, and you would be better off not using DataTables.net:
var oTable = $('#example').dataTable()
function MoveRowDown(tableId, index)
{
var rows = oTable.$("tr");
rows.eq(index).insertAfter(rows.eq(index + 1));
}
function MoveRowUp(tableId, index)
{
var rows = oTable.$("tr");
rows.eq(index).insertBefore(rows.eq(index - 1));
}
Upvotes: 0