Merge pull request #3805 from colemanw/groups
[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
30 function getElementClass(element) {
31 return $(element).attr('class') || '';
32 }
33
34 // fetch the occurrence of element
35 function getRowId(row,str) {
36 var optionId;
37 $.each( row, function(i, n) {
38 if( str === $(n).attr('class') ) {
39 optionId = i;
40 }
41 });
42 return optionId;
43 }
44
45 // for date sorting see http://wiki.civicrm.org/confluence/display/CRMDOC/Sorting+Date+Fields+in+dataTables+Widget
46 var useAjax = {/literal}{if $useAjax}1{else}0{/if}{literal};
47 var sortEnabled = true;
48 var sourceUrl = '';
49 var useClass = 'display';
50
51 var tcount =1;
52 if ( useAjax ) {
53 {/literal}{if isset($sourceUrl)}sourceUrl = "{$sourceUrl}";{/if}{literal}
54 useClass = 'pagerDisplay';
55 tcount =5;
56 }
57
58 var tableId = '';
59 var count = 1;
60
61 //rename id of table with sequence
62 //and create the object for navigation
63 $('table.' + useClass).each(function(){
64 $(this).attr('id','option' + tcount + count);
65 tableId += count + ',';
66 count++;
67 });
68
69 //remove last comma
70 tableId = tableId.substring(0, tableId.length - 1 );
71 eval('tableId =[' + tableId + ']');
72
73 $.each(tableId, function(i,n){
74 tabId = '#option' + tcount + n;
75 //get the object of first tr data row.
76 tdObject = $(tabId + ' tr:nth(1) td');
77 var id = -1; var count = 0; var columns=''; var sortColumn = '';
78 //build columns array for sorting or not sorting
79 $(tabId + ' th').each( function( ) {
80 var option = $(this).prop('id').split("_");
81 option = ( option.length > 1 ) ? option[1] : option[0];
82 stype = 'numeric';
83 switch( option ) {
84 case 'sortable':
85 sortColumn += '[' + count + ', "asc" ],';
86 columns += '{"sClass": "'+ getElementClass( this ) +'"},';
87 break;
88 case 'date':
89 stype = 'date';
90 case 'order':
91 if ( $(this).attr('class') == 'sortable' ){
92 sortColumn += '[' + count + ', "asc" ],';
93 }
94 sortId = getRowId(tdObject, $(this).attr('id') +' hiddenElement' );
95 sortEnabled = true;
96 columns += '{ "render": function ( data, type, row ) { return "<div style=\'display:none\'>"+ data +"</div>" + row[sortId] ; }, "targets": sortColumn,"bUseRendered": false},';
97 break;
98 case 'nosort':
99 columns += '{ "bSortable": false, "sClass": "'+ getElementClass( this ) +'"},';
100 break;
101 case 'currency':
102 columns += '{ "sType": "currency" },';
103 break;
104 case 'link':
105 columns += '{"sType": "html"},';
106 break;
107 default:
108 if ( $(this).text() ) {
109 columns += '{"sClass": "'+ getElementClass( this ) +'"},';
110 } else {
111 columns += '{ "bSortable": false },';
112 }
113 break;
114 }
115 count++;
116 });
117 columns = columns.substring(0, columns.length - 1 );
118 sortColumn = sortColumn.substring(0, sortColumn.length - 1 );
119 eval('sortColumn =[' + sortColumn + ']');
120 eval('columns =[' + columns + ']');
121
122 var currTable = $(tabId);
123 if (currTable) {
124 // contains the dataTables master records
125 var s = $(document).dataTableSettings;
126 if (s != 'undefined') {
127 var len = s.length;
128 for (var i=0; i < len; i++) {
129 // if already exists, remove from the array
130 if (s[i].sInstance = tabId) {
131 s.splice(i,1);
132 }
133 }
134 }
135 }
136
137 var noRecordFoundMsg = {/literal}'{ts escape="js"}There are no records.{/ts}'{literal};
138
139 oTable = null;
140 if ( useAjax ) {
141 oTable = $(tabId).dataTable({
142 "bFilter" : false,
143 "bAutoWidth" : false,
144 "aaSorting" : sortColumn,
145 "aoColumns" : columns,
146 "bProcessing": true,
147 "bJQueryUI" : true,
148 "asStripClasses" : [ "odd-row", "even-row" ],
149 "sPaginationType": "full_numbers",
150 "sDom" : '<"crm-datatable-pager-top"lfp>rt<"crm-datatable-pager-bottom"ip>',
151 "bServerSide": true,
152 "sAjaxSource": sourceUrl,
153 "oLanguage":{"sEmptyTable" : noRecordFoundMsg,
154 "sZeroRecords" : noRecordFoundMsg },
155
156 {/literal}{if !empty($callBack)}{literal}
157 "fnDrawCallback": function() { checkSelected(); },
158 {/literal}{/if}{literal}
159
160 "fnServerData": function ( sSource, aoData, fnCallback ) {
161 $.ajax( {
162 "dataType": 'json',
163 "type": "POST",
164 "url": sSource,
165 "data": aoData,
166 "success": fnCallback
167 });
168 }
169 });
170 } else {
171 oTable = $(tabId).dataTable({
172 "aaSorting" : sortColumn,
173 "bPaginate" : false,
174 "bLengthChange": true,
175 "bFilter" : false,
176 "bInfo" : false,
177 "asStripClasses" : [ "odd-row", "even-row" ],
178 "bAutoWidth" : false,
179 "aoColumns" : columns,
180 "bSort" : sortEnabled,
181 "oLanguage":{"sEmptyTable" : noRecordFoundMsg,
182 "sZeroRecords" : noRecordFoundMsg }
183 });
184 }
185 var object;
186
187 });
188 });
189
190 //plugin to sort on currency
191 var symbol = "{/literal}{$config->defaultCurrencySymbol($config->defaultSymbol)}{literal}";
192 cj.fn.dataTableExt.oSort['currency-asc'] = function(a,b) {
193 var x = (a == "-") ? 0 : a.replace( symbol, "" );
194 var y = (b == "-") ? 0 : b.replace( symbol, "" );
195 x = parseFloat( x );
196 y = parseFloat( y );
197 return ((x < y) ? -1 : ((x > y) ? 1 : 0));
198 };
199
200 cj.fn.dataTableExt.oSort['currency-desc'] = function(a,b) {
201 var x = (a == "-") ? 0 : a.replace( symbol, "" );
202 var y = (b == "-") ? 0 : b.replace( symbol, "" );
203 x = parseFloat( x );
204 y = parseFloat( y );
205 return ((x < y) ? 1 : ((x > y) ? -1 : 0));
206 };
207 </script>
208 {/literal}