简体   繁体   English

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

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

What I did inside my dataTable is I have two checkboxes that are separated from my column(0) and to column(6), My code is working for checkbox to check all of my elements but the problem is I can only specify column(0).我在 dataTable 中所做的是我有两个复选框,它们与我的列(0)和列(6)分开,我的代码正在为复选框工作以检查我的所有元素,但问题是我只能指定列(0 )。 How can I access the column(6) inside my dataTables with checkbox on it?如何访问带有复选框的数据表中的列(6)?

Here is my code example.这是我的代码示例。

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

You can use columns() instead of column() - and use an array selector to specify the indexes you want to select: [ 0, 6 ] .您可以使用columns()而不是column() - 并使用数组选择器来指定您想要 select: [ 0, 6 ]的索引。

(In fact, there is a variety of such selector options which can be used here, in addition to a basic index integer, and my array of index integers.) (事实上,除了基本索引 integer 和我的索引整数数组之外,还有多种此类选择器选项可以在这里使用。)

Here is my version of your code:这是我的代码版本:

$("#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');
});

In the above code, the columns( [ 0, 6 ] ) function returns an object containing 2 arrays - one for each selected column.在上面的代码中, columns( [ 0, 6 ] ) function 返回一个 object 包含 2 个 arrays - 每个选定列一个。

So then we have to merge those into a single array: nodesObj[0].concat( nodesObj[1] ) .所以我们必须将它们合并到一个数组中: nodesObj[0].concat( nodesObj[1] )

After that, we can apply our jQuery find to the resulting array of jQuery objects.之后,我们可以将我们的 jQuery find应用于 jQuery 对象的结果数组。

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

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