Skip to content
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

Merged
merged 3 commits into from
May 21, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion js/Common.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Copy link

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:

var url = ajax.url || ajax

return typeof url === 'string' ? url.replace(/[&?]\w*id=\d+/g, '') : null;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if you remove the querystring altogether? Is that too generic?

Copy link
Member Author

Choose a reason for hiding this comment

The 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,
Expand All @@ -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);
});
Expand Down
16 changes: 3 additions & 13 deletions templates/CRM/Contact/Page/View/RelationshipSelector.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@
+--------------------------------------------------------------------+
*}
{* relationship selector *}

<div class="crm-contact-relationship-{$context}">
<table class="crm-contact-relationship-selector-{$context} crm-ajax-table" data-page-length='10'>
<table
class="crm-contact-relationship-selector-{$context} crm-ajax-table"
data-ajax="{crmURL p="civicrm/ajax/contactrelationships" q="context=$context&cid=$contactId"}">
<thead>
<tr>
<th data-data="relation" class='crm-contact-relationship-type'>{ts}Relationship{/ts}</th>
Expand All @@ -41,15 +42,4 @@
</tr>
</thead>
</table>

{literal}
<script type="text/javascript">
(function($) {
var context = {/literal}"{$context}"{literal};
CRM.$('table.crm-contact-relationship-selector-' + context).data({
"ajax": {/literal}'{crmURL p="civicrm/ajax/contactrelationships" h=0 q="context=$context&cid=$contactId"}'{literal},
});
})(CRM.$);
</script>
{/literal}
</div>