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 if ( table_settings && CAN_user_parameters_manage_column_config ) {
165 dt_parameters[ "buttons" ].push(
167 className: "dt_button_configure_table",
168 titleAttr: _("Table settings"),
169 text: '<i class="fa fa-lg fa-wrench"></i> <span class="dt-button-text">' + _("Configure") + '</span>',
171 window.location = '/cgi-bin/koha/admin/columns_settings.pl?module=' + table_settings['module'] + '&page=' + table_settings['page'] + '&table=' + table_settings['table'];
177 var table = $(selector);
179 // Duplicate the table header row for columnFilter
180 thead_row = table.find('thead tr');
181 clone = thead_row.clone().addClass('filters_row');
182 clone.find("th.NoSort").html('');
183 thead_row.before(clone);
186 var new_parameters = {}
187 $.extend(true, new_parameters, dataTablesDefaults, dt_parameters);
188 var default_column_defs = [
189 { "targets": [ "title-string" ], "type": "title-string" },
190 { "targets": [ "string-sort" ], "type": "string" },
191 { "targets": [ "anti-the" ], "type": "anti-the" },
192 { "targets": [ "NoSort" ], "orderable": false, "searchable": false },
193 { "targets": [ "NoVisible" ], "visible": false }
195 if ( new_parameters["aoColumnDefs"] === undefined ) {
196 new_parameters["aoColumnDefs"] = default_column_defs;
198 $(default_column_defs).each(function(){
199 new_parameters["aoColumnDefs"].push(this);
203 if ( table_settings ) {
204 if ( table_settings.hasOwnProperty('default_display_length') && table_settings['default_display_length'] != null ) {
205 new_parameters["pageLength"] = table_settings['default_display_length'];
207 if ( table_settings.hasOwnProperty('default_sort_order') && table_settings['default_sort_order'] != null ) {
208 new_parameters["order"] = [[ table_settings['default_sort_order'], 'asc' ]];
212 table.dataTable(new_parameters);
213 table.DataTable().on("column-visibility.dt", function(){
214 if( typeof columnsInit == 'function' ){
215 // This function can be created separately and used to trigger
216 // an event after the DataTable has loaded AND column visibility
217 // has been updated according to the table's configuration
220 }).columns( hidden_ids ).visible( false );
223 // show a link to activate filtering
226 .attr('id', id_selector + '_activate_filters');
227 $("." + id_selector + "_table_controls").prepend(link);
228 deactivate_filters(id_selector);
231 $(".dt_button_clear_filter, .columns_controls, .export_controls").tooltip();
237 /* get_columns_saved_state checks for a DataTables configuration saved
238 * in the browser's local storage. If it is present, the columns
239 * configuration supplied by Koha is overwritten
241 * It takes two parameters:
242 * - localstorage_config, the DataTables saved state object from local storage
243 * - columns_settings, the columns settings object supplied by the template
247 * var columns_settings = [% ColumnsSettings.GetColumns( 'module', 'page', 'table', 'json' ) | $raw %];
248 * var saved_table = localStorage.getItem("DataTables_TABLE_ID_/cgi-bin/koha/PATH/TO/SCRIPT.pl");
249 * var updated_settings = get_columns_saved_state( saved_table, columns_settings );
251 * KohaTable("TABLE_ID", {
253 * }, updated_settings);
256 function get_columns_saved_state( localstorage_config, columns_settings ){
257 var tables = JSON.parse( localstorage_config );
258 // if a table configuration was found in local storage, parse it
260 var stateSave_column_visibility = [];
261 $(tables.columns).each(function(){
262 stateSave_column_visibility.push( this.visible === true ? 0 : 1 );
264 $.each( columns_settings, function( index, key ){
265 if( stateSave_column_visibility[ index ] !== columns_settings[key] ){
266 columns_settings[ index ].is_hidden = stateSave_column_visibility[ index ];
269 return columns_settings;
271 return columns_settings;
276 <!-- / columns_settings.inc -->