【问题标题】:How to get data-value from selected row datatables如何从选定的行数据表中获取数据值
【发布时间】:2020-03-29 11:48:57
【问题描述】:

我正在使用 jQuery Datatables 使用复选框和选择扩展进行行选择。

我需要从复选框行中获取数据值。我使用了这段代码,但我无法获取数据:

var rows_selected = table.column(0).checkboxes.selected();
$.each(rows_selected, function (index, rowId) {
    var data = table.row(index).data();
    fed_id = data['federation_id'];
    console.log(fed_id);
});

这是表格的html代码:

<table id="customerTable" class="table color-table info-table table-striped display dataTable no-footer dt-checkboxes-select" role="grid" aria-describedby="customerTable_info" style="width: 961px;">
<thead>
<tr role="row">
    <th class="dt-checkboxes-cell dt-checkboxes-select-all sorting_disabled" tabindex="0" aria-controls="customerTable" data-col="0" aria-label="">
        <input type="checkbox"></th>
    <th>Cognome</th>
    <th>Nome</th>
    <th>Codice Fiscale</th>
    <th>Federazione/Ente</th>
    <th>Codice tessera</th>
    <th>Data inizio</th>
    <th>Data fine</th>
    <th>Stato</th>
    <th></th>
</tr>
</thead>
<tbody>
<tr id="2554" data-federation_id="79" role="row" class="odd">
    <td class=" dt-checkboxes-cell"><input type="checkbox" class="dt-checkboxes"></td>
    <td class="sorting_1">Mario</td>
    <td>Rossi</td>
    <td>ddddddddd</td>
    <td>ddddddddd</td>
    <td></td>
    <td></td>
    <td></td>
    <td><span class="label label-info">DA INVIARE</span></td>
    <td>&nbsp;</td>
</tr>
<tr id="2558" data-federation_id="24" role="row" class="even">
    <td class=" dt-checkboxes-cell"><input type="checkbox" class="dt-checkboxes"></td>
    <td class="sorting_1">Mario</td>
    <td>Verdi</td>
    <td>ddddddddd</td>
    <td>ddddddddd</td>
    <td></td>
    <td></td>
    <td></td>
    <td><span class="label label-info">DA INVIARE</span></td>
    <td>&nbsp;</td>
</tr>

</tbody>
</table>

【问题讨论】:

  • 您在 devtools 控制台上看到任何错误吗?
  • 一个小点:您的‘federation_id’,如上所示,使用了弯撇号。它应该使用标准(键盘)撇号:'federation_id'
  • @andrewjames 只是打字错误,谢谢,我会改正的

标签: jquery datatables yajra-datatable


【解决方案1】:

您可以使用以下 jQuery 获取选定复选框的联合 ID:

var checked_rows = $('input.dt-checkboxes:checkbox:checked').parents("tr");
$.each(checked_rows, function (key, val) {
  console.log($(this).attr('data-federation_id'));
});

第一行获取所有选中的复选框,然后找到这些复选框的&lt;tr&gt; 父级。

然后代码遍历找到的 &lt;tr&gt; 元素以提取每个 ID。

请注意,此 jQuery 不使用任何 DataTable 函数,并且独立于 DataTables 提供的功能。如果您只想获取 ID,那么这应该没问题。如果您想更改数据表的内容/行为,那么这可能无济于事。

【讨论】:

  • 感谢您的帮助,我会为数据表找到一些解决方案,如果找不到,我会使用您的!
猜你喜欢
  • 1970-01-01
  • 2016-11-25
  • 2016-05-14
  • 1970-01-01
  • 1970-01-01
  • 2012-08-09
  • 2017-11-29
  • 2019-07-05
  • 1970-01-01
相关资源
最近更新 更多