繁体   English   中英

如何使用 jQuery 使用 column() 访问 dataTables 中的列?

[英]How to access column in dataTables by using column() using jQuery?

我在 dataTable 中所做的是我有两个复选框,它们与我的列(0)和列(6)分开,我的代码正在为复选框工作以检查我的所有元素,但问题是我只能指定列(0 )。 如何访问带有复选框的数据表中的列(6)?

这是我的代码示例。

 $("#select_all").on('click', function() { $('#dataTables').DataTable().column(0)<<<<<<<<<<<<<<<<<<< this is my problem, it only specifies column(0).nodes().to$().find('input[type="checkbox"]:enabled:visible').prop('checked', this.checked); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="row"> <div class="col-lg-12"> <div class="panel panel-default"> <div class="panel-heading"> Emailed Data for approval </div> <.-- /?panel-heading --> <div class="panel-body"> <div class="table-responsive"> <table width="100%" class="table table-striped table-bordered table-hover" id="dataTables"> <thead> <tr> <th> <center> Select Data <input type="checkbox" id="select_all"> </center> </th> <th> Control Number </th> <th> Tools Specification </th> <th> Supplier </th> <th> <center> Status </center> </th> <th> <center> Reason for transfer </center> </th> <th class="hide_me"> <center> #### <input name="chk_id[]" type="checkbox" class="subCheckbox" value="<;php echo $row['tools_req_id']??>"> </center> </th> </tr> </thead> <td> <center> <input name="chk_status[]" class="is_checked_status" type="checkbox" value="<;php echo $row['req_stats']??>"> </center> </td> <td> <center> <label data-id="<;php echo $row['ID']??>" class="statusButton <?php echo ($row['req_stats']): 'label-success'? 'label-danger'?>"> </label> </center> </td> <td> <center> <p> </p> </center> </td> </tbody> </table> </div> </div> </div> </div> </div>

您可以使用columns()而不是column() - 并使用数组选择器来指定您想要 select: [ 0, 6 ]的索引。

(事实上,除了基本索引 integer 和我的索引整数数组之外,还有多种此类选择器选项可以在这里使用。)

这是我的代码版本:

$("#select_all").on('click', function() {
  var nodesObj = $('#dataTables').DataTable().columns( [ 0, 6 ] ).nodes().to$();
  var nodesArray = nodesObj[0].concat( nodesObj[1] );
  $(nodesArray).find('input[type="checkbox"]:enabled:visible').prop('checked', 'true');
});

在上面的代码中, columns( [ 0, 6 ] ) function 返回一个 object 包含 2 个 arrays - 每个选定列一个。

所以我们必须将它们合并到一个数组中: nodesObj[0].concat( nodesObj[1] )

之后,我们可以将我们的 jQuery find应用于 jQuery 对象的结果数组。

暂无
暂无

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

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