1 // These default options are for translation but can be used
2 // for any other datatables settings
3 // MSG_DT_* variables comes from datatables-strings.inc
5 // $("#table_id").dataTable($.extend(true, {}, dataTableDefaults, {
8 var dataTablesDefaults = {
11 "sFirst" : window.MSG_DT_FIRST || "First",
12 "sLast" : window.MSG_DT_LAST || "Last",
13 "sNext" : window.MSG_DT_NEXT || "Next",
14 "sPrevious" : window.MSG_DT_PREVIOUS || "Previous"
16 "sEmptyTable" : window.MSG_DT_EMPTY_TABLE || "No data available in table",
17 "sInfo" : window.MSG_DT_INFO || "Showing _START_ to _END_ of _TOTAL_ entries",
18 "sInfoEmpty" : window.MSG_DT_INFO_EMPTY || "No entries to show",
19 "sInfoFiltered" : window.MSG_DT_INFO_FILTERED || "(filtered from _MAX_ total entries)",
20 "sLengthMenu" : window.MSG_DT_LENGTH_MENU || "Show _MENU_ entries",
21 "sLoadingRecords" : window.MSG_DT_LOADING_RECORDS || "Loading...",
22 "sProcessing" : window.MSG_DT_PROCESSING || "Processing...",
23 "sSearch" : window.MSG_DT_SEARCH || "Search:",
24 "sZeroRecords" : window.MSG_DT_ZERO_RECORDS || "No matching records found"
26 "sDom": 'C<"top pager"ilpf>tr<"bottom pager"ip>',
27 "aLengthMenu": [[10, 20, 50, 100, -1], [10, 20, 50, 100, window.MSG_DT_ALL || "All"]],
32 // Return an array of string containing the values of a particular column
33 $.fn.dataTableExt.oApi.fnGetColumnData = function ( oSettings, iColumn, bUnique, bFiltered, bIgnoreEmpty ) {
34 // check that we have a column id
35 if ( typeof iColumn == "undefined" ) return new Array();
36 // by default we only wany unique data
37 if ( typeof bUnique == "undefined" ) bUnique = true;
38 // by default we do want to only look at filtered data
39 if ( typeof bFiltered == "undefined" ) bFiltered = true;
40 // by default we do not wany to include empty values
41 if ( typeof bIgnoreEmpty == "undefined" ) bIgnoreEmpty = true;
42 // list of rows which we're going to loop through
44 // use only filtered rows
45 if (bFiltered == true) aiRows = oSettings.aiDisplay;
47 else aiRows = oSettings.aiDisplayMaster; // all row numbers
50 var asResultData = new Array();
51 for (var i=0,c=aiRows.length; i<c; i++) {
53 var aData = this.fnGetData(iRow);
54 var sValue = aData[iColumn];
55 // ignore empty values?
56 if (bIgnoreEmpty == true && sValue.length == 0) continue;
57 // ignore unique values?
58 else if (bUnique == true && jQuery.inArray(sValue, asResultData) > -1) continue;
59 // else push the value onto the result data array
60 else asResultData.push(sValue);
65 // List of unbind keys (Ctrl, Alt, Direction keys, etc.)
66 // These keys must not launch filtering
67 var blacklist_keys = new Array(0, 16, 17, 18, 37, 38, 39, 40);
69 // Set a filtering delay for global search field
70 jQuery.fn.dataTableExt.oApi.fnSetFilteringDelay = function ( oSettings, iDelay ) {
72 * Inputs: object:oSettings - dataTables settings object - automatically given
73 * integer:iDelay - delay in milliseconds
74 * Usage: $('#example').dataTable().fnSetFilteringDelay(250);
75 * Author: Zygimantas Berziunas (www.zygimantas.com) and Allan Jardine
76 * License: GPL v2 or BSD 3 point style
77 * Contact: zygimantas.berziunas /AT\ hotmail.com
81 iDelay = (typeof iDelay == 'undefined') ? 250 : iDelay;
83 this.each( function ( i ) {
84 $.fn.dataTableExt.iApiIndex = i;
88 sPreviousSearch = null,
89 anControl = $( 'input', _that.fnSettings().aanFeatures.f );
91 anControl.unbind( 'keyup.DT' ).bind( 'keyup.DT', function(event) {
93 if (blacklist_keys.indexOf(event.keyCode) != -1) {
95 }else if ( event.keyCode == '13' ) {
96 $.fn.dataTableExt.iApiIndex = i;
97 _that.fnFilter( $(this).val() );
99 if (sPreviousSearch === null || sPreviousSearch != anControl.val()) {
100 window.clearTimeout(oTimerId);
101 sPreviousSearch = anControl.val();
102 oTimerId = window.setTimeout(function() {
103 $.fn.dataTableExt.iApiIndex = i;
104 _that.fnFilter( anControl.val() );
115 // Add a filtering delay on general search and on all input (with a class 'filter')
116 jQuery.fn.dataTableExt.oApi.fnAddFilters = function ( oSettings, sClass, iDelay ) {
118 this.fnSetFilteringDelay(iDelay);
119 var filterTimerId = null;
120 $(table).find("input."+sClass).keyup(function(event) {
121 if (blacklist_keys.indexOf(event.keyCode) != -1) {
123 }else if ( event.keyCode == '13' ) {
124 table.fnFilter( $(this).val(), $(this).attr('data-column_num') );
126 window.clearTimeout(filterTimerId);
128 filterTimerId = window.setTimeout(function() {
129 table.fnFilter($(input).val(), $(input).attr('data-column_num'));
133 $(table).find("select."+sClass).on('change', function() {
134 table.fnFilter($(this).val(), $(this).attr('data-column_num'));
138 // Useful if you want to filter on dates with 2 inputs (start date and end date)
139 // You have to include calendar.inc to use it
140 function dt_add_rangedate_filter(begindate_id, enddate_id, dateCol) {
141 $.fn.dataTableExt.afnFiltering.push(
142 function( oSettings, aData, iDataIndex ) {
144 var beginDate = Date_from_syspref($("#"+begindate_id).val()).getTime();
145 var endDate = Date_from_syspref($("#"+enddate_id).val()).getTime();
147 var data = Date_from_syspref(aData[dateCol]).getTime();
149 if ( !parseInt(beginDate) && ! parseInt(endDate) ) {
152 else if ( beginDate <= data && !parseInt(endDate) ) {
155 else if ( data <= endDate && !parseInt(beginDate) ) {
158 else if ( beginDate <= data && data <= endDate) {
166 // Sorting on html contains
167 // <a href="foo.pl">bar</a> sort on 'bar'
168 function dt_overwrite_html_sorting_localeCompare() {
169 jQuery.fn.dataTableExt.oSort['html-asc'] = function(a,b) {
170 a = a.replace(/<.*?>/g, "").replace(/\s+/g, " ");
171 b = b.replace(/<.*?>/g, "").replace(/\s+/g, " ");
172 if (typeof(a.localeCompare == "function")) {
173 return a.localeCompare(b);
175 return (a > b) ? 1 : ((a < b) ? -1 : 0);
179 jQuery.fn.dataTableExt.oSort['html-desc'] = function(a,b) {
180 a = a.replace(/<.*?>/g, "").replace(/\s+/g, " ");
181 b = b.replace(/<.*?>/g, "").replace(/\s+/g, " ");
182 if(typeof(b.localeCompare == "function")) {
183 return b.localeCompare(a);
185 return (b > a) ? 1 : ((b < a) ? -1 : 0);
189 jQuery.fn.dataTableExt.oSort['num-html-asc'] = function(a,b) {
190 var x = a.replace( /<.*?>/g, "" );
191 var y = b.replace( /<.*?>/g, "" );
194 return ((x < y) ? -1 : ((x > y) ? 1 : 0));
197 jQuery.fn.dataTableExt.oSort['num-html-desc'] = function(a,b) {
198 var x = a.replace( /<.*?>/g, "" );
199 var y = b.replace( /<.*?>/g, "" );
202 return ((x < y) ? 1 : ((x > y) ? -1 : 0));
206 // Sorting on string without accentued characters
207 function dt_overwrite_string_sorting_localeCompare() {
208 jQuery.fn.dataTableExt.oSort['string-asc'] = function(a,b) {
209 a = a.replace(/<.*?>/g, "").replace(/\s+/g, " ");
210 b = b.replace(/<.*?>/g, "").replace(/\s+/g, " ");
211 if (typeof(a.localeCompare == "function")) {
212 return a.localeCompare(b);
214 return (a > b) ? 1 : ((a < b) ? -1 : 0);
218 jQuery.fn.dataTableExt.oSort['string-desc'] = function(a,b) {
219 a = a.replace(/<.*?>/g, "").replace(/\s+/g, " ");
220 b = b.replace(/<.*?>/g, "").replace(/\s+/g, " ");
221 if(typeof(b.localeCompare == "function")) {
222 return b.localeCompare(a);
224 return (b > a) ? 1 : ((b < a) ? -1 : 0);
229 // Replace a node with a html and js contain.
230 function replace_html( original_node, type ) {
231 switch ( $(original_node).attr('data-type') ) {
233 var id = $(original_node).attr("data-id");
234 var format = $(original_node).attr("data-format");
235 replace_html_date( original_node, id, format );
238 alert(_("This node can't be replaced"));
242 // Replace a node with a "From [date] To [date]" element
243 // Used on tfoot > td
244 function replace_html_date( original_node, id, format ) {
245 var node = $('<span style="white-space:nowrap">' + _("From") + '<input type="text" id="' + id + 'from" readonly="readonly" placeholder=\'' + _("Pick date") + '\' size="7" /><a title="' + _("Delete this filter") + '" style="cursor:pointer" onclick=\'$("#' + id + 'from").val("").change();\' >×</a></span><br/><span style="white-space:nowrap">' + _("To") + '<input type="text" id="' + id + 'to" readonly="readonly" placeholder=\'' + _("Pick date") + '\' size="7" /><a title="' + _("Delete this filter") + '" style="cursor:pointer" onclick=\'$("#' + id + 'to").val("").change();\' >×</a></span>');
246 $(original_node).replaceWith(node);
247 var script = document.createElement( 'script' );
248 script.type = 'text/javascript';
249 var script_content = "Calendar.setup({";
250 script_content += " inputField: \"" + id + "from\",";
251 script_content += " ifFormat: \"" + format + "\",";
252 script_content += " button: \"" + id + "from\",";
253 script_content += " onClose: function(){ $(\"#" + id + "from\").change(); this.hide();}";
254 script_content += " });";
255 script_content += " Calendar.setup({";
256 script_content += " inputField: \"" + id + "to\",";
257 script_content += " ifFormat: \"" + format + "\",";
258 script_content += " button: \"" + id + "to\",";
259 script_content += " onClose: function(){ $(\"#" + id + "to\").change(); this.hide();}";
260 script_content += " });";
261 script.text = script_content;
262 $(original_node).append( script );
265 $.fn.dataTableExt.oPagination.four_button = {
267 * Function: oPagination.four_button.fnInit
268 * Purpose: Initalise dom elements required for pagination with a list of the pages
270 * Inputs: object:oSettings - dataTables settings object
271 * node:nPaging - the DIV which contains this pagination control
272 * function:fnCallbackDraw - draw function which must be called on update
274 "fnInit": function ( oSettings, nPaging, fnCallbackDraw )
276 nFirst = document.createElement( 'span' );
277 nPrevious = document.createElement( 'span' );
278 nNext = document.createElement( 'span' );
279 nLast = document.createElement( 'span' );
281 nFirst.appendChild( document.createTextNode( oSettings.oLanguage.oPaginate.sFirst ) );
282 nPrevious.appendChild( document.createTextNode( oSettings.oLanguage.oPaginate.sPrevious ) );
283 nNext.appendChild( document.createTextNode( oSettings.oLanguage.oPaginate.sNext ) );
284 nLast.appendChild( document.createTextNode( oSettings.oLanguage.oPaginate.sLast ) );
286 nFirst.className = "paginate_button first";
287 nPrevious.className = "paginate_button previous";
288 nNext.className="paginate_button next";
289 nLast.className = "paginate_button last";
291 nPaging.appendChild( nFirst );
292 nPaging.appendChild( nPrevious );
293 nPaging.appendChild( nNext );
294 nPaging.appendChild( nLast );
296 $(nFirst).click( function () {
297 oSettings.oApi._fnPageChange( oSettings, "first" );
298 fnCallbackDraw( oSettings );
301 $(nPrevious).click( function() {
302 oSettings.oApi._fnPageChange( oSettings, "previous" );
303 fnCallbackDraw( oSettings );
306 $(nNext).click( function() {
307 oSettings.oApi._fnPageChange( oSettings, "next" );
308 fnCallbackDraw( oSettings );
311 $(nLast).click( function() {
312 oSettings.oApi._fnPageChange( oSettings, "last" );
313 fnCallbackDraw( oSettings );
316 /* Disallow text selection */
317 $(nFirst).bind( 'selectstart', function () { return false; } );
318 $(nPrevious).bind( 'selectstart', function () { return false; } );
319 $(nNext).bind( 'selectstart', function () { return false; } );
320 $(nLast).bind( 'selectstart', function () { return false; } );
324 * Function: oPagination.four_button.fnUpdate
325 * Purpose: Update the list of page buttons shows
327 * Inputs: object:oSettings - dataTables settings object
328 * function:fnCallbackDraw - draw function which must be called on update
330 "fnUpdate": function ( oSettings, fnCallbackDraw )
332 if ( !oSettings.aanFeatures.p )
337 /* Loop over each instance of the pager */
338 var an = oSettings.aanFeatures.p;
339 for ( var i=0, iLen=an.length ; i<iLen ; i++ )
341 var buttons = an[i].getElementsByTagName('span');
342 if ( oSettings._iDisplayStart === 0 )
344 buttons[0].className = "paginate_disabled_previous";
345 buttons[1].className = "paginate_disabled_previous";
349 buttons[0].className = "paginate_enabled_previous";
350 buttons[1].className = "paginate_enabled_previous";
353 if ( oSettings.fnDisplayEnd() == oSettings.fnRecordsDisplay() )
355 buttons[2].className = "paginate_disabled_next";
356 buttons[3].className = "paginate_disabled_next";
360 buttons[2].className = "paginate_enabled_next";
361 buttons[3].className = "paginate_enabled_next";
367 $.fn.dataTableExt.oSort['num-html-asc'] = function(a,b) {
368 var x = a.replace( /<.*?>/g, "" );
369 var y = b.replace( /<.*?>/g, "" );
372 return ((x < y) ? -1 : ((x > y) ? 1 : 0));
375 $.fn.dataTableExt.oSort['num-html-desc'] = function(a,b) {
376 var x = a.replace( /<.*?>/g, "" );
377 var y = b.replace( /<.*?>/g, "" );
380 return ((x < y) ? 1 : ((x > y) ? -1 : 0));
386 * Natural Sort algorithm for Javascript - Version 0.7 - Released under MIT license
387 * Author: Jim Palmer (based on chunking idea from Dave Koelle)
388 * Contributors: Mike Grier (mgrier.com), Clint Priest, Kyle Adams, guillermo
389 * See: http://js-naturalsort.googlecode.com/svn/trunk/naturalSort.js
391 function naturalSort (a, b) {
392 var re = /(^-?[0-9]+(\.?[0-9]*)[df]?e?[0-9]?$|^0x[0-9a-f]+$|[0-9]+)/gi,
393 sre = /(^[ ]*|[ ]*$)/g,
394 dre = /(^([\w ]+,?[\w ]+)?[\w ]+,?[\w ]+\d+:\d+(:\d+)?[\w ]?|^\d{1,4}[\/\-]\d{1,4}[\/\-]\d{1,4}|^\w+, \w+ \d+, \d{4})/,
395 hre = /^0x[0-9a-f]+$/i,
397 // convert all to strings and trim()
398 x = a.toString().replace(sre, '') || '',
399 y = b.toString().replace(sre, '') || '',
401 xN = x.replace(re, '\0$1\0').replace(/\0$/,'').replace(/^\0/,'').split('\0'),
402 yN = y.replace(re, '\0$1\0').replace(/\0$/,'').replace(/^\0/,'').split('\0'),
403 // numeric, hex or date detection
404 xD = parseInt(x.match(hre), 10) || (xN.length != 1 && x.match(dre) && Date.parse(x)),
405 yD = parseInt(y.match(hre), 10) || xD && y.match(dre) && Date.parse(y) || null;
406 // first try and sort Hex codes or Dates
408 if ( xD < yD ) return -1;
409 else if ( xD > yD ) return 1;
410 // natural sorting through split numeric strings and default strings
411 for(var cLoc=0, numS=Math.max(xN.length, yN.length); cLoc < numS; cLoc++) {
412 // find floats not starting with '0', string or 0 if not defined (Clint Priest)
413 var oFxNcL = !(xN[cLoc] || '').match(ore) && parseFloat(xN[cLoc]) || xN[cLoc] || 0;
414 var oFyNcL = !(yN[cLoc] || '').match(ore) && parseFloat(yN[cLoc]) || yN[cLoc] || 0;
415 // handle numeric vs string comparison - number < string - (Kyle Adams)
416 if (isNaN(oFxNcL) !== isNaN(oFyNcL)) return (isNaN(oFxNcL)) ? 1 : -1;
417 // rely on string comparison if different types - i.e. '02' < 2 != '02' < '2'
418 else if (typeof oFxNcL !== typeof oFyNcL) {
422 if (oFxNcL < oFyNcL) return -1;
423 if (oFxNcL > oFyNcL) return 1;
428 jQuery.extend( jQuery.fn.dataTableExt.oSort, {
429 "natural-asc": function ( a, b ) {
430 return naturalSort(a,b);
433 "natural-desc": function ( a, b ) {
434 return naturalSort(a,b) * -1;
440 /* Plugin to allow sorting on data stored in a span's title attribute
442 * Ex: <td><span title="[% ISO_date %]">[% formatted_date %]</span></td>
444 * In DataTables config:
446 * { "sType": "title-string" },
448 * http://datatables.net/plug-ins/sorting#hidden_title_string
450 jQuery.extend( jQuery.fn.dataTableExt.oSort, {
451 "title-string-pre": function ( a ) {
452 return a.match(/title="(.*?)"/)[1].toLowerCase();
455 "title-string-asc": function ( a, b ) {
456 return ((a < b) ? -1 : ((a > b) ? 1 : 0));
459 "title-string-desc": function ( a, b ) {
460 return ((a < b) ? 1 : ((a > b) ? -1 : 0));
464 /* Plugin to allow sorting on numeric data stored in a span's title attribute
466 * Ex: <td><span title="[% decimal_number_that_JS_parseFloat_accepts %]">
467 * [% formatted currency %]
470 * In DataTables config:
472 * { "sType": "title-numeric" },
474 * http://datatables.net/plug-ins/sorting#hidden_title
476 jQuery.extend( jQuery.fn.dataTableExt.oSort, {
477 "title-numeric-pre": function ( a ) {
478 var x = a.match(/title="*(-?[0-9\.]+)/)[1];
479 return parseFloat( x );
482 "title-numeric-asc": function ( a, b ) {
483 return ((a < b) ? -1 : ((a > b) ? 1 : 0));
486 "title-numeric-desc": function ( a, b ) {
487 return ((a < b) ? 1 : ((a > b) ? -1 : 0));
493 /* Plugin to allow text sorting to ignore articles
495 * In DataTables config:
497 * { "sType": "anti-the" },
499 * Based on the plugin found here:
500 * http://datatables.net/plug-ins/sorting#anti_the
501 * Modified to exclude HTML tags from sorting
502 * Extended to accept a string of space-separated articles
503 * from a configuration file (in English, "a," "an," and "the")
506 if(CONFIG_EXCLUDE_ARTICLES_FROM_SORT){
507 var articles = CONFIG_EXCLUDE_ARTICLES_FROM_SORT.split(" ");
509 for(i=0;i<articles.length;i++){
510 rpattern += "^" + articles[i] + " ";
511 if(i < articles.length - 1){ rpattern += "|"; }
513 var re = new RegExp(rpattern, "i");
516 jQuery.extend( jQuery.fn.dataTableExt.oSort, {
517 "anti-the-pre": function ( a ) {
518 var x = String(a).replace( /<[\s\S]*?>/g, "" );
520 var z = y.replace(re, "").toLowerCase();
524 "anti-the-asc": function ( a, b ) {
525 return ((a < b) ? -1 : ((a > b) ? 1 : 0));
528 "anti-the-desc": function ( a, b ) {
529 return ((a < b) ? 1 : ((a > b) ? -1 : 0));
535 // Remove string between NSB NSB characters
536 $.fn.dataTableExt.oSort['nsb-nse-asc'] = function(a,b) {
537 var pattern = new RegExp("\x88.*\x89");
538 a = a.replace(pattern, "");
539 b = b.replace(pattern, "");
540 return (a > b) ? 1 : ((a < b) ? -1 : 0);
542 $.fn.dataTableExt.oSort['nsb-nse-desc'] = function(a,b) {
543 var pattern = new RegExp("\x88.*\x89");
544 a = a.replace(pattern, "");
545 b = b.replace(pattern, "");
546 return (b > a) ? 1 : ((b < a) ? -1 : 0);
549 /* Define two custom functions (asc and desc) for basket callnumber sorting */
550 jQuery.fn.dataTableExt.oSort['callnumbers-asc'] = function(x,y) {
551 var x_array = x.split("<div>");
552 var y_array = y.split("<div>");
554 /* Pop the first elements, they are empty strings */
558 x_array = jQuery.map( x_array, function( a ) {
559 return parse_callnumber( a );
561 y_array = jQuery.map( y_array, function( a ) {
562 return parse_callnumber( a );
571 if ( !x ) { x = ""; }
572 if ( !y ) { y = ""; }
574 return ((x < y) ? -1 : ((x > y) ? 1 : 0));
577 jQuery.fn.dataTableExt.oSort['callnumbers-desc'] = function(x,y) {
578 var x_array = x.split("<div>");
579 var y_array = y.split("<div>");
581 /* Pop the first elements, they are empty strings */
585 x_array = jQuery.map( x_array, function( a ) {
586 return parse_callnumber( a );
588 y_array = jQuery.map( y_array, function( a ) {
589 return parse_callnumber( a );
598 if ( !x ) { x = ""; }
599 if ( !y ) { y = ""; }
601 return ((x < y) ? 1 : ((x > y) ? -1 : 0));
604 function parse_callnumber ( html ) {
605 var array = html.split('<span class="callnumber">');
607 array = array[1].split('</span>');
614 // see http://www.datatables.net/examples/advanced_init/footer_callback.html
615 function footer_column_sum( api, column_numbers ) {
616 // Remove the formatting to get integer data for summation
617 var intVal = function ( i ) {
618 if ( typeof i === 'number' ) {
619 if ( isNaN(i) ) return 0;
621 } else if ( typeof i === 'string' ) {
622 var value = i.replace(/[a-zA-Z ,.]/g, '')*1;
623 if ( isNaN(value) ) return 0;
630 for ( var indice = 0 ; indice < column_numbers.length ; indice++ ) {
631 var column_number = column_numbers[indice];
634 var cells = api.column( column_number, { page: 'current' } ).nodes().to$().find("span.total_amount");
635 $(cells).each(function(){
636 total += intVal( $(this).html() );
638 total /= 100; // Hard-coded decimal precision
641 $( api.column( column_number ).footer() ).html(total.format_price());