Commit | Line | Data |
---|---|---|
1c952f13 CW |
1 | // https://civicrm.org/licensing |
2 | (function($, _) { | |
3 | "use strict"; | |
4 | /* jshint validthis: true */ | |
5 | ||
6 | $.fn.crmAjaxTable = function() { | |
7 | ||
8 | // Strip the ids from ajax urls to make pageLength storage more generic | |
9 | function simplifyUrl(ajax) { | |
10 | // Datatables ajax prop could be a url string or an object containing the url | |
11 | var url = typeof ajax === 'object' ? ajax.url : ajax; | |
12 | return typeof url === 'string' ? url.replace(/[&?]\w*id=\d+/g, '') : null; | |
13 | } | |
14 | ||
15 | return $(this).each(function() { | |
16 | // Recall pageLength for this table | |
17 | var url = simplifyUrl($(this).data('ajax')); | |
18 | if (url && window.localStorage && localStorage['dataTablePageLength:' + url]) { | |
19 | $(this).data('pageLength', localStorage['dataTablePageLength:' + url]); | |
20 | } | |
21 | // Declare the defaults for DataTables | |
22 | var defaults = { | |
23 | "processing": true, | |
24 | "serverSide": true, | |
25 | "order": [], | |
26 | "dom": '<"crm-datatable-pager-top"lfp>rt<"crm-datatable-pager-bottom"ip>', | |
27 | "pageLength": 25, | |
28 | "pagingType": "full_numbers", | |
29 | "drawCallback": function(settings) { | |
30 | //Add data attributes to cells | |
31 | $('thead th', settings.nTable).each( function( index ) { | |
32 | $.each(this.attributes, function() { | |
33 | if(this.name.match("^cell-")) { | |
34 | var cellAttr = this.name.substring(5); | |
35 | var cellValue = this.value; | |
36 | $('tbody tr', settings.nTable).each( function() { | |
37 | $('td:eq('+ index +')', this).attr( cellAttr, cellValue ); | |
38 | }); | |
39 | } | |
40 | }); | |
41 | }); | |
42 | //Reload table after draw | |
43 | $(settings.nTable).trigger('crmLoad'); | |
44 | } | |
45 | }; | |
46 | //Include any table specific data | |
47 | var settings = $.extend(true, defaults, $(this).data('table')); | |
48 | // Remember pageLength | |
49 | $(this).on('length.dt', function(e, settings, len) { | |
50 | if (settings.ajax && window.localStorage) { | |
51 | localStorage['dataTablePageLength:' + simplifyUrl(settings.ajax)] = len; | |
52 | } | |
53 | }); | |
54 | //Make the DataTables call | |
55 | $(this).DataTable(settings); | |
56 | }); | |
57 | }; | |
58 | ||
59 | })(CRM.$, CRM._); |