-
-
Notifications
You must be signed in to change notification settings - Fork 814
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
CRM-18286 - Use localStorage to remember datatable page length #8298
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -728,8 +728,20 @@ if (!CRM.vars) CRM.vars = {}; | |
}; | ||
|
||
$.fn.crmAjaxTable = function() { | ||
// Strip the ids from ajax urls to make pageLength storage more generic | ||
function simplifyUrl(ajax) { | ||
// Datatables ajax prop could be a url string or an object containing the url | ||
var url = typeof ajax === 'object' ? ajax.url : ajax; | ||
return typeof url === 'string' ? url.replace(/[&?]\w*id=\d+/g, '') : null; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What if you remove the querystring altogether? Is that too generic? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, it would apply to all datatables (at least in WP and Joomla where the civi path is part of the query string). |
||
} | ||
|
||
return $(this).each(function() { | ||
//Declare the defaults for DataTables | ||
// Recall pageLength for this table | ||
var url = simplifyUrl($(this).data('ajax')); | ||
if (url && window.localStorage && localStorage['dataTablePageLength:' + url]) { | ||
$(this).data('pageLength', localStorage['dataTablePageLength:' + url]); | ||
} | ||
// Declare the defaults for DataTables | ||
var defaults = { | ||
"processing": true, | ||
"serverSide": true, | ||
|
@@ -755,6 +767,12 @@ if (!CRM.vars) CRM.vars = {}; | |
}; | ||
//Include any table specific data | ||
var settings = $.extend(true, defaults, $(this).data('table')); | ||
// Remember pageLength | ||
$(this).on('length.dt', function(e, settings, len) { | ||
if (settings.ajax && window.localStorage) { | ||
localStorage['dataTablePageLength:' + simplifyUrl(settings.ajax)] = len; | ||
} | ||
}); | ||
//Make the DataTables call | ||
$(this).DataTable(settings); | ||
}); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this could be simplified as: