【问题标题】:JQuery DataTable get row index after sortingJQuery DataTable排序后获取行索引
【发布时间】:2017-08-28 17:49:15
【问题描述】:

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

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

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

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

有什么解决办法吗?

【问题讨论】:

  • 如果该行位于顶部,则其索引 0。如果您的应用逻辑无法处理该问题并且您需要原始索引 - 然后循环遍历第一行排序前的时间,并将当前索引存储到自定义属性中,以便以后可以从那里访问它。

标签: javascript jquery


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2016-10-12
    • 1970-01-01
    • 2022-06-22
    • 2011-06-19
    • 1970-01-01
    • 2014-09-27
    • 1970-01-01
    • 2016-11-30
    • 2014-11-06
    相关资源
    最近更新 更多