如何在不使用TableTool的情况下从jquery数据表中获取选定的行索引

How to get selected row index from jquery datatables without using TableTool

本文关键字:数据表 获取 索引 jquery 情况下 TableTool      更新时间:2023-09-26

我需要在不使用TableTool的情况下从jquery数据表中获取用户选择的行的行id或索引。一旦我得到索引或Id,我将在用户返回同一页面后使用它们来选择这些行。如何获取所选行的行Id或索引?非常感谢!

JSP代码:

 // when a row is selected, I want to get the row id or index
 $('#userTable tbody tr').on('click', function() 
 {
       var oTable = $('#userTable').dataTable();
       var data = oTable.fnGetData(this);
       selectedRowId = data[4]; 
       alert(selectedRowId); // this printed "undefined"
       var rowIndex = oTable.row(this).index();
       alert(rowIndex);  // this alert didn't even get invoked.
  });
var rowIndex = oTable.row(this).index();

以上将工作,但你必须使用:

 var oTable = $('#userTable').DataTable();

它将返回API,并应允许您使用row(this).index()

代替:

 var oTable = $('#userTable').dataTable();

然而,由于没有看到代码的工作副本(可能是JSFiddle),我不确定fnGetData()为什么不工作。