英文:
How to apply clear filter to datatables reset button?
问题
我正在处理级联数据表列。它运行正常。但是,我不确定如何将重置筛选应用于重置按钮。这是我拥有的内容。要求是当我单击“清除筛选”时,它将删除下拉列筛选以及搜索文本筛选。有办法做到这一点吗?谢谢。
https://live.datatables.net/xexeraci/1/edit
$(document).ready(function() {
var table = $('#example').DataTable({
responsive: true,
searching: true
});
buildSelect(table);
table.on('draw', function () {
buildSelect(table);
});
});
function buildSelect(table) {
var counter = 0;
table.columns([0, 1, 2]).every(function () {
var column = table.column(this, {search: 'applied'});
counter++;
var select = $('<select><option value=""></option></select>')
.appendTo($('#dropdown' + counter).empty())
.on('change', function () {
var val = $.fn.dataTable.util.escapeRegex(
$(this).val()
);
column
.search(val ? '^'+val+'$' : '', true, false)
.draw();
});
column.data().unique().sort().each(function (d, j) {
select.append('<option value="' + d + '">' + d + '</option>');
});
// 重建将清除现有的选择,因此需要重新填充
var currSearch = column.search();
if (currSearch) {
select.val(currSearch.substring(1, currSearch.length-1));
}
});
}
<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-1.11.3.min.js"></script>
<link href="https://nightly.datatables.net/css/jquery.dataTables.css" rel="stylesheet" type="text/css" />
<script src="https://nightly.datatables.net/js/jquery.dataTables.js"></script>
<meta charset=utf-8 />
<title>DataTables - JS Bin</title>
</head>
<div class="searchbox">
<p>Name: <span id="dropdown1">
</span>
</p>
<p>Position: <span id="dropdown2">
</span>
</p>
<p>Office: <span id="dropdown3">
</span>
</p>
<button type="button" id="test">Clear Filters</button>
</div>
<table id="example" class="cell-border row-border stripe dataTable no-footer dtr-inline" role="grid" style=" width: 100%; padding-top: 10px;"><thead>
<tr>
<th> </th>
<th> </th>
<th> </th>
<th colspan="3" style=" text-align: center;">Information</th>
</tr>
<tr>
<th>Name</th>
<th>Position</th>
<th>Office</th>
<th>Age</th>
<th>Start date</th>
<th>Salary</th>
</tr>
</thead>
<tbody>
<tr>
<td>ID.AI</td>
<td>System Architect</td>
<td>Edinburgh</td>
<td>61</td>
<td>2011/04/25</td>
<td>$3,120</td>
</tr>
<tr>
<td>Garrett -2</td>
<td>Director</td>
<td>Edinburgh</td>
<td>63</td>
<td>2011/07/25</td>
<td>$5,300</td>
</tr>
<tr>
<td>Ashton.1 -2</td>
<td>Technical Author</td>
<td>San Francisco</td>
<td>66</td>
<td>2009/01/12</td>
<td>$4,800</td>
</tr>
</tbody></table>
</div>
英文:
I am working on cascading datatable columns. It works fine. However, I am not sure how to apply reset filter to the reset button. Here is what I have. The requirement is when I click on Clear Filters, it will remove the drop-down column filter as well as search text filter. Is there a way to do this? Thanks.
https://live.datatables.net/xexeraci/1/edit
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
$(document).ready(function() {
var table = $('#example').DataTable({
responsive: true,
searching: true
});
buildSelect( table );
table.on( 'draw', function () {
buildSelect( table );
} );
});
function buildSelect( table ) {
var counter = 0;
table.columns( [0, 1, 2] ).every( function () {
var column = table.column( this, {search: 'applied'} );
counter++;
var select = $('<select><option value=""></option></select>')
.appendTo( $( '#dropdown' + counter ).empty() )
.on( 'change', function () {
var val = $.fn.dataTable.util.escapeRegex(
$(this).val()
);
column
.search( val ? '^'+val+'$' : '', true, false )
.draw();
} );
column.data().unique().sort().each( function ( d, j ) {
select.append( '<option value="'+d+'">'+d+'</option>' );
} );
// The rebuild will clear the exisiting select, so it needs to be repopulated
var currSearch = column.search();
if ( currSearch ) {
select.val( currSearch.substring(1, currSearch.length-1) );
}
} );
}
<!-- language: lang-html -->
<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-1.11.3.min.js"></script>
<link href="https://nightly.datatables.net/css/jquery.dataTables.css" rel="stylesheet" type="text/css" />
<script src="https://nightly.datatables.net/js/jquery.dataTables.js"></script>
<meta charset=utf-8 />
<title>DataTables - JS Bin</title>
</head>
<div class="searchbox">
<p>Name: <span id="dropdown1">
</span>
</p>
<p>Postion: <span id="dropdown2">
</span>
</p>
<p>Office: <span id="dropdown3">
</span>
</p>
<button type="button" id="test">Clear Filters</button>
</div>
<table id="example" class="cell-border row-border stripe dataTable no-footer dtr-inline" role="grid" style=" width: 100%; padding-top: 10px;"><thead>
<tr>
<th>&#160;</th>
<th>&#160;</th>
<th>&#160;</th>
<th colspan="3" style=" text-align: center;">Information</th>
</tr>
<tr>
<th>Name</th>
<th>Position</th>
<th>Office</th>
<th>Age</th>
<th>Start date</th>
<th>Salary</th>
</tr>
</thead>
<tbody>
<tr>
<td>ID.AI</td>
<td>System Architect</td>
<td>Edinburgh</td>
<td>61</td>
<td>2011/04/25</td>
<td>$3,120</td>
</tr>
<tr>
<td>Garrett -2</td>
<td>Director</td>
<td>Edinburgh</td>
<td>63</td>
<td>2011/07/25</td>
<td>$5,300</td>
</tr>
<tr>
<td>Ashton.1 -2</td>
<td>Technical Author</td>
<td>San Francisco</td>
<td>66</td>
<td>2009/01/12</td>
<td>$4,800</td>
</tr>
</tbody></table>
</div>
<!-- end snippet -->
答案1
得分: 1
你需要为"Clear Filters"按钮添加一个点击监听器。
演示链接: https://live.datatables.net/clone/7731/edit
这段代码对我来说运行正常。
$(document).ready(function() {
var table = $('#example').DataTable({
responsive: true,
searching: true
});
buildSelect(table);
table.on('draw', function() {
buildSelect(table);
});
$('#test').on('click', function() {
table.search('').columns().search('').draw();
});
});
英文:
You have to add a click listener to the "Clear Filters" button.
Demo: https://live.datatables.net/clone/7731/edit
This code works fine for me.
$(document).ready(function() {
var table = $('#example').DataTable({
responsive: true,
searching: true
});
buildSelect(table);
table.on('draw', function() {
buildSelect(table);
});
$('#test').on('click', function() {
table.search('').columns().search('').draw();
});
});
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论