exclude date improvements
[civicrm-core.git] / templates / CRM / common / jsortable.tpl
CommitLineData
6a488035
TO
1{*
2 +--------------------------------------------------------------------+
819d0d41 3 | CiviCRM version 4.5 |
6a488035 4 +--------------------------------------------------------------------+
819d0d41 5 | Copyright CiviCRM LLC (c) 2004-2014 |
6a488035
TO
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">
ae8f569f 28 CRM.$(function($) {
6a488035 29
ae8f569f
CW
30 function getElementClass(element) {
31 return $(element).attr('class') || '';
32 }
6a488035 33
ae8f569f 34 // fetch the occurrence of element
a44340a5 35 function getRowId(row, str) {
ae8f569f 36 var optionId;
a44340a5
CW
37 $.each(row, function(i, n) {
38 if (str === $(n).attr('class')) {
ae8f569f
CW
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};
ae8f569f
CW
47 var sourceUrl = '';
48 var useClass = 'display';
6a488035 49
a44340a5 50 var tcount = 1;
ae8f569f
CW
51 if ( useAjax ) {
52 {/literal}{if isset($sourceUrl)}sourceUrl = "{$sourceUrl}";{/if}{literal}
53 useClass = 'pagerDisplay';
a44340a5 54 tcount = 5;
ae8f569f
CW
55 }
56
a44340a5
CW
57 var tableId = [], count = 1;
58
ae8f569f
CW
59 //rename id of table with sequence
60 //and create the object for navigation
a44340a5 61 $('table.' + useClass).each(function() {
ae8f569f 62 $(this).attr('id','option' + tcount + count);
a44340a5 63 tableId.push(count);
ae8f569f
CW
64 count++;
65 });
6a488035 66
ae8f569f 67 $.each(tableId, function(i,n){
a44340a5 68 var tabId = '#option' + tcount + n;
ae8f569f 69 //get the object of first tr data row.
a44340a5 70 var tdObject = $(tabId + ' tr:nth(1) td');
ae8f569f
CW
71 var id = -1; var count = 0; var columns=''; var sortColumn = '';
72 //build columns array for sorting or not sorting
73 $(tabId + ' th').each( function( ) {
74 var option = $(this).prop('id').split("_");
6a488035 75 option = ( option.length > 1 ) ? option[1] : option[0];
a44340a5 76 var stype = 'numeric';
6a488035 77 switch( option ) {
ae8f569f
CW
78 case 'sortable':
79 sortColumn += '[' + count + ', "asc" ],';
80 columns += '{"sClass": "'+ getElementClass( this ) +'"},';
6a488035 81 break;
ae8f569f
CW
82 case 'date':
83 stype = 'date';
84 case 'order':
85 if ( $(this).attr('class') == 'sortable' ){
86 sortColumn += '[' + count + ', "asc" ],';
87 }
a44340a5 88 var sortId = getRowId(tdObject, $(this).attr('id') +' hiddenElement' );
ae8f569f 89 columns += '{ "render": function ( data, type, row ) { return "<div style=\'display:none\'>"+ data +"</div>" + row[sortId] ; }, "targets": sortColumn,"bUseRendered": false},';
6a488035 90 break;
ae8f569f
CW
91 case 'nosort':
92 columns += '{ "bSortable": false, "sClass": "'+ getElementClass( this ) +'"},';
6a488035 93 break;
ae8f569f
CW
94 case 'currency':
95 columns += '{ "sType": "currency" },';
6a488035 96 break;
ae8f569f
CW
97 case 'link':
98 columns += '{"sType": "html"},';
6a488035 99 break;
ae8f569f
CW
100 default:
101 if ( $(this).text() ) {
102 columns += '{"sClass": "'+ getElementClass( this ) +'"},';
103 } else {
104 columns += '{ "bSortable": false },';
105 }
6a488035
TO
106 break;
107 }
108 count++;
ae8f569f 109 });
a44340a5
CW
110 // Fixme: this could be done without eval
111 columns = columns.substring(0, columns.length - 1 );
112 sortColumn = sortColumn.substring(0, sortColumn.length - 1 );
113 eval('sortColumn =[' + sortColumn + ']');
114 eval('columns =[' + columns + ']');
6a488035 115
ae8f569f
CW
116 var currTable = $(tabId);
117 if (currTable) {
118 // contains the dataTables master records
119 var s = $(document).dataTableSettings;
120 if (s != 'undefined') {
121 var len = s.length;
122 for (var i=0; i < len; i++) {
123 // if already exists, remove from the array
124 if (s[i].sInstance = tabId) {
125 s.splice(i,1);
126 }
127 }
6a488035 128 }
ae8f569f 129 }
6a488035 130
29bc93ee 131 var noRecordFoundMsg = {/literal}'{ts escape="js"}None found.{/ts}'{literal};
6a488035 132
a44340a5 133 var oTable;
ae8f569f
CW
134 if ( useAjax ) {
135 oTable = $(tabId).dataTable({
a44340a5
CW
136 "bFilter": false,
137 "bAutoWidth": false,
138 "aaSorting": sortColumn,
139 "aoColumns": columns,
ae8f569f 140 "bProcessing": true,
a44340a5
CW
141 "bJQueryUI": true,
142 "asStripClasses": [ "odd-row", "even-row" ],
ae8f569f 143 "sPaginationType": "full_numbers",
a44340a5 144 "sDom": '<"crm-datatable-pager-top"lfp>rt<"crm-datatable-pager-bottom"ip>',
ae8f569f
CW
145 "bServerSide": true,
146 "sAjaxSource": sourceUrl,
634598a1 147 "oLanguage":{
a44340a5
CW
148 "sEmptyTable": noRecordFoundMsg,
149 "sZeroRecords": noRecordFoundMsg
634598a1 150 },
ae8f569f
CW
151 "fnServerData": function ( sSource, aoData, fnCallback ) {
152 $.ajax( {
153 "dataType": 'json',
154 "type": "POST",
155 "url": sSource,
156 "data": aoData,
157 "success": fnCallback
158 });
159 }
160 });
161 } else {
162 oTable = $(tabId).dataTable({
a44340a5
CW
163 "aaSorting": sortColumn,
164 "bPaginate": false,
ae8f569f 165 "bLengthChange": true,
a44340a5
CW
166 "bFilter": false,
167 "bInfo": false,
168 "asStripClasses": [ "odd-row", "even-row" ],
169 "bAutoWidth": false,
170 "aoColumns": columns,
171 "bSort": true,
634598a1 172 "oLanguage":{
a44340a5
CW
173 "sEmptyTable": noRecordFoundMsg,
174 "sZeroRecords": noRecordFoundMsg
634598a1 175 }
ae8f569f
CW
176 });
177 }
6a488035 178 });
ae8f569f 179 });
6a488035 180
ae8f569f 181 //plugin to sort on currency
ae8f569f 182 cj.fn.dataTableExt.oSort['currency-asc'] = function(a,b) {
a44340a5 183 var symbol = "{/literal}{$config->defaultCurrencySymbol($config->defaultSymbol)}{literal}";
ae8f569f
CW
184 var x = (a == "-") ? 0 : a.replace( symbol, "" );
185 var y = (b == "-") ? 0 : b.replace( symbol, "" );
186 x = parseFloat( x );
187 y = parseFloat( y );
188 return ((x < y) ? -1 : ((x > y) ? 1 : 0));
189 };
6a488035 190
ae8f569f 191 cj.fn.dataTableExt.oSort['currency-desc'] = function(a,b) {
a44340a5 192 var symbol = "{/literal}{$config->defaultCurrencySymbol($config->defaultSymbol)}{literal}";
ae8f569f
CW
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 };
6a488035
TO
199</script>
200{/literal}