简体   繁体   English

DataTables:在选定单元格之前获取单元格的textContent

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

I can get the selected cell textContent like this: 我可以像这样获得选定的单元格textContent:

this.textContent

but I want to get the textContent of cell before the selected cell. 但是我想在选定的单元格之前获取单元格的textContent。 How can I do that?. 我怎样才能做到这一点?。 I tried like this but it says undefined : 我试图这样,但它说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);
})

Use table.cell(rowIndex, colIndex).data() instead of table.cells(rowIndex, colIndex).textContent; 使用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);
});

Datatable check _DT_CellIndex attribute inside Jquery object of triggered element to determine column and row. 检查触发元素的Jquery对象内的数据表_DT_CellIndex属性以确定列和行。

We can simply modify _DT_CellIndex before load it with Datatable 我们可以在将_DT_CellIndex加载到Datatable之前简单地进行修改

Sample code: 样例代码:

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