我在 DataTables.net 中有这个小代码负责显示表格。
<script>
$(document).ready(function () {
// Setup - add a text input to each header cell
$('#DT-GRvWJTAH thead th').each(function () {
var title = $(this).text();
$(this).html('<input type="text" placeholder="' + title + '" />');
});
// Setup - add a text input to each footer cell
$('#DT-GRvWJTAH tfoot th').each(function () {
var title = $(this).text();
$(this).html('<input type="text" placeholder="' + title + '" />');
});
// Table code
var table = $('#DT-GRvWJTAH').DataTable({
"dom": "Bfrtip",
"buttons": [
"copyHtml5",
"excelHtml5",
"csvHtml5",
"pdfHtml5"
],
"colReorder": true,
"paging": true,
"scrollCollapse": false,
"pagingType": [
"full_numbers"
],
"lengthMenu": [
[
15,
25,
50,
100
],
-1,
[
15,
25,
50,
100
],
"All"
],
"ordering": true,
"order": [
],
"info": true,
"procesing": true,
"responsive": {
"details": true
},
"select": true,
"searching": true,
"stateSave": true
});
// Apply the search for header cells
table.columns().eq(0).each(function (colIdx) {
$('input', table.column(colIdx).header()).on('keyup change', function () {
table
.column(colIdx)
.search(this.value)
.draw();
});
$('input', table.column(colIdx).header()).on('click', function (e) {
e.stopPropagation();
});
});
// Apply the search for footer cells
table.columns().every(function () {
var that = this;
$('input', this.footer()).on('keyup change', function () {
if (that.search() !== this.value) {
that.search(this.value).draw();
}
});
});
});
</script>
完整代码在这里:https ://codepen.io/przemyslawklys/pen/jRxLRy
这是一个启用过滤的简单表。问题是当您过滤某些列并按 F5 刷新输入字段中的值时,将被删除,但过滤仍然存在。如果您进入该过滤器并按退格键,它将再次开始工作。价值就在那里,只是看不见。
现在在我的 Chrome Canary 中,它没有这个问题,但我可以在其他浏览器中看到它。当代码托管在代码笔中时,我什至可以看到它与 Chrome 相同,所以它显然就在那里。
我该如何解决这个问题?我看到 2 个选项:
- 使过滤器正确显示,以便用户知道
- 在刷新时删除任何过滤器
但是在我的例子中我怎么能做到这一点?我试图玩它,但没有真正的成功。