admin管理员组文章数量:1391805
I created a datatable that filters on each column. Everything was fine until I updated my code trying to reset all filters. Here's my code:
$('.dt-column-search thead tr:eq(1) th').each(function(i) {
var title = $(this).text();
//another condition {
} else {
$(this).html(
'<button class="btn btn-danger btn-sm" id="resetFilter">reset</button>');
}
$('input, select', this).on('keyup change', function() {
if (dt_filter.column(i).search() !== this.value) {
dt_filter.column(i).search(this.value).draw();
}
});
});
var dt_filter = dt_filter_table.DataTable({
ajax: {
},
columns: [
],
orderCellsTop: true,
dom: '<"row"<"col-sm-12 col-md-6"l>><"table-responsive"t><"row"<"col-sm-12 col-md-6"i><"col-sm-12 col-md-6"p>>',
drawCallback: function() {
const resetFilter = document.querySelector('#resetFilter');
if (resetFilter) {
resetFilter.addEventListener('click', function() {
$('input[type="text"]').val('');
$('select').val('').trigger('change');
$('.flatpickr-input').each(function() {
if ($(this).hasClass('flatpickr-input')) {
this._flatpickr.clear();
}
});
dt_filter.columns().search('').draw();
dt_filter.ajax.reload(null, false);
});
}
}
});
The problem is that the table is not showing data as wanted when pressing the reset button see the image.
Any help please!
I created a datatable that filters on each column. Everything was fine until I updated my code trying to reset all filters. Here's my code:
$('.dt-column-search thead tr:eq(1) th').each(function(i) {
var title = $(this).text();
//another condition {
} else {
$(this).html(
'<button class="btn btn-danger btn-sm" id="resetFilter">reset</button>');
}
$('input, select', this).on('keyup change', function() {
if (dt_filter.column(i).search() !== this.value) {
dt_filter.column(i).search(this.value).draw();
}
});
});
var dt_filter = dt_filter_table.DataTable({
ajax: {
},
columns: [
],
orderCellsTop: true,
dom: '<"row"<"col-sm-12 col-md-6"l>><"table-responsive"t><"row"<"col-sm-12 col-md-6"i><"col-sm-12 col-md-6"p>>',
drawCallback: function() {
const resetFilter = document.querySelector('#resetFilter');
if (resetFilter) {
resetFilter.addEventListener('click', function() {
$('input[type="text"]').val('');
$('select').val('').trigger('change');
$('.flatpickr-input').each(function() {
if ($(this).hasClass('flatpickr-input')) {
this._flatpickr.clear();
}
});
dt_filter.columns().search('').draw();
dt_filter.ajax.reload(null, false);
});
}
}
});
The problem is that the table is not showing data as wanted when pressing the reset button see the image.
Any help please!
Share Improve this question edited Mar 14 at 9:52 pmaruszczyk 2,1652 gold badges24 silver badges50 bronze badges asked Mar 12 at 6:33 cussaxxcussaxx 111 silver badge1 bronze badge 1 |1 Answer
Reset to default 0#resetFilter
is used as anid
(should be unique). Instead, you can assign aclass
(e.g.,.resetFilter
).$(this).html('<button class="btn btn-danger btn-sm resetFilter">reset</button>');
The
resetFilter
button's event listener is being bound inside thedrawCallback
, butdrawCallback
is invoked repeatedly, causing redundant bindings. This can be avoided by using event delegation:$(document).on('click', '.resetFilter', function() { $('input[type="text"]').val(''); $('select').val('').trigger('change'); $('.flatpickr-input').each(function() { if ($(this).hasClass('flatpickr-input')) { this._flatpickr.clear(); } }); dt_filter.columns().search('').draw(); dt_filter.ajax.reload(null, false); });
本文标签: javascriptHow to reset datatable columns39 filtersStack Overflow
版权声明:本文标题:javascript - How to reset datatable columns' filters - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744767725a2624143.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
datatable
tag (e.g. by hovering your mouse over the tag) - and then follow the tag usage guidelines mentioned there. – andrewJames Commented Mar 12 at 15:06