@@ -53,7 +53,7 @@ function addOrUpdateUriParameter(uri, parameter, value) {
53
53
54
54
}
55
55
56
- function updateDatatablesOnFilterChange (filterName , filterValue , update_url = false ) {
56
+ function updateDatatablesOnFilterChange (filterName , filterValue , update_url = false , debounce = 500 ) {
57
57
// behaviour for ajax table
58
58
var current_url = crud .table .ajax .url ();
59
59
var new_url = addOrUpdateUriParameter (current_url, filterName, filterValue);
@@ -68,12 +68,38 @@ function updateDatatablesOnFilterChange(filterName, filterValue, update_url = fa
68
68
// and we have a function that will do this update for us after all filters had been cleared.
69
69
if (update_url) {
70
70
// replace the datatables ajax url with new_url and reload it
71
- crud . table . ajax . url (new_url). load ( );
71
+ callFunctionOnce ( function () { refreshDatatablesOnFilterChange (new_url) }, debounce, ' refreshDatatablesOnFilterChange ' );
72
72
}
73
73
74
74
return new_url;
75
75
}
76
76
77
+ /* *
78
+ * calls the function func once within the within time window.
79
+ * this is a debounce function which actually calls the func as
80
+ * opposed to returning a function that would call func.
81
+ *
82
+ * @param func the function to call
83
+ * @param within the time window in milliseconds, defaults to 300
84
+ * @param timerId an optional key, defaults to func
85
+ *
86
+ * FROM: https://stackoverflow.com/questions/27787768/debounce-function-in-jquery
87
+ */
88
+ function callFunctionOnce (func , within = 300 , timerId = null ) {
89
+ window .callOnceTimers = window .callOnceTimers || {};
90
+ timerId = timerId || func;
91
+ if (window .callOnceTimers [timerId]) {
92
+ clearTimeout (window .callOnceTimers [timerId]);
93
+ }
94
+ window .callOnceTimers [timerId] = setTimeout (func, within);
95
+ }
96
+
97
+ function refreshDatatablesOnFilterChange (url )
98
+ {
99
+ // replace the datatables ajax url with new_url and reload it
100
+ crud .table .ajax .url (url).load ();
101
+ }
102
+
77
103
78
104
function normalizeAmpersand (string ) {
79
105
return string .replace (/ &/ g , " &" ).replace (/ amp%3B/ g , " " );
0 commit comments