微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何使用 jQuery 使用 column() 访问数据表中的列?

如何解决如何使用 jQuery 使用 column() 访问数据表中的列?

我在数据表中所做的是我有两个复选框,它们与我的列(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() - 并使用数组选择器指定您要选择的索引:[ 0,6 ]

(实际上,除了一个基本的索引整数和我的索引整数数组之外,还有很多这样的selector options可以在这里使用。)

这是我的代码版本:

$("#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 ] ) 函数返回一个包含 2 个数组的对象 - 每个选定的列一个。

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

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。