1 // https://civicrm.org/licensing
4 /* jshint validthis: true */
6 $.fn
.crmAjaxTable = function() {
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;
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
]);
21 // Declare the defaults for DataTables
26 "dom": '<"crm-datatable-pager-top"lfp>rt<"crm-datatable-pager-bottom"ip>',
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
);
42 //Reload table after draw
43 $(settings
.nTable
).trigger('crmLoad');
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
;
54 //Make the DataTables call
55 $(this).DataTable(settings
);