簡體   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