1 // These default options are for translation but can be used
2 // for any other datatables settings
3 // MSG_DT_* variables comes from datatables.inc
5 // $("#table_id").dataTable($.extend(true, {}, dataTableDefaults, {
8 var dataTablesDefaults = {
11 "first" : window.MSG_DT_FIRST || "First",
12 "last" : window.MSG_DT_LAST || "Last",
13 "next" : window.MSG_DT_NEXT || "Next",
14 "previous" : window.MSG_DT_PREVIOUS || "Previous"
16 "emptyTable" : window.MSG_DT_EMPTY_TABLE || "No data available in table",
17 "info" : window.MSG_DT_INFO || "Showing _START_ to _END_ of _TOTAL_ entries",
18 "infoEmpty" : window.MSG_DT_INFO_EMPTY || "No entries to show",
19 "infoFiltered" : window.MSG_DT_INFO_FILTERED || "(filtered from _MAX_ total entries)",
20 "lengthMenu" : window.MSG_DT_LENGTH_MENU || "Show _MENU_ entries",
21 "loadingRecords" : window.MSG_DT_LOADING_RECORDS || "Loading...",
22 "processing" : window.MSG_DT_PROCESSING || "Processing...",
23 "search" : window.MSG_DT_SEARCH || "Search:",
24 "zeroRecords" : window.MSG_DT_ZERO_RECORDS || "No matching records found"
26 // "sorting": [$(" - select row position of th -")],
29 // "fnHeaderCallback": function() {
30 // return $('th.sorting.nosort,th.sorting_desc.nosort,th.sorting_asc.nosort').removeClass("sorting sorting_desc sorting_asc").unbind("click");
34 /* Plugin to allow sorting on data stored in a span's title attribute
36 * Ex: <td><span title="[% ISO_date %]">[% formatted_date %]</span></td>
38 * In DataTables config:
40 * { "type": "title-string" },
42 * http://datatables.net/plug-ins/sorting#hidden_title_string
44 jQuery.extend( jQuery.fn.dataTableExt.sort, {
45 "title-string-pre": function ( a ) {
46 return a.match(/title="(.*?)"/)[1].toLowerCase();
49 "title-string-asc": function ( a, b ) {
50 return ((a < b) ? -1 : ((a > b) ? 1 : 0));
53 "title-string-desc": function ( a, b ) {
54 return ((a < b) ? 1 : ((a > b) ? -1 : 0));
58 /* Plugin to allow sorting numerically on data stored in a span's title attribute
60 * Ex: <td><span title="[% total %]">Total: [% total %]</span></td>
62 * In DataTables config:
64 * { "type": "title-numeric" }
66 * http://legacy.datatables.net/plug-ins/sorting#hidden_title
68 jQuery.extend( jQuery.fn.dataTableExt.sort, {
69 "title-numeric-pre": function ( a ) {
70 var x = a.match(/title="*(-?[0-9\.]+)/)[1];
71 return parseFloat( x );
74 "title-numeric-asc": function ( a, b ) {
75 return ((a < b) ? -1 : ((a > b) ? 1 : 0));
78 "title-numeric-desc": function ( a, b ) {
79 return ((a < b) ? 1 : ((a > b) ? -1 : 0));
85 /* Plugin to allow text sorting to ignore articles
87 * In DataTables config:
89 * { "type": "anti-the" },
91 * Based on the plugin found here:
92 * http://datatables.net/plug-ins/sorting#anti_the
93 * Modified to exclude HTML tags from sorting
94 * Extended to accept a string of space-separated articles
95 * from a configuration file (in English, "a," "an," and "the")
98 if(CONFIG_EXCLUDE_ARTICLES_FROM_SORT){
99 var articles = CONFIG_EXCLUDE_ARTICLES_FROM_SORT.split(" ");
101 for(i=0;i<articles.length;i++){
102 rpattern += "^" + articles[i] + " ";
103 if(i < articles.length - 1){ rpattern += "|"; }
105 var re = new RegExp(rpattern, "i");
108 jQuery.extend( jQuery.fn.dataTableExt.sort, {
109 "anti-the-pre": function ( a ) {
110 var x = String(a).replace( /<[\s\S]*?>/g, "" );
112 var z = y.replace(re, "").toLowerCase();
116 "anti-the-asc": function ( a, b ) {
117 return ((a < b) ? -1 : ((a > b) ? 1 : 0));
120 "anti-the-desc": function ( a, b ) {
121 return ((a < b) ? 1 : ((a > b) ? -1 : 0));
127 jQuery.fn.dataTable.ext.errMode = function(settings, note, message) {
128 console.warn(message);
133 $.fn.api = function(options) {
136 if(!options.criteria || ['contains', 'starts_with', 'ends_with', 'exact'].indexOf(options.criteria.toLowerCase()) === -1) options.criteria = 'contains';
137 options.criteria = options.criteria.toLowerCase();
138 settings = $.extend(true, {}, dataTablesDefaults, {
143 'pagingType': 'full',
148 'beforeSend': function(xhr, settings) {
151 xhr.setRequestHeader('x-koha-embed', Array.isArray(options.embed)?options.embed.join(','):options.embed);
153 if(options.header_filter && options.query_parameters) {
154 xhr.setRequestHeader('x-koha-query', options.query_parameters);
155 delete options.query_parameters;
158 'dataFilter': function(data, type) {
159 var json = {data: JSON.parse(data)};
160 if(total = this._xhr.getResponseHeader('x-total-count')) {
161 json.recordsTotal = total;
162 json.recordsFiltered = total;
164 return JSON.stringify(json);
166 'data': function( data, settings ) {
167 var length = data.length;
168 var start = data.start;
171 _page: Math.floor(start/length) + 1,
175 var filter = data.search.value;
176 var query_parameters = settings.aoColumns
177 .filter(function(col) {
178 return col.bSearchable && typeof col.data == 'string' && (data.columns[col.idx].search.value != '' || filter != '')
182 var value = data.columns[col.idx].search.value != '' ? data.columns[col.idx].search.value : filter;
183 part[!col.data.includes('.')?'me.'+col.data:col.data] = options.criteria === 'exact'?value:{like: (['contains', 'ends_with'].indexOf(options.criteria) !== -1?'%':'')+value+(['contains', 'starts_with'].indexOf(options.criteria) !== -1?'%':'')};
187 if(query_parameters.length) {
188 query_parameters = JSON.stringify(query_parameters.length === 1?query_parameters[0]:query_parameters);
189 if(options.header_filter) {
190 options.query_parameters = query_parameters;
192 dataSet.q = query_parameters;
193 delete options.query_parameters;
196 delete options.query_parameters;
199 dataSet._match = options.criteria;
201 if(options.columns) {
202 var order = data.order;
203 order.forEach(function (e,i) {
204 var order_col = e.column;
205 var order_by = options.columns[order_col].data;
206 var order_dir = e.dir == 'asc' ? '+' : '-';
207 dataSet._order_by = order_dir + (!order_by.includes('.')?'me.'+order_by:order_by);
216 return $(this).dataTable(settings);