简体   繁体   English

DataTables-从单元格数据中的跨度获取ID

[英]DataTables - Get ID from span in Cell-Data

I have a DataTable and in this table is a cell, which contains multiple span -Elements, like this one: 我有一个DataTable ,在此表中是一个单元格,其中包含多个span -Elements,如下所示:

<span class="btn btn-xs btn-info" data-room="1910" data-id="1" disabled>
  <strong>unimportant description</strong>
</span>

I've just put the content of the cell into a variable: 我只是将单元格的内容放入变量中:

var extras = roomstable.cell(closestRow,3).data();

How can I filter out the IDs in the data-id -Tags to put it into an Array? 如何过滤掉data-id -Tags中的ID并将其放入数组中?

I can provide the solution with jquery. 我可以用jquery提供解决方案。

<script src="https://code.jquery.com/jquery-3.3.1.js"></script>

As argument you should define your cell, in your case it would be extras . 作为参数,您应该定义您的单元格,在您的情况下将是extras Try to give yours roomstable.cell(closestRow,3) . 尝试给您的roomstable.cell(closestRow,3) In my case was $('#dt').find('tr').eq(1).find('td').eq(0) 在我的情况下是$('#dt').find('tr').eq(1).find('td').eq(0)

 $(function(){ var table = $('#dt').DataTable(); arr_ids($('#dt').find('tr').eq(1).find('td').eq(0)); }) function arr_ids(celll) { var ar = []; celll.find('span').each(function(){ if ($(this).data('id')) { ar.push($(this).data('id')); } }); alert(ar); } 
 <script src="https://code.jquery.com/jquery-3.3.1.js"></script> <script src="https://cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js"></script> <script src="https://cdn.datatables.net/1.10.19/js/dataTables.bootstrap4.min.js"></script> <table id="dt"> <thead> <th>aaaaaaaaaaaaaaaaaaaaaa&nbsp;&nbsp;</th> <th>bbbbbbbbbbbbbbbbbbbbbbb&nbsp;&nbsp;</th> <th>ccccccccccccccccccccc&nbsp;&nbsp;</th> </thead> <tbody> <tr> <td> <span class="btn btn-xs btn-info" data-room="1910" data-id="1" disabled> <strong>unimportant description</strong> </span> <span class="btn btn-xs btn-info" data-room="1945" data-id="13" disabled> <strong>unimportant description</strong> </span> </td> <td> </td> <td> </td> </tr> </tbody> <tfoot> <th>aaaaaaaaaaaaaaaaaaaaaa&nbsp;&nbsp;</th> <th>bbbbbbbbbbbbbbbbbbbbbbb&nbsp;&nbsp;</th> <th>ccccccccccccccccccccc</th> </tfoot> </table> 

If the idea is to extract multiple buttons id's within a cell into an array, you would rather need to employ cell().node() method instead of cell().data() . 如果想法是将一个单元格中的多个按钮id提取到数组中,则需要使用cell().node()方法而不是cell().data()

That would let you grab all the buttons, turn that set toArray() and extract data-id attribute values: 这样一来,您就可以抓住所有按钮,将其设置为toArray()并提取data-id属性值:

var extras = $(roomstable.cell(closestRow,3).node())
   .find('span.btn')
   .toArray()
   .map(span => $(span).attr('data-id'));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM