簡體   English   中英

DataTables:在選定單元格之前獲取單元格的textContent

[英]DataTables: get textContent of cell before the selected cell

我可以像這樣獲得選定的單元格textContent:

this.textContent

但是我想在選定的單元格之前獲取單元格的textContent。 我怎樣才能做到這一點?。 我試圖這樣,但它說undefined

$('#dtBasicExample').on('click', 'tbody td', function() {

  var table = $('#dtBasicExample').DataTable();


  var colIndex = table.cell(this).index().column-1;//get previous column index
  var rowIndex = table.cell(this).index().row;//get row index

  var Text=table.cells(rowIndex, colIndex).textContent;//get textContent(not working)

    alert(Text);
})

使用table.cell(rowIndex, colIndex).data()代替table.cells(rowIndex, colIndex).textContent;

var table = $('#dtBasicExample').DataTable();

$('#dtBasicExample').on('click', 'tbody td', function () {

    var colIndex = table.cell(this).index().column - 1; //get previous column index
    var rowIndex = table.cell(this).index().row; //get row index

    // Use data() instead of textContent and cell instead of cells
    var text = table.cell(rowIndex, colIndex).data(); 

    alert(text);
});

檢查觸發元素的Jquery對象內的數據表_DT_CellIndex屬性以確定列和行。

我們可以在將_DT_CellIndex加載到Datatable之前簡單地進行修改

樣例代碼:

 $('#example').on('click', 'td', function() { var table = $(this).closest('table').DataTable(); $(this)[0]._DT_CellIndex.column-=1; alert(table.cell($(this)).data()); }); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script src="https://cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js"></script> <link href="https://cdn.datatables.net/1.10.19/css/jquery.dataTables.min.css" rel="stylesheet" /> <table id="example" class="display" style="width:100%"> <thead> <tr> <th>Name</th> <th>Position</th> <th>Office</th> <th>Numero</th> <th>Start date</th> <th>Salary</th> </tr> </thead> <tbody> <tr> <td>Tiger Nixon</td> <td>System Architect</td> <td>Edinburgh</td> <td>155555</td> <td>2011/04/25</td> <td>$320,800</td> </tr> <tr> <td>Garrett Winters</td> <td>Accountant</td> <td>Tokyo</td> <td>63</td> <td>2011/07/25</td> <td>$170,750</td> </tr> <tr> <td>Ashton Cox</td> <td>Junior Technical Author</td> <td>San Francisco</td> <td>1</td> <td>2009/01/12</td> <td>$86,000</td> </tr> </tbody> <tfoot> <tr> <td>Ashton Cox</td> <td>Junior Technical Author</td> <td>San Francisco</td> <td>1</td> <td>2009/01/12</td> <td>$86,000</td> </tr> </tfoot> </table> 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM