首页 > 解决方案 > How to know index of DataTable row just added?

问题描述

I am using the DataTables jQuery plugin version 1.10.22.

I add a row to DataTable, like the following.

var table = $('#example').DataTable();

var data = { Id: 0, Name: 'Fred', Age: 42 };
table.row.add( data ).draw( false );

That works. A new row is added to the table, and the data I provided is displayed.

But, immediately after adding the row, I would like to know the row index of that row I just added. How do I determine that?

标签: javascriptjquerydatatables

解决方案


The row.add() function returns an instance of a DataTable object containing the new row (and only the new row):

var newRow = table.row.add( data ).draw( false );

So, now you can get the assigned index from that:

console.log(newRow.index());

This is the internal DataTables index of the row - not the visual/displayed position index in the HTML table. This value does not change as the table is sorted/filtered.


推荐阅读