У меня есть маленький код в 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, чтобы обновить значение из поля ввода, удаляется, но фильтрация остается. Если вы войдете в этот фильтр и нажмете клавишу Backspace, он снова начнет работать. Значение там, просто невидимое.
Теперь в моей Chrome Canary такой проблемы нет, но я вижу ее в других браузерах. Я даже вижу тот же Chrome, когда код размещен в коде пера, так что он явно там.
Как я могу решить это? Я вижу 2 варианта:
- заставить фильтр правильно отображаться, чтобы пользователь знал
- удалить все фильтры при обновлении
Но как на моем примере я могу это сделать? Я пытался играть с ним, но безуспешно.