简体   繁体   English

jQuery DataTable排序后获取行索引

[英]JQuery DataTable get row index after sorting

I have a table with data and a button that when clicked should do something with the row index. 我有一个带有数据的表和一个按钮,单击该按钮应对行索引进行操作。 I've done this like so: 我这样做是这样的:

 $("#tblData tBody").on('click', '.updateButton', function() {

       updateButtonRowIndex = $(this).closest('tr').prevAll().length;
       alert(updateButtonRowIndex);
    });

This works but when I apply sorting to one of the columns, it no longer takes the actual row number but restarts from 0. This means that if I sort on ID and click on the button for 182 (now at the top) it will show that the row index is 0 and it will draw a value in the wrong row (the actual row 0). 这有效,但是当我对其中一列进行排序时,它不再使用实际的行号,而是从0重新开始。这意味着,如果我对ID进行排序并单击182(现在位于顶部)的按钮,它将显示该行的索引为0,它将在错误的行(实际的行0)中绘制一个值。

Any solution for this? 有什么解决办法吗?

You need to store the value for the original row index, you can always use an attribute for that like this: 您需要存储原始行索引的值,您可以始终像这样使用属性:

$("#tblData tBody").on('click', '.updateButton', function() {
  if ($(this).closest('tr').attr('originalRowIndex')) {
    alert("This is the original value: "
      $(this).closest('tr').attr('originalRowIndex'));
  } else {
    updateButtonRowIndex = $(this).closest('tr').prevAll().length;
    $(this).closest('tr').attr('originalRowIndex', updateButtonRowIndex)
    alert(updateButtonRowIndex);
  }
});

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

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