2 // These default options are for translation but can be used
3 // for any other datatables settings
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 "copyTitle" : window.MSG_DT_COPY_TO_CLIPBOARD || "Copy to clipboard",
27 "copyKeys" : window.MSG_DT_COPY_KEYS || "Press <i>ctrl</i> or <i>⌘</i> + <i>C</i> to copy the table data<br>to your system clipboard.<br><br>To cancel, click this message or press escape.",
29 _: window.MSG_DT_COPIED_ROWS || "Copied %d rows to clipboard",
30 1: window.MSG_DT_COPIED_ONE_ROW || "Copied one row to clipboard",
37 'clearFilter', 'copy', 'csv', 'print'
40 initComplete: function( settings) {
41 var tableId = settings.nTable.id
42 // When the DataTables search function is triggered,
43 // enable or disable the "Clear filter" button based on
44 // the presence of a search string
45 $("#" + tableId ).on( 'search.dt', function ( e, settings ) {
46 if( settings.oPreviousSearch.sSearch == "" ){
47 $("#" + tableId + "_wrapper").find(".dt_button_clear_filter").addClass("disabled");
49 $("#" + tableId + "_wrapper").find(".dt_button_clear_filter").removeClass("disabled");
55 $.fn.dataTable.ext.buttons.clearFilter = {
57 className: "dt_button_clear_filter",
58 titleAttr: window.MSG_CLEAR_FILTER,
60 text: '<i class="fa fa-lg fa-remove"></i> <span class="dt-button-text">' + window.MSG_CLEAR_FILTER + '</span>',
61 available: function ( dt ) {
62 // The "clear filter" button is made available if this test returns true
63 if( dt.settings()[0].aanFeatures.f ){ // aanFeatures.f is null if there is no search form
67 action: function ( e, dt, node ) {
68 dt.search( "" ).draw("page");
69 node.addClass("disabled");
75 /* Plugin to allow text sorting to ignore articles
77 * In DataTables config:
79 * { "sType": "anti-the" },
81 * Based on the plugin found here:
82 * http://datatables.net/plug-ins/sorting#anti_the
83 * Modified to exclude HTML tags from sorting
84 * Extended to accept a string of space-separated articles
85 * from a configuration file (in English, "a," "an," and "the")
88 var config_exclude_articles_from_sort = window.CONFIG_EXCLUDE_ARTICLES_FROM_SORT || "a an the";
89 if (config_exclude_articles_from_sort){
90 var articles = config_exclude_articles_from_sort.split(" ");
92 for( var i=0; i<articles.length; i++){
93 rpattern += "^" + articles[i] + " ";
94 if(i < articles.length - 1){ rpattern += "|"; }
96 var re = new RegExp(rpattern, "i");
99 jQuery.extend( jQuery.fn.dataTableExt.oSort, {
100 "anti-the-pre": function ( a ) {
101 var x = String(a).replace( /<[\s\S]*?>/g, "" );
103 var z = y.replace(re, "").toLowerCase();
107 "anti-the-asc": function ( a, b ) {
108 return ((a < b) ? -1 : ((a > b) ? 1 : 0));
111 "anti-the-desc": function ( a, b ) {
112 return ((a < b) ? 1 : ((a > b) ? -1 : 0));
120 $.fn.api = function(options, columns_settings, add_filters) {
123 if(!options.criteria || ['contains', 'starts_with', 'ends_with', 'exact'].indexOf(options.criteria.toLowerCase()) === -1) options.criteria = 'contains';
124 options.criteria = options.criteria.toLowerCase();
125 settings = $.extend(true, {}, dataTablesDefaults, {
132 'emptyTable': (options.emptyTable) ? options.emptyTable : "No data available in table"
134 'pagingType': 'full',
139 'beforeSend': function(xhr, settings) {
142 xhr.setRequestHeader('x-koha-embed', Array.isArray(options.embed)?options.embed.join(','):options.embed);
144 if(options.header_filter && options.query_parameters) {
145 xhr.setRequestHeader('x-koha-query', options.query_parameters);
146 delete options.query_parameters;
149 'dataFilter': function(data, type) {
150 var json = {data: JSON.parse(data)};
151 if(total = this._xhr.getResponseHeader('x-total-count')) {
152 json.recordsTotal = total;
153 json.recordsFiltered = total;
155 if(total = this._xhr.getResponseHeader('x-base-total-count')) {
156 json.recordsTotal = total;
158 return JSON.stringify(json);
160 'data': function( data, settings ) {
161 var length = data.length;
162 var start = data.start;
165 _page: Math.floor(start/length) + 1,
169 var filter = data.search.value;
170 var query_parameters = settings.aoColumns
171 .filter(function(col) {
172 return col.bSearchable && typeof col.data == 'string' && (data.columns[col.idx].search.value != '' || filter != '')
176 var value = data.columns[col.idx].search.value != '' ? data.columns[col.idx].search.value : filter;
177 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?'%':'')};
181 if(query_parameters.length) {
182 query_parameters = JSON.stringify(query_parameters.length === 1?query_parameters[0]:query_parameters);
183 if(options.header_filter) {
184 options.query_parameters = query_parameters;
186 dataSet.q = query_parameters;
187 delete options.query_parameters;
190 delete options.query_parameters;
193 dataSet._match = options.criteria;
195 if(options.columns) {
196 var order = data.order;
197 order.forEach(function (e,i) {
198 var order_col = e.column;
199 var order_by = options.columns[order_col].data;
200 var order_dir = e.dir == 'asc' ? '+' : '-';
201 dataSet._order_by = order_dir + (!order_by.includes('.')?'me.'+order_by:order_by);
213 var included_ids = [];
215 $(columns_settings).each( function() {
216 var named_id = $( 'thead th[data-colname="' + this.columnname + '"]', this ).index( 'th' );
217 var used_id = settings.bKohaColumnsUseNames ? named_id : counter;
218 if ( used_id == -1 ) return;
220 if ( this['is_hidden'] == "1" ) {
221 hidden_ids.push( used_id );
223 if ( this['cannot_be_toggled'] == "0" ) {
224 included_ids.push( used_id );
229 var exportColumns = ":visible:not(.noExport)";
230 if( settings.hasOwnProperty("exportColumns") ){
231 // A custom buttons configuration has been passed from the page
232 exportColumns = settings["exportColumns"];
235 var export_format = {
236 body: function ( data, row, column, node ) {
237 var newnode = $(node);
239 if ( newnode.find(".noExport").length > 0 ) {
240 newnode = newnode.clone();
241 newnode.find(".noExport").remove();
244 return newnode.text().replace( /\n/g, ' ' ).trim();
248 var export_buttons = [
250 extend: 'excelHtml5',
253 columns: exportColumns,
254 format: export_format
261 columns: exportColumns,
262 format: export_format
269 columns: exportColumns,
270 format: export_format
277 columns: exportColumns,
278 format: export_format
283 settings[ "buttons" ] = [
286 className: "dt_button_clear_filter",
287 titleAttr: __("Clear filter"),
289 text: '<i class="fa fa-lg fa-remove"></i> <span class="dt-button-text">' + __("Clear filter") + '</span>',
290 action: function ( e, dt, node ) {
291 dt.search( "" ).draw("page");
292 node.addClass("disabled");
297 if( included_ids.length > 0 ){
298 settings[ "buttons" ].push(
302 columns: included_ids,
303 className: "columns_controls",
304 titleAttr: __("Columns settings"),
305 text: '<i class="fa fa-lg fa-gear"></i> <span class="dt-button-text">' + __("Columns") + '</span>',
307 columns: exportColumns
313 settings[ "buttons" ].push(
315 extend: 'collection',
318 className: "export_controls",
319 titleAttr: __("Export or print"),
320 text: '<i class="fa fa-lg fa-download"></i> <span class="dt-button-text">' + __("Export") + '</span>',
321 buttons: export_buttons
325 $(".dt_button_clear_filter, .columns_controls, .export_controls").tooltip();
327 return $(this).dataTable(settings);