简体   繁体   English

从DataTables API获取当前行索引

[英]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. 我想使用DataTables API来获取表中特定单元格的CURRENT行索引。 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. 当使用cell()选择器选择单个单元格时,返回的行索引始终是初始索引。 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. 在此bin中,Hope Fuentes最初位于第37行,但在表初始化中完成排序之后,Hope Fuentes位于第13行。索引始终返回37,而不是13。我需要13,因为当前索引是13。

Seems like a bug. 好像是个错误。

data tables live jsbin http://live.datatables.net/saweyado/1/edit 数据表实时jsbin http://live.datatables.net/saweyado/1/edit

The row index is also inaccurate if .index().row is chained to the original cell() method. 如果.index()。row链接到原始cell()方法,则行索引也不准确。

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. 尽管API方法row().index()cell().index()名称有些令人误解,但jQuery DataTables使用行索引作为行标识方法,但它最初分配时从未更改。 Many API methods use row index as the way to access rows and cells. 许多API方法都使用行索引作为访问行和单元格的方式。

SOLUTION

Use the following code to convert row index used by DataTables to row index as shown on page: 使用以下代码将DataTables使用的行索引转换为行索引,如页面所示:

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

DEMO 演示

See this jsFiddle for code and demonstration. 有关代码和演示,请参见此jsFiddle

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() . 有一个插件可以执行您想要的操作(通过从列中搜索数据跳转到页面),而无需使用行索引,请参阅page.jumpToData()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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