簡體   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