Skip to content

Commit e85cbf4

Browse files
committed
feat(38078): add clear-filters logic
1 parent fa79e83 commit e85cbf4

File tree

1 file changed

+20
-12
lines changed

1 file changed

+20
-12
lines changed

src/Datatable/Datatable.php

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,10 @@ class Datatable
272272
api.on('order', function() {
273273
saveFilters(api);
274274
})
275+
276+
$('.clear-filters').on('click', function() {
277+
resetFilters(api)
278+
})
275279
COLUMN_SEARCH_CONFIGURATION;
276280

277281
protected $genericSearchTemplate = <<<GENERIC_SEARCH_CONFIGURATION
@@ -355,7 +359,7 @@ class Datatable
355359
356360
dt.css(:tableCss);
357361
358-
function saveFilters(api) {
362+
async function saveFilters(api) {
359363
let filters = {};
360364
$('#:tagId .filters input, #:tagId .filters select').each(function (index, item) {
361365
filters[index] = $(item).val();
@@ -366,20 +370,24 @@ function saveFilters(api) {
366370
localStorage.setItem('filters_:tagId', JSON.stringify({filters, order}));
367371
}
368372
369-
function loadFilters(api) {
370-
let {filters, order} = JSON.parse(localStorage.getItem('filters_:tagId'));
371-
if (filters) {
372-
$('#:tagId .filters input, #:tagId .filters select').each(function (index, item) {
373-
$(item).val(filters[index]);
374-
api.columns(index).search(filters[index])
375-
});
376-
}
377-
if (order) {
378-
api.order(order);
379-
}
373+
async function loadFilters(api) {
374+
let {filters, order} = JSON.parse(localStorage.getItem('filters_:tagId')) ?? {filters: [], order: []};
375+
376+
$('#:tagId .filters input, #:tagId .filters select').each(function (index, item) {
377+
$(item).val(filters[index] ?? null);
378+
api.columns(index).search(filters[index] ?? '')
379+
});
380+
381+
api.order(order);
382+
380383
api.draw();
381384
}
382385
386+
async function resetFilters(api) {
387+
await localStorage.removeItem('filters_:tagId');
388+
await loadFilters(api)
389+
}
390+
383391
function validateDate(text) {
384392
text = text.replaceAll("/","-");
385393
var re = /^(\d{4}(-)\d{2}(-)\d{2}|\d{2}(-)\d{2}(-)\d{4})$/;

0 commit comments

Comments
 (0)