Merge pull request #3342 from systopia/CRM-14741
[civicrm-core.git] / templates / CRM / common / jsortable.tpl
1 {*
2 +--------------------------------------------------------------------+
3 | CiviCRM version 4.5 |
4 +--------------------------------------------------------------------+
5 | Copyright CiviCRM LLC (c) 2004-2014 |
6 +--------------------------------------------------------------------+
7 | This file is a part of CiviCRM. |
8 | |
9 | CiviCRM is free software; you can copy, modify, and distribute it |
10 | under the terms of the GNU Affero General Public License |
11 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
12 | |
13 | CiviCRM is distributed in the hope that it will be useful, but |
14 | WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
16 | See the GNU Affero General Public License for more details. |
17 | |
18 | You should have received a copy of the GNU Affero General Public |
19 | License and the CiviCRM Licensing Exception along |
20 | with this program; if not, contact CiviCRM LLC |
21 | at info[AT]civicrm[DOT]org. If you have questions about the |
22 | GNU Affero General Public License or the licensing of CiviCRM, |
23 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
24 +--------------------------------------------------------------------+
25 *}
26 {literal}
27 <script type="text/javascript">
28 CRM.$(function($) {
29 // for date sorting see http://wiki.civicrm.org/confluence/display/CRMDOC/Sorting+Date+Fields+in+dataTables+Widget
30 var useAjax = {/literal}{if $useAjax}1{else}0{/if}{literal};
31 var sortEnabled = true;
32 var sourceUrl = '';
33 var useClass = 'display';
34
35 var tcount =1;
36 if ( useAjax ) {
37 {/literal}{if isset($sourceUrl)}sourceUrl = "{$sourceUrl}";{/if}{literal}
38 useClass = 'pagerDisplay';
39 tcount =5;
40 }
41
42 var tableId = '';
43 var count = 1;
44
45 //rename id of table with sequence
46 //and create the object for navigation
47 cj('table.' + useClass).each(function(){
48 cj(this).attr('id','option' + tcount + count);
49 tableId += count + ',';
50 count++;
51 });
52
53 //remove last comma
54 tableId = tableId.substring(0, tableId.length - 1 );
55 eval('tableId =[' + tableId + ']');
56
57 cj.each(tableId, function(i,n){
58 tabId = '#option' + tcount + n;
59 //get the object of first tr data row.
60 tdObject = cj(tabId + ' tr:nth(1) td');
61 var id = -1; var count = 0; var columns=''; var sortColumn = '';
62 //build columns array for sorting or not sorting
63 cj(tabId + ' th').each( function( ) {
64 var option = cj(this).prop('id').split("_");
65 option = ( option.length > 1 ) ? option[1] : option[0];
66 stype = 'numeric';
67 switch( option ) {
68 case 'sortable':
69 sortColumn += '[' + count + ', "asc" ],';
70 columns += '{"sClass": "'+ getElementClass( this ) +'"},';
71 break;
72 case 'date':
73 stype = 'date';
74 case 'order':
75 if ( cj(this).attr('class') == 'sortable' ){
76 sortColumn += '[' + count + ', "asc" ],';
77 }
78 sortId = getRowId(tdObject, cj(this).attr('id') +' hiddenElement' );
79 sortEnabled = false;
80 columns += '{ "sType": \'' + stype + '\', "fnRender": function (oObj) { return oObj.aData[' + sortId + ']; },"bUseRendered": false},';
81 break;
82 case 'nosort':
83 columns += '{ "bSortable": false, "sClass": "'+ getElementClass( this ) +'"},';
84 break;
85 case 'currency':
86 columns += '{ "sType": "currency" },';
87 break;
88 case 'link':
89 columns += '{"sType": "html"},';
90 break;
91 default:
92 if ( cj(this).text() ) {
93 columns += '{"sClass": "'+ getElementClass( this ) +'"},';
94 } else {
95 columns += '{ "bSortable": false },';
96 }
97 break;
98 }
99 count++;
100 });
101 columns = columns.substring(0, columns.length - 1 );
102 sortColumn = sortColumn.substring(0, sortColumn.length - 1 );
103 eval('sortColumn =[' + sortColumn + ']');
104 eval('columns =[' + columns + ']');
105
106 var currTable = cj(tabId);
107 if (currTable) {
108 // contains the dataTables master records
109 var s = cj(document).dataTableSettings;
110 if (s != 'undefined') {
111 var len = s.length;
112 for (var i=0; i < len; i++) {
113 // if already exists, remove from the array
114 if (s[i].sInstance = tabId) {
115 s.splice(i,1);
116 }
117 }
118 }
119 }
120
121 var noRecordFoundMsg = {/literal}'{ts escape="js"}There are no records.{/ts}'{literal};
122
123 oTable = null;
124 if ( useAjax ) {
125 oTable = cj(tabId).dataTable({
126 "bFilter" : false,
127 "bAutoWidth" : false,
128 "aaSorting" : sortColumn,
129 "aoColumns" : columns,
130 "bProcessing": true,
131 "bJQueryUI" : true,
132 "asStripClasses" : [ "odd-row", "even-row" ],
133 "sPaginationType": "full_numbers",
134 "sDom" : '<"crm-datatable-pager-top"lfp>rt<"crm-datatable-pager-bottom"ip>',
135 "bServerSide": true,
136 "sAjaxSource": sourceUrl,
137 "oLanguage":{"sEmptyTable" : noRecordFoundMsg,
138 "sZeroRecords" : noRecordFoundMsg },
139
140 {/literal}{if !empty($callBack)}{literal}
141 "fnDrawCallback": function() { checkSelected(); },
142 {/literal}{/if}{literal}
143
144 "fnServerData": function ( sSource, aoData, fnCallback ) {
145 cj.ajax( {
146 "dataType": 'json',
147 "type": "POST",
148 "url": sSource,
149 "data": aoData,
150 "success": fnCallback
151 });
152 }
153 });
154 } else {
155 oTable = cj(tabId).dataTable({
156 "aaSorting" : sortColumn,
157 "bPaginate" : false,
158 "bLengthChange": true,
159 "bFilter" : false,
160 "bInfo" : false,
161 "asStripClasses" : [ "odd-row", "even-row" ],
162 "bAutoWidth" : false,
163 "aoColumns" : columns,
164 "bSort" : sortEnabled,
165 "oLanguage":{"sEmptyTable" : noRecordFoundMsg,
166 "sZeroRecords" : noRecordFoundMsg }
167 });
168 }
169 var object;
170
171 });
172 });
173
174 function getElementClass( element ) {
175 if( cj(element).attr('class') ) return cj(element).attr('class');
176 return '';
177 }
178
179 //function to fetch the occurence of element
180 function getRowId(row,str){
181 cj.each( row, function(i, n) {
182 if( str === cj(n).attr('class') ) {
183 optionId = i;
184 }
185 });
186 return optionId;
187 }
188
189 //plugin to sort on currency
190 var symbol = "{/literal}{$config->defaultCurrencySymbol($config->defaultSymbol)}{literal}";
191 cj.fn.dataTableExt.oSort['currency-asc'] = function(a,b) {
192 var x = (a == "-") ? 0 : a.replace( symbol, "" );
193 var y = (b == "-") ? 0 : b.replace( symbol, "" );
194 x = parseFloat( x );
195 y = parseFloat( y );
196 return ((x < y) ? -1 : ((x > y) ? 1 : 0));
197 };
198
199 cj.fn.dataTableExt.oSort['currency-desc'] = function(a,b) {
200 var x = (a == "-") ? 0 : a.replace( symbol, "" );
201 var y = (b == "-") ? 0 : b.replace( symbol, "" );
202 x = parseFloat( x );
203 y = parseFloat( y );
204 return ((x < y) ? 1 : ((x > y) ? -1 : 0));
205 };
206 </script>
207 {/literal}