From 6f5dc3dc0dd9fa02d6d11e92a6e466e545b8eccc Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Mon, 27 Apr 2020 02:28:59 -0300 Subject: [PATCH] Bug 25287: Add columns_settings capabilities to API datatables wrapper MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit This patch adds the code that is used for handling columns settings on datatables and allows passing the columns_settings information to the API-centric datatable. To test, you need bug 25288, which uses this features. Signed-off-by: Tomas Cohen Arazi Signed-off-by: Frédéric Demians Works with bug 24561. Make working bug 25288 Signed-off-by: Jonathan Druart Signed-off-by: Jonathan Druart --- koha-tmpl/intranet-tmpl/prog/js/datatables.js | 131 +++++++++++++++++++- .../prog => opac-tmpl/bootstrap}/js/datatables.js | 133 ++++++++++++++++++++- 2 files changed, 261 insertions(+), 3 deletions(-) copy koha-tmpl/{intranet-tmpl/prog => opac-tmpl/bootstrap}/js/datatables.js (83%) diff --git a/koha-tmpl/intranet-tmpl/prog/js/datatables.js b/koha-tmpl/intranet-tmpl/prog/js/datatables.js index f1594d4b07..5c8ab9e419 100644 --- a/koha-tmpl/intranet-tmpl/prog/js/datatables.js +++ b/koha-tmpl/intranet-tmpl/prog/js/datatables.js @@ -509,7 +509,7 @@ jQuery.fn.dataTable.ext.errMode = function(settings, note, message) { (function($) { - $.fn.api = function(options) { + $.fn.api = function(options, columns_settings, add_filters) { var settings = null; if(options) { if(!options.criteria || ['contains', 'starts_with', 'ends_with', 'exact'].indexOf(options.criteria.toLowerCase()) === -1) options.criteria = 'contains'; @@ -520,6 +520,10 @@ jQuery.fn.dataTable.ext.errMode = function(settings, note, message) { 'serverSide': true, 'searching': true, 'pagingType': 'full_numbers', + 'processing': true, + 'language': { + 'emptyTable': (options.emptyTable) ? options.emptyTable : _("No data available in table") + }, 'ajax': { 'type': 'GET', 'cache': true, @@ -592,6 +596,131 @@ jQuery.fn.dataTable.ext.errMode = function(settings, note, message) { } }, options); } + + var counter = 0; + var hidden_ids = []; + var included_ids = []; + + $(columns_settings).each( function() { + var named_id = $( 'thead th[data-colname="' + this.columnname + '"]', this ).index( 'th' ); + var used_id = settings.bKohaColumnsUseNames ? named_id : counter; + if ( used_id == -1 ) return; + + if ( this['is_hidden'] == "1" ) { + hidden_ids.push( used_id ); + } + if ( this['cannot_be_toggled'] == "0" ) { + included_ids.push( used_id ); + } + counter++; + }); + + var exportColumns = ":visible:not(.noExport)"; + if( settings.hasOwnProperty("exportColumns") ){ + // A custom buttons configuration has been passed from the page + exportColumns = settings["exportColumns"]; + } + + var export_format = { + body: function ( data, row, column, node ) { + var newnode = $(node); + + if ( newnode.find(".noExport").length > 0 ) { + newnode = newnode.clone(); + newnode.find(".noExport").remove(); + } + + return newnode.text().replace( /\n/g, ' ' ).trim(); + } + } + + var export_buttons = [ + { + extend: 'excelHtml5', + text: _("Excel"), + exportOptions: { + columns: exportColumns, + format: export_format + }, + }, + { + extend: 'csvHtml5', + text: _("CSV"), + exportOptions: { + columns: exportColumns, + format: export_format + }, + }, + { + extend: 'copyHtml5', + text: _("Copy"), + exportOptions: { + columns: exportColumns, + format: export_format + }, + }, + { + extend: 'print', + text: _("Print"), + exportOptions: { + columns: exportColumns, + format: export_format + }, + } + ]; + + settings[ "buttons" ] = [ + { + fade: 100, + className: "dt_button_clear_filter", + titleAttr: _("Clear filter"), + enabled: false, + text: ' ' + _("Clear filter") + '', + action: function ( e, dt, node, config ) { + dt.search( "" ).draw("page"); + node.addClass("disabled"); + } + } + ]; + + if( included_ids.length > 0 ){ + settings[ "buttons" ].push( + { + extend: 'colvis', + fade: 100, + columns: included_ids, + className: "columns_controls", + titleAttr: _("Columns settings"), + text: ' ' + _("Columns") + '', + exportOptions: { + columns: exportColumns + } + } + ); + } + + settings[ "buttons" ].push( + { + extend: 'collection', + autoClose: true, + fade: 100, + className: "export_controls", + titleAttr: _("Export or print"), + text: ' ' + _("Export") + '', + buttons: export_buttons + } + ); + + if ( add_filters ) { + // Duplicate the table header row for columnFilter + thead_row = this.find('thead tr'); + clone = thead_row.clone().addClass('filters_row'); + clone.find("th.NoSort").html(''); + thead_row.before(clone); + } + + $(".dt_button_clear_filter, .columns_controls, .export_controls").tooltip(); + return $(this).dataTable(settings); }; diff --git a/koha-tmpl/intranet-tmpl/prog/js/datatables.js b/koha-tmpl/opac-tmpl/bootstrap/js/datatables.js similarity index 83% copy from koha-tmpl/intranet-tmpl/prog/js/datatables.js copy to koha-tmpl/opac-tmpl/bootstrap/js/datatables.js index f1594d4b07..19f2944dde 100644 --- a/koha-tmpl/intranet-tmpl/prog/js/datatables.js +++ b/koha-tmpl/opac-tmpl/bootstrap/js/datatables.js @@ -509,7 +509,7 @@ jQuery.fn.dataTable.ext.errMode = function(settings, note, message) { (function($) { - $.fn.api = function(options) { + $.fn.api = function(options, columns_settings, add_filters) { var settings = null; if(options) { if(!options.criteria || ['contains', 'starts_with', 'ends_with', 'exact'].indexOf(options.criteria.toLowerCase()) === -1) options.criteria = 'contains'; @@ -519,7 +519,11 @@ jQuery.fn.dataTable.ext.errMode = function(settings, note, message) { "paging": true, 'serverSide': true, 'searching': true, - 'pagingType': 'full_numbers', + 'processing': true, + 'language': { + 'emptyTable': (options.emptyTable) ? options.emptyTable : "No data available in table" + }, + 'pagingType': 'full', 'ajax': { 'type': 'GET', 'cache': true, @@ -592,6 +596,131 @@ jQuery.fn.dataTable.ext.errMode = function(settings, note, message) { } }, options); } + + var counter = 0; + var hidden_ids = []; + var included_ids = []; + + $(columns_settings).each( function() { + var named_id = $( 'thead th[data-colname="' + this.columnname + '"]', this ).index( 'th' ); + var used_id = settings.bKohaColumnsUseNames ? named_id : counter; + if ( used_id == -1 ) return; + + if ( this['is_hidden'] == "1" ) { + hidden_ids.push( used_id ); + } + if ( this['cannot_be_toggled'] == "0" ) { + included_ids.push( used_id ); + } + counter++; + }); + + var exportColumns = ":visible:not(.noExport)"; + if( settings.hasOwnProperty("exportColumns") ){ + // A custom buttons configuration has been passed from the page + exportColumns = settings["exportColumns"]; + } + + var export_format = { + body: function ( data, row, column, node ) { + var newnode = $(node); + + if ( newnode.find(".noExport").length > 0 ) { + newnode = newnode.clone(); + newnode.find(".noExport").remove(); + } + + return newnode.text().replace( /\n/g, ' ' ).trim(); + } + } + + var export_buttons = [ + { + extend: 'excelHtml5', + text: _("Excel"), + exportOptions: { + columns: exportColumns, + format: export_format + }, + }, + { + extend: 'csvHtml5', + text: _("CSV"), + exportOptions: { + columns: exportColumns, + format: export_format + }, + }, + { + extend: 'copyHtml5', + text: _("Copy"), + exportOptions: { + columns: exportColumns, + format: export_format + }, + }, + { + extend: 'print', + text: _("Print"), + exportOptions: { + columns: exportColumns, + format: export_format + }, + } + ]; + + settings[ "buttons" ] = [ + { + fade: 100, + className: "dt_button_clear_filter", + titleAttr: _("Clear filter"), + enabled: false, + text: ' ' + _("Clear filter") + '', + action: function ( e, dt, node, config ) { + dt.search( "" ).draw("page"); + node.addClass("disabled"); + } + } + ]; + + if( included_ids.length > 0 ){ + settings[ "buttons" ].push( + { + extend: 'colvis', + fade: 100, + columns: included_ids, + className: "columns_controls", + titleAttr: _("Columns settings"), + text: ' ' + _("Columns") + '', + exportOptions: { + columns: exportColumns + } + } + ); + } + + settings[ "buttons" ].push( + { + extend: 'collection', + autoClose: true, + fade: 100, + className: "export_controls", + titleAttr: _("Export or print"), + text: ' ' + _("Export") + '', + buttons: export_buttons + } + ); + + if ( add_filters ) { + // Duplicate the table header row for columnFilter + thead_row = this.find('thead tr'); + clone = thead_row.clone().addClass('filters_row'); + clone.find("th.NoSort").html(''); + thead_row.before(clone); + } + + $(".dt_button_clear_filter, .columns_controls, .export_controls").tooltip(); + return $(this).dataTable(settings); }; -- 2.11.4.GIT