简体   繁体   English

列排序后如何从jquery dataTable获取正确的行数据?

[英]How to get correct row data from jquery dataTable after column sorting?

I am using jquery dataTable for displaying customer records.我正在使用 jquery dataTable 来显示客户记录。 First time i go to customer page,i have 20 customer records in dataTable sorted by customerId column( so they are diplayed starting from 20 to 1 where 20 is the id of 20th customer and 1 is the id of first customer) in desc order.我第一次进入客户页面时,我在 dataTable 中有 20 条客户记录,按 customerId 列排序(因此它们从 20 到 1 开始显示,其中 20 是第 20 个客户的 id,1 是第一个客户的 id)以降序排列。 20th customer is shown at top in dataTable.第 20 个客户显示在数据表的顶部。

now if do below in my javascript code现在,如果在我的 javascript 代码中执行以下操作

 var customerTable=$('#customer').dataTable();// where customer is the html div associated with dataTable

 var custData = customerTable.fnGetData(18)// it gives me data of 19 row in dataTable( as dataTable index starts from 0)
 // so i get the data of customer having customer id=19

Perfectly fine till here.到这里都很好。

Now i click the sorting icon on customerId column .现在我单击 customerId 列上的排序图标。 So after sorting customer with id as 1 is displayed on top in dataTable and customer with id as 20 will be displayed at bottom.所以在排序后,id为1的客户会显示在dataTable的顶部,而id为20的客户将显示在底部。 Now i do the my javascript operation again现在我再次执行我的 javascript 操作

var customerTable=$('#customer').dataTable();

 var custData = customerTable.fnGetData(18)
 // it should give me data for customer id=2 Right? but still i am getting the old data i.e customer Data having id=19.

Why i am not getting the right data after sorting?为什么排序后没有得到正确的数据? how should i get the correct data with row id after dataTable sorting?在dataTable排序后,我应该如何获得带有行ID的正确数据?

I tried same thing with little change.我尝试了同样的事情,几乎没有什么变化。

First created global variable to hold the table.首先创建全局变量来保存表。

var customerTable;

Then initiated the object in onready state.然后将对象初始化为 onready 状态。

$(document).ready(function() {
        customerTable = $('#customer').dataTable();
});

If you sort and get the data now.如果您现在排序并获取数据。 You probably get what you want.你可能会得到你想要的。

var custData = customerTable.fnGetData(18);

Only thing I aware of is I didn't initiate the table object after sorting.我唯一知道的是我没有在排序后启动 table 对象。

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

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