因此,我认为这可能是数据表库中的一个bug。我只使用他们在jsfiddle中的示例代码就可以再现这一结果。
重建步骤:
注意:数据表仍然保持过滤,但是搜索字段现在都是空的。
有没有其他人看到或者看到我在这里做错了什么?
这是javascript
<script src="js/jquery.js" type="text/javascript"></script>
<script src="js/jquery.dataTables.js" type="text/javascript"></script>
<script src='https://code.jquery.com/jquery-1.12.4.js'></script>
<script src='https://cdn.datatables.net/1.10.16/js/jquery.dataTables.min.js' type="text/javascript"></script>
<script src='https://cdn.datatables.net/select/1.2.5/js/dataTables.select.min.js' type="text/javascript"></script>
<script src='https://cdn.datatables.net/buttons/1.5.1/js/dataTables.buttons.min.js' type="text/javascript"></script>
<script src='https://cdn.datatables.net/1.10.16/js/jquery.dataTables.min.js' type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function() {
const table = $('#example').DataTable({
stateSave: true
});
// Setup - add a text input to each footer cell
$('#example tfoot th').each( function () {
var title = $(this).text();
$(this).html( '<input type="text" placeholder="Search '+title+'" />' );
} );
// Apply the search
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>大部分代码是从这个链接filter.html中提取的。
预期的功能是数据表应保存加载之间的状态,搜索框应在页面重新加载时重新加载筛选的文本。
发布于 2018-04-17 16:41:35
因此,使用我的代码和上面的答案的组合,我想出了一个解决方案
$(document).ready(function() {
const table = $('#example').DataTable({
stateSave: true
});
// Setup - add a text input to each footer cell
$('#example tfoot th').each( function (index,value) {
var title = $(this).text();
$(this).html( '<input type="text" placeholder="Search '+title+'" value="'+table.column(index).search()+'"/>' );
} );
// Apply the search
table.columns().every( function () {
var that = this;
$( 'input', this.footer() ).on( 'keyup change', function () {
if ( that.search() !== this.value ) {
that
.search( this.value )
.draw();
}
} );
});
});这里的主要代码是修复程序
$(this).html( '<input type="text" placeholder="Search '+title+'" value="'+table.column(index).search()+'"/>' );首先获得索引值对,然后遍历列
$('#example tfoot th').each( function (index,value) 然后,窃取上面的答案,按索引从列中注入搜索值。
$(this).html( '<input type="text" placeholder="Search '+title+'" value="'+table.column(index).search()+'"/>' );发布于 2018-04-17 12:33:02
这是因为您启用了stateSave。这样可以执行列搜索,这是DataTables的内部内容,但是由于输入元素是外部的,而且DataTables不知道它们,所以必须自己填充这些元素。看看这示例,它是在initComplete中实现的
// Restore state saved values
var state = this.state.loaded();
if (state) {
var val = state.columns[this.index()];
input.val(val.search.search);
}https://stackoverflow.com/questions/49878160
复制相似问题