繁体   English   中英

jQuery DataTable排序后获取行索引

[英]JQuery DataTable get row index after sorting

我有一个带有数据的表和一个按钮,单击该按钮应对行索引进行操作。 我这样做是这样的:

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

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

这有效,但是当我对其中一列进行排序时,它不再使用实际的行号,而是从0重新开始。这意味着,如果我对ID进行排序并单击182(现在位于顶部)的按钮,它将显示该行的索引为0,它将在错误的行(实际的行0)中绘制一个值。

有什么解决办法吗?

您需要存储原始行索引的值,您可以始终像这样使用属性:

$("#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