简体   繁体   中英

Get current row index from DataTables API

I want to use the DataTables API to get the CURRENT row index of a specific cell in my table. How do I do this properly?

When using the cell() selector to select a single cell, the row index returned is always the initial index. This seems to be the case with either the default selector-modifier or explicitly setting the order to current.

In this bin, Hope Fuentes is initially on row 37 but after the sort done in the table initialization, Hope Fuentes is on row 13. Index is always returned as 37 and not 13. I need 13, as current index is 13.

Seems like a bug.

data tables live jsbin http://live.datatables.net/saweyado/1/edit

The row index is also inaccurate if .index().row is chained to the original cell() method.

CAUSE

Although the name for API methods row().index() or cell().index() is somewhat misleading but jQuery DataTables uses row index as row identification method, it is being assigned initially and never changes. Many API methods use row index as the way to access rows and cells.

SOLUTION

Use the following code to convert row index used by DataTables to row index as shown on page:

var rowIndex = 9;
var rowIndexSorted = table.rows().eq(0).indexOf(rowIndex);

DEMO

See this jsFiddle for code and demonstration.

NOTES

There is a plugin that does what you want (jump to a page by searching for data from a column) without using row indexes, see page.jumpToData() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM