3 [% USE TablesSettings %]
4 <!-- columns_settings.inc -->
7 function KohaTable(id_selector, dt_parameters, table_settings, add_filters) {
10 var included_ids = [];
11 var selector = '#' + id_selector;
13 if ( table_settings ) {
14 var columns_settings = table_settings['columns'];
16 $(columns_settings).each( function() {
17 var named_id = $( 'thead th[data-colname="' + this.columnname + '"]', selector ).index( selector + ' th' );
18 var used_id = dt_parameters.bKohaColumnsUseNames ? named_id : counter;
19 if ( used_id == -1 ) return;
21 if ( this['is_hidden'] == "1" ) {
22 hidden_ids.push( used_id );
24 if ( this['cannot_be_toggled'] == "0" ) {
25 included_ids.push( used_id );
31 var exportColumns = ":visible:not(.noExport)";
32 var exportRows = ":visible:not(.noExport)";
33 if( dt_parameters.hasOwnProperty("exportColumns") ){
34 // A custom buttons configuration has been passed from the page
35 exportColumns = dt_parameters["exportColumns"];
39 body: function ( data, row, column, node ) {
40 var newnode = $(node);
42 if ( newnode.find(".noExport").length > 0 ) {
43 newnode = newnode.clone();
44 newnode.find(".noExport").remove();
47 return newnode.text().replace( /\n/g, ' ' ).trim();
51 var export_numeric = {
52 body: function ( data, row, column, node ) {
53 var newnode = $(node);
55 if ( newnode.find(".noExport").length > 0 ) {
56 newnode = newnode.clone();
57 newnode.find(".noExport").remove();
59 let tp = newnode.text().replace( /\n/g, ' ' ).trim();
60 tp = $('<p>' + tp + '</p>').text();
61 return $.isNumeric(tp.replace(',', '.')) ? tp.replace(',', '.') : tp;
65 var export_buttons = [
70 columns: exportColumns,
79 columns: exportColumns,
88 columns: exportColumns,
95 [% IF Koha.Preference("CurrencyFormat") != 'FR' %]
96 export_buttons.unshift (
101 columns: exportColumns,
103 format: export_format
108 export_buttons.unshift (
110 extend: 'excelHtml5',
113 columns: exportColumns,
115 format: export_numeric
121 dt_parameters[ "buttons" ] = [
124 className: "dt_button_clear_filter",
125 titleAttr: _("Clear filter"),
127 text: '<i class="fa fa-lg fa-remove"></i> <span class="dt-button-text">' + _("Clear filter") + '</span>',
128 action: function ( e, dt, node, config ) {
129 dt.search( "" ).draw("page");
130 node.addClass("disabled");
135 if( included_ids.length > 0 ){
136 dt_parameters[ "buttons" ].push(
140 columns: included_ids,
141 className: "columns_controls",
142 titleAttr: _("Columns settings"),
143 text: '<i class="fa fa-lg fa-gear"></i> <span class="dt-button-text">' + _("Columns") + '</span>',
145 columns: exportColumns,
152 dt_parameters[ "buttons" ].push(
154 extend: 'collection',
157 className: "export_controls",
158 titleAttr: _("Export or print"),
159 text: '<i class="fa fa-lg fa-download"></i> <span class="dt-button-text">' + _("Export") + '</span>',
160 buttons: export_buttons
164 var table = $(selector);
166 // Duplicate the table header row for columnFilter
167 thead_row = table.find('thead tr');
168 clone = thead_row.clone().addClass('filters_row');
169 clone.find("th.NoSort").html('');
170 thead_row.before(clone);
173 var new_parameters = {}
174 $.extend(true, new_parameters, dataTablesDefaults, dt_parameters);
175 var default_column_defs = [
176 { "targets": [ "title-string" ], "type": "title-string" },
177 { "targets": [ "string-sort" ], "type": "string" },
178 { "targets": [ "anti-the" ], "type": "anti-the" },
179 { "targets": [ "NoSort" ], "orderable": false, "searchable": false },
180 { "targets": [ "NoVisible" ], "visible": false }
182 if ( new_parameters["aoColumnDefs"] === undefined ) {
183 new_parameters["aoColumnDefs"] = default_column_defs;
185 $(default_column_defs).each(function(){
186 new_parameters["aoColumnDefs"].push(this);
190 if ( table_settings ) {
191 if ( table_settings.hasOwnProperty('default_display_length') && table_settings['default_display_length'] != null ) {
192 new_parameters["pageLength"] = table_settings['default_display_length'];
194 if ( table_settings.hasOwnProperty('default_sort_order') && table_settings['default_sort_order'] != null ) {
195 new_parameters["order"] = [[ table_settings['default_sort_order'], 'asc' ]];
199 table.dataTable(new_parameters);
200 table.DataTable().on("column-visibility.dt", function(){
201 if( typeof columnsInit == 'function' ){
202 // This function can be created separately and used to trigger
203 // an event after the DataTable has loaded AND column visibility
204 // has been updated according to the table's configuration
207 }).columns( hidden_ids ).visible( false );
210 // show a link to activate filtering
213 .attr('id', id_selector + '_activate_filters');
214 $("." + id_selector + "_table_controls").prepend(link);
215 deactivate_filters(id_selector);
218 $(".dt_button_clear_filter, .columns_controls, .export_controls").tooltip();
224 /* get_columns_saved_state checks for a DataTables configuration saved
225 * in the browser's local storage. If it is present, the columns
226 * configuration supplied by Koha is overwritten
228 * It takes two parameters:
229 * - localstorage_config, the DataTables saved state object from local storage
230 * - columns_settings, the columns settings object supplied by the template
234 * var columns_settings = [% ColumnsSettings.GetColumns( 'module', 'page', 'table', 'json' ) | $raw %];
235 * var saved_table = localStorage.getItem("DataTables_TABLE_ID_/cgi-bin/koha/PATH/TO/SCRIPT.pl");
236 * var updated_settings = get_columns_saved_state( saved_table, columns_settings );
238 * KohaTable("TABLE_ID", {
240 * }, updated_settings);
243 function get_columns_saved_state( localstorage_config, columns_settings ){
244 var tables = JSON.parse( localstorage_config );
245 // if a table configuration was found in local storage, parse it
247 var stateSave_column_visibility = [];
248 $(tables.columns).each(function(){
249 stateSave_column_visibility.push( this.visible === true ? 0 : 1 );
251 $.each( columns_settings, function( index, key ){
252 if( stateSave_column_visibility[ index ] !== columns_settings[key] ){
253 columns_settings[ index ].is_hidden = stateSave_column_visibility[ index ];
256 return columns_settings;
258 return columns_settings;
263 <!-- / columns_settings.inc -->