【发布时间】:2020-07-21 06:38:01
【问题描述】:
我有一个 Jquery DataTables,在页脚我添加了一个 bootstrap-select 来过滤我的数据。
Inside the dropdown select, I added a button 'Clear filter' When there is/are option(s) selected.
当我单击该按钮时,我需要获取标题名称。
因此,在我的示例中,如果我单击“位置”列中的“清除过滤器”按钮,我应该提醒“位置”。 但它始终显示最后一列名称。
任何建议请我在我的代码中缺少什么?我添加了对我所做步骤的详细说明。非常感谢。
$(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>
【问题讨论】:
-
为什么不给按钮一个id,然后在点击处理程序中检查id?这样你就可以很容易地知道哪个按钮被点击了。
-
@Geshode 我将该按钮一次添加到页脚上的所有选择中。并且因为表格是动态的,它基于用户选择他们想要查看的信息,所以我无法为每个按钮设置唯一的 id。也许有可能这样做,首先检查可用的列,然后影响每个按钮的唯一 id ......我不知道。我的想法首先出现在我的脑海中,无法弄清楚如何解决它
标签: javascript jquery datatables bootstrap-selectpicker