繁体   English   中英

如何使用 Jquery / DataTables 根据单击的元素获取表的列名

[英]how to get column name of a table based on clicked element using Jquery / DataTables

我有一个 Jquery数据表,在页脚我添加了一个引导选择来过滤我的数据。

Inside the dropdown select, I added a button ' Clear filter ' When there is/are option(s) selected.

当我单击该按钮时,我需要获取 header 名称。

因此,在我的示例中,如果我单击“ Position ”列中的“清除过滤器”按钮,我应该提醒“ Position ”。 但它始终显示最后一列名称。

在此处输入图像描述

任何建议请我在我的代码中缺少什么? 我添加了对我所做步骤的详细说明。 非常感谢。

 $(document).ready(function() { var table = $('#example').DataTable({ searching: false, info: false, paging: false, initComplete: function() { this.api().columns().every(function() { //add select to each column footer var column = this; var select = $('<select class="form-control show-tick" data-container="body" data-header="Select option(s)" data-actions-box="true" data-live-search="true" title="All" data-selected-text-format="count > 0" multiple></select>').appendTo($(column.footer()).empty()); // populate the select options with unique values of current column column.data().unique().sort().each(function(d, j) { select.append('<option value="' + d + '">' + d + '</option>'); }); }); //apply bootstrap selectpicker $("select").selectpicker(); //add 'clear filter' button below the search box in the dropdown select var buttonPosition = $('.bs-searchbox'); $('<div class="text-center"><button type="button" class="btn btn-sm btn-light clearButton">Clear filter</button></div>').appendTo(buttonPosition); // $(".clearButton").on("click", function(){ //check the closest <th> in the footer to the clicked button var tableFoot =$(this).closest('tfoot').find('th'); //console.log(tableFoot); //we know the position of the button in which <th> on the footer and from it we will check the column name on header alert('Column:'+$('table thead tr th').eq(tableFoot.index()).html().trim()); }); } }); });
 <link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/css/bootstrap.css" rel="stylesheet"> <link href="https://cdn.datatables.net/1.10.21/css/dataTables.bootstrap4.min.css" rel="stylesheet"> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.13.17/css/bootstrap-select.min.css"> <script src="https://code.jquery.com/jquery-3.5.1.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.1/umd/popper.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/js/bootstrap.min.js"></script> <script src="https://cdn.datatables.net/1.10.21/js/jquery.dataTables.min.js"></script> <script src="https://cdn.datatables.net/1.10.21/js/dataTables.bootstrap4.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.13.17/js/bootstrap-select.min.js"></script> <table id="example" class="table table-bordered table-hover nowrap" style="width:100%"> <thead> <tr> <th>Name</th> <th>Position</th> <th>Office</th> </tr> </thead> <tbody> <tr> <td>Ashton Cox</td> <td>Junior Technical Author</td> <td>San Francisco</td> </tr> <tr> <td>Cedric Kelly</td> <td>Senior Javascript Developer</td> <td>Edinburgh</td> </tr> <tr> <td>Airi Satou</td> <td>Accountant</td> <td>Tokyo</td> </tr> </tbody> <tfoot> <tr> <th>Name</th> <th>Position</th> <th>Office</th> </tr> </tfoot> </table>

问题是由于 Bootstrap select 不是它显示的表格的一部分,而是连接到 HTML 主体并绝对定位在表格单元格上。

有一个配置选项container: string | false container: string | false可用于.selectpicker()调用,因此您可以做的是在列上设置 ID 并设置选项以在此元素中呈现。

    let cf = $(column.footer()).empty();
    var select = $('<select class="form-control show-tick" data-container="body" data-header="Select option(s)" data-actions-box="true" data-live-search="true" title="All" data-selected-text-format="count > 0" multiple></select>')
      .appendTo(cf).selectpicker({container: '#ci'+ci});
      cf.attr('id',  'ci' + ci).data('col', ci);

在你清晰的例行程序中:

  $(".clearButton").on("click", function() {
    var col = $(this).closest('th').data('col');
    alert('Column:' + $('table thead tr th').eq(col).html().trim());
  });

摆弄这个解决方案。


另一种选择是创建 object 并引用循环中的当前列索引,并显示 select 通过show.bs.select事件设置列索引:

    let obj= { ci: ci};
    var select = $('<select id="ci' + ci + '" class="form-control show-tick" data-container="body" data-header="Select option(s)" data-actions-box="true" data-live-search="true" title="All" data-selected-text-format="count > 0" multiple></select>')
      .appendTo($(column.footer()).empty()).selectpicker().on('show.bs.select',  function() {
        window.currentSelect = obj.ci;
      });

在明确的例程中:

  $(".clearButton").on("click", function() {
    alert('Column:' + $('table thead tr th').eq(window.currentSelect).html().trim());
  });

摆弄替代解决方案。

暂无
暂无

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

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