Skip to content

Commit

Permalink
Merge pull request #5675 from Laravel-Backpack/add-debounce-to-filters
Browse files Browse the repository at this point in the history
add debounce to filters
  • Loading branch information
pxpm authored Oct 4, 2024
2 parents 3e3b279 + 09a22af commit c2e22e4
Showing 1 changed file with 28 additions and 2 deletions.
30 changes: 28 additions & 2 deletions src/resources/views/crud/inc/filters_navbar.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ function addOrUpdateUriParameter(uri, parameter, value) {
}
function updateDatatablesOnFilterChange(filterName, filterValue, update_url = false) {
function updateDatatablesOnFilterChange(filterName, filterValue, update_url = false, debounce = 500) {
// behaviour for ajax table
var current_url = crud.table.ajax.url();
var new_url = addOrUpdateUriParameter(current_url, filterName, filterValue);
Expand All @@ -68,12 +68,38 @@ function updateDatatablesOnFilterChange(filterName, filterValue, update_url = fa
// and we have a function that will do this update for us after all filters had been cleared.
if(update_url) {
// replace the datatables ajax url with new_url and reload it
crud.table.ajax.url(new_url).load();
callFunctionOnce(function() { refreshDatatablesOnFilterChange(new_url) }, debounce, 'refreshDatatablesOnFilterChange');
}
return new_url;
}
/**
* calls the function func once within the within time window.
* this is a debounce function which actually calls the func as
* opposed to returning a function that would call func.
*
* @param func the function to call
* @param within the time window in milliseconds, defaults to 300
* @param timerId an optional key, defaults to func
*
* FROM: https://stackoverflow.com/questions/27787768/debounce-function-in-jquery
*/
function callFunctionOnce(func, within = 300, timerId = null) {
window.callOnceTimers = window.callOnceTimers || {};
timerId = timerId || func;
if (window.callOnceTimers[timerId]) {
clearTimeout(window.callOnceTimers[timerId]);
}
window.callOnceTimers[timerId] = setTimeout(func, within);
}
function refreshDatatablesOnFilterChange(url)
{
// replace the datatables ajax url with new_url and reload it
crud.table.ajax.url(url).load();
}
function normalizeAmpersand(string) {
return string.replace(/&/g, "&").replace(/amp%3B/g, "");
Expand Down

0 comments on commit c2e22e4

Please sign in to comment.