Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | {* |
2 | +--------------------------------------------------------------------+ | |
1188c7a8 | 3 | | Copyright CiviCRM LLC. All rights reserved. | |
6a488035 | 4 | | | |
1188c7a8 TO |
5 | | This work is published under the GNU AGPLv3 license with some | |
6 | | permitted exceptions and without any warranty. For full license | | |
7 | | and copyright information, see https://civicrm.org/licensing | | |
6a488035 TO |
8 | +--------------------------------------------------------------------+ |
9 | *} | |
10 | {literal} | |
11 | <script type="text/javascript"> | |
ae8f569f | 12 | CRM.$(function($) { |
6a488035 | 13 | |
ae8f569f CW |
14 | function getElementClass(element) { |
15 | return $(element).attr('class') || ''; | |
16 | } | |
6a488035 | 17 | |
ae8f569f | 18 | // fetch the occurrence of element |
a44340a5 | 19 | function getRowId(row, str) { |
ae8f569f | 20 | var optionId; |
a44340a5 CW |
21 | $.each(row, function(i, n) { |
22 | if (str === $(n).attr('class')) { | |
ae8f569f CW |
23 | optionId = i; |
24 | } | |
25 | }); | |
26 | return optionId; | |
27 | } | |
ebf2b57b | 28 | |
ae8f569f | 29 | // for date sorting see http://wiki.civicrm.org/confluence/display/CRMDOC/Sorting+Date+Fields+in+dataTables+Widget |
8bf54226 CW |
30 | var useAjax = {/literal}{if $useAjax}1{else}0{/if}{literal}, |
31 | sourceUrl = '', | |
32 | useClass = 'display', | |
33 | tcount = 1, | |
34 | tableId = []; | |
6a488035 | 35 | |
ae8f569f CW |
36 | if ( useAjax ) { |
37 | {/literal}{if isset($sourceUrl)}sourceUrl = "{$sourceUrl}";{/if}{literal} | |
38 | useClass = 'pagerDisplay'; | |
a44340a5 | 39 | tcount = 5; |
ae8f569f CW |
40 | } |
41 | ||
f25b7142 | 42 | CRM.dataTableCount = CRM.dataTableCount || 1; |
a44340a5 | 43 | |
8bf54226 | 44 | // FIXME: Rewriting DOM ids is probably a bad idea, and could be avoided |
f25b7142 CW |
45 | $('table.' + useClass).not('.dataTable').each(function() { |
46 | $(this).attr('id','option' + tcount + CRM.dataTableCount); | |
47 | tableId.push(CRM.dataTableCount); | |
48 | CRM.dataTableCount++; | |
ae8f569f | 49 | }); |
6a488035 | 50 | |
ae8f569f | 51 | $.each(tableId, function(i,n){ |
a44340a5 | 52 | var tabId = '#option' + tcount + n; |
ae8f569f | 53 | //get the object of first tr data row. |
a44340a5 | 54 | var tdObject = $(tabId + ' tr:nth(1) td'); |
ae8f569f CW |
55 | var id = -1; var count = 0; var columns=''; var sortColumn = ''; |
56 | //build columns array for sorting or not sorting | |
57 | $(tabId + ' th').each( function( ) { | |
58 | var option = $(this).prop('id').split("_"); | |
6a488035 | 59 | option = ( option.length > 1 ) ? option[1] : option[0]; |
a44340a5 | 60 | var stype = 'numeric'; |
6a488035 | 61 | switch( option ) { |
ae8f569f CW |
62 | case 'sortable': |
63 | sortColumn += '[' + count + ', "asc" ],'; | |
64 | columns += '{"sClass": "'+ getElementClass( this ) +'"},'; | |
6a488035 | 65 | break; |
ae8f569f CW |
66 | case 'date': |
67 | stype = 'date'; | |
68 | case 'order': | |
69 | if ( $(this).attr('class') == 'sortable' ){ | |
70 | sortColumn += '[' + count + ', "asc" ],'; | |
71 | } | |
a44340a5 | 72 | var sortId = getRowId(tdObject, $(this).attr('id') +' hiddenElement' ); |
ae8f569f | 73 | columns += '{ "render": function ( data, type, row ) { return "<div style=\'display:none\'>"+ data +"</div>" + row[sortId] ; }, "targets": sortColumn,"bUseRendered": false},'; |
6a488035 | 74 | break; |
ae8f569f CW |
75 | case 'nosort': |
76 | columns += '{ "bSortable": false, "sClass": "'+ getElementClass( this ) +'"},'; | |
6a488035 | 77 | break; |
ae8f569f CW |
78 | case 'currency': |
79 | columns += '{ "sType": "currency" },'; | |
6a488035 | 80 | break; |
ae8f569f CW |
81 | case 'link': |
82 | columns += '{"sType": "html"},'; | |
6a488035 | 83 | break; |
ae8f569f CW |
84 | default: |
85 | if ( $(this).text() ) { | |
86 | columns += '{"sClass": "'+ getElementClass( this ) +'"},'; | |
87 | } else { | |
88 | columns += '{ "bSortable": false },'; | |
89 | } | |
6a488035 TO |
90 | break; |
91 | } | |
92 | count++; | |
ae8f569f | 93 | }); |
a44340a5 CW |
94 | // Fixme: this could be done without eval |
95 | columns = columns.substring(0, columns.length - 1 ); | |
96 | sortColumn = sortColumn.substring(0, sortColumn.length - 1 ); | |
97 | eval('sortColumn =[' + sortColumn + ']'); | |
98 | eval('columns =[' + columns + ']'); | |
6a488035 | 99 | |
29bc93ee | 100 | var noRecordFoundMsg = {/literal}'{ts escape="js"}None found.{/ts}'{literal}; |
6a488035 | 101 | |
a44340a5 | 102 | var oTable; |
ae8f569f CW |
103 | if ( useAjax ) { |
104 | oTable = $(tabId).dataTable({ | |
a0def08c | 105 | "iDisplayLength": 25, |
a44340a5 CW |
106 | "bFilter": false, |
107 | "bAutoWidth": false, | |
108 | "aaSorting": sortColumn, | |
109 | "aoColumns": columns, | |
ae8f569f | 110 | "bProcessing": true, |
a44340a5 CW |
111 | "bJQueryUI": true, |
112 | "asStripClasses": [ "odd-row", "even-row" ], | |
ae8f569f | 113 | "sPaginationType": "full_numbers", |
a44340a5 | 114 | "sDom": '<"crm-datatable-pager-top"lfp>rt<"crm-datatable-pager-bottom"ip>', |
ae8f569f CW |
115 | "bServerSide": true, |
116 | "sAjaxSource": sourceUrl, | |
634598a1 | 117 | "oLanguage":{ |
a44340a5 CW |
118 | "sEmptyTable": noRecordFoundMsg, |
119 | "sZeroRecords": noRecordFoundMsg | |
634598a1 | 120 | }, |
ae8f569f CW |
121 | "fnServerData": function ( sSource, aoData, fnCallback ) { |
122 | $.ajax( { | |
123 | "dataType": 'json', | |
124 | "type": "POST", | |
125 | "url": sSource, | |
126 | "data": aoData, | |
127 | "success": fnCallback | |
128 | }); | |
129 | } | |
130 | }); | |
131 | } else { | |
132 | oTable = $(tabId).dataTable({ | |
a44340a5 CW |
133 | "aaSorting": sortColumn, |
134 | "bPaginate": false, | |
ae8f569f | 135 | "bLengthChange": true, |
a44340a5 CW |
136 | "bFilter": false, |
137 | "bInfo": false, | |
138 | "asStripClasses": [ "odd-row", "even-row" ], | |
139 | "bAutoWidth": false, | |
140 | "aoColumns": columns, | |
141 | "bSort": true, | |
8f70219a | 142 | "sDom": 'ti', |
634598a1 | 143 | "oLanguage":{ |
a44340a5 CW |
144 | "sEmptyTable": noRecordFoundMsg, |
145 | "sZeroRecords": noRecordFoundMsg | |
634598a1 | 146 | } |
ae8f569f CW |
147 | }); |
148 | } | |
6a488035 | 149 | }); |
ae8f569f | 150 | }); |
6a488035 | 151 | |
ae8f569f | 152 | //plugin to sort on currency |
ae8f569f | 153 | cj.fn.dataTableExt.oSort['currency-asc'] = function(a,b) { |
a5dfa653 | 154 | var symbol = "{/literal}{$defaultCurrencySymbol}{literal}"; |
ae8f569f CW |
155 | var x = (a == "-") ? 0 : a.replace( symbol, "" ); |
156 | var y = (b == "-") ? 0 : b.replace( symbol, "" ); | |
157 | x = parseFloat( x ); | |
158 | y = parseFloat( y ); | |
159 | return ((x < y) ? -1 : ((x > y) ? 1 : 0)); | |
160 | }; | |
6a488035 | 161 | |
ae8f569f | 162 | cj.fn.dataTableExt.oSort['currency-desc'] = function(a,b) { |
a5dfa653 | 163 | var symbol = "{/literal}{$defaultCurrencySymbol}{literal}"; |
ae8f569f CW |
164 | var x = (a == "-") ? 0 : a.replace( symbol, "" ); |
165 | var y = (b == "-") ? 0 : b.replace( symbol, "" ); | |
166 | x = parseFloat( x ); | |
167 | y = parseFloat( y ); | |
168 | return ((x < y) ? 1 : ((x > y) ? -1 : 0)); | |
169 | }; | |
6a488035 TO |
170 | </script> |
171 | {/literal} |