View | Details | Raw Unified | Return to bug 38436
Collapse All | Expand All

(-)a/koha-tmpl/opac-tmpl/bootstrap/en/includes/columns_settings.inc (-22 / +52 lines)
Lines 1-24 Link Here
1
[% USE TablesSettings %]
1
[% USE TablesSettings %]
2
2
3
<script>
3
<script>
4
function KohaTable(selector, dt_parameters, columns_settings) {
4
5
    var id = 0;
5
function _dt_visibility(table_settings, table_dt){
6
    var hidden_ids = [];
6
    let hidden_ids = [];
7
    var included_ids = [];
7
    if ( table_settings ) {
8
    $(columns_settings).each( function() {
8
        var columns_settings = table_settings.columns;
9
        var named_id = $( 'thead th[data-colname="' + this.columnname + '"]', selector ).index( selector+' th' );
9
        let i = 0;
10
10
        let use_names = $(table_dt.table().node()).data('bKohaColumnsUseNames');
11
        var used_id = dt_parameters.bKohaColumnsUseNames ? named_id : id;
11
        if ( use_names ) {
12
        if ( used_id == -1 ) return;
12
            let hidden_columns = table_settings.columns.filter(c => c.is_hidden);
13
13
            if (!hidden_columns.length) return [];
14
        if ( this['is_hidden'] == "1" ) {
14
            table_dt.columns(hidden_columns.map(c => "[data-colname='%s']".format(c.columnname)).join(',')).every(function(){
15
            hidden_ids.push( used_id );
15
                hidden_ids.push(this.index());
16
        }
16
            });
17
        if ( this['cannot_be_toggled'] == "0" ) {
17
        } else {
18
            included_ids.push( used_id );
18
            $(columns_settings).each( function(i, c) {
19
                if ( c.is_hidden == '1' ) {
20
                    hidden_ids.push(i);
21
                }
22
            });
19
        }
23
        }
20
        id++;
24
    }
21
    });
25
    return hidden_ids;
26
}
27
28
function KohaTable(selector, dt_parameters, table_settings) {
22
29
23
    // By default we include all visible columns in exports and print unless they have the "noExport" class
30
    // By default we include all visible columns in exports and print unless they have the "noExport" class
24
    var exportColumns = ":visible:not(.noExport)";
31
    var exportColumns = ":visible:not(.noExport)";
Lines 79-90 function KohaTable(selector, dt_parameters, columns_settings) { Link Here
79
        }
86
        }
80
    ];
87
    ];
81
88
82
    if( included_ids.length > 0 ){
89
    // Retrieving bKohaColumnsUseNames from the options passed to the constructor, not DT settings
90
    // But ideally should be retrieved using table.data()
91
    let use_names = dt_parameters.bKohaColumnsUseNames;
92
    let included_columns = [];
93
    if (table_settings) {
94
        if (use_names) {
95
            // bKohaColumnsUseNames is set, identify columns by their data-colname
96
            included_columns = table_settings.columns
97
                .filter(c => !c.cannot_be_toggled)
98
                .map(c => "[data-colname='%s']".format(c.columnname))
99
                .join(",");
100
        } else {
101
            // Not set, columns are ordered the same than in the columns settings
102
            included_columns = table_settings.columns
103
                .map((c, i) => (!c.cannot_be_toggled ? i : null))
104
                .filter(i => i !== null);
105
        }
106
    }
107
108
    if( included_columns.length > 0 ){
83
        dt_parameters[ "buttons" ].push(
109
        dt_parameters[ "buttons" ].push(
84
            {
110
            {
85
                extend: 'colvis',
111
                extend: 'colvis',
86
                fade: 100,
112
                fade: 100,
87
                columns: included_ids,
113
                columns: included_columns,
88
                className: "columns_controls",
114
                className: "columns_controls",
89
                titleAttr: _("Columns settings"),
115
                titleAttr: _("Columns settings"),
90
                text: '<i class="fa fa-lg fa-gear" aria-hidden="true"></i> <span class="dt-button-text">' + _("Columns") + '</span>',
116
                text: '<i class="fa fa-lg fa-gear" aria-hidden="true"></i> <span class="dt-button-text">' + _("Columns") + '</span>',
Lines 96-101 function KohaTable(selector, dt_parameters, columns_settings) { Link Here
96
    }
122
    }
97
123
98
    var table = $(selector);
124
    var table = $(selector);
125
99
    var new_parameters = {}
126
    var new_parameters = {}
100
    $.extend(true, new_parameters, dataTablesDefaults, dt_parameters);
127
    $.extend(true, new_parameters, dataTablesDefaults, dt_parameters);
101
    var default_column_defs = [
128
    var default_column_defs = [
Lines 109-116 function KohaTable(selector, dt_parameters, columns_settings) { Link Here
109
        $.extend(true, new_parameters, default_column_defs);
136
        $.extend(true, new_parameters, default_column_defs);
110
    }
137
    }
111
138
139
    $(table).data('bKohaColumnsUseNames', dt_parameters.bKohaColumnsUseNames);
140
112
    table.dataTable(new_parameters);
141
    table.dataTable(new_parameters);
113
    table.DataTable().on("column-visibility.dt", function () {
142
    var table_dt = table.DataTable();
143
144
    let hidden_ids = _dt_visibility(table_settings, table_dt);
145
    table_dt.on("column-visibility.dt", function () {
114
        if (typeof columnsInit == 'function') {
146
        if (typeof columnsInit == 'function') {
115
            // This function can be created separately and used to trigger
147
            // This function can be created separately and used to trigger
116
            // an event after the DataTable has loaded AND column visibility
148
            // an event after the DataTable has loaded AND column visibility
Lines 119-126 function KohaTable(selector, dt_parameters, columns_settings) { Link Here
119
        }
151
        }
120
    }).columns(hidden_ids).visible(false);
152
    }).columns(hidden_ids).visible(false);
121
153
122
    $(".dt_button_clear_filter, .columns_controls, .export_controls").tooltip();
123
124
    return table;
154
    return table;
125
}
155
}
126
156
(-)a/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-detail.tt (-5 / +5 lines)
Lines 1742-1748 Link Here
1742
                });
1742
                });
1743
            [% END # /IF OpacBrowseResults %]
1743
            [% END # /IF OpacBrowseResults %]
1744
1744
1745
            var columns_settings = [% TablesSettings.GetColumns( 'opac', 'biblio-detail', 'holdingst', 'json' ) | $raw %];
1745
            var table_settings = [% TablesSettings.GetTableSettings( 'opac', 'biblio-detail', 'holdingst', 'json' ) | $raw %];
1746
1746
1747
            KohaTable("#holdingst", {
1747
            KohaTable("#holdingst", {
1748
                dom: '<"clearfix">t',
1748
                dom: '<"clearfix">t',
Lines 1753-1759 Link Here
1753
                "responsive": {
1753
                "responsive": {
1754
                    "details": { "type": 'column', "target": -1 }
1754
                    "details": { "type": 'column', "target": -1 }
1755
                }
1755
                }
1756
            }, columns_settings);
1756
            }, table_settings);
1757
1757
1758
            KohaTable("#otherholdingst", {
1758
            KohaTable("#otherholdingst", {
1759
                dom: '<"clearfix">t',
1759
                dom: '<"clearfix">t',
Lines 1764-1772 Link Here
1764
                "responsive": {
1764
                "responsive": {
1765
                    "details": { "type": 'column', "target": -1 }
1765
                    "details": { "type": 'column', "target": -1 }
1766
                }
1766
                }
1767
            }, columns_settings);
1767
            }, table_settings);
1768
1768
1769
            var serial_column_settings = [% TablesSettings.GetColumns( 'opac', 'biblio-detail', 'subscriptionst', 'json' ) | $raw %];
1769
            var serial_table_settings = [% TablesSettings.GetTableSettings( 'opac', 'biblio-detail', 'subscriptionst', 'json' ) | $raw %];
1770
1770
1771
            KohaTable("#subscriptionst", {
1771
            KohaTable("#subscriptionst", {
1772
                dom: '<"clearfix">t',
1772
                dom: '<"clearfix">t',
Lines 1778-1784 Link Here
1778
                "columnDefs": [
1778
                "columnDefs": [
1779
                    { "className": "dtr-control", "orderable": false, "targets": -1 }
1779
                    { "className": "dtr-control", "orderable": false, "targets": -1 }
1780
                ],
1780
                ],
1781
            }, serial_column_settings);
1781
            }, serial_table_settings);
1782
1782
1783
            var dTables = $("#holdingst,#subscriptionst,#otherholdingst");
1783
            var dTables = $("#holdingst,#subscriptionst,#otherholdingst");
1784
            $('a[data-bs-toggle="tab"]').on('shown.bs.tab', function (event) {
1784
            $('a[data-bs-toggle="tab"]').on('shown.bs.tab', function (event) {
(-)a/koha-tmpl/opac-tmpl/bootstrap/js/datatables.js (-199 lines)
Lines 117-317 $.fn.dataTable.ext.buttons.clearFilter = { Link Here
117
    });
117
    });
118
118
119
}());
119
}());
120
121
(function($) {
122
123
    $.fn.api = function(options, columns_settings, add_filters) {
124
        var settings = null;
125
        if(options) {
126
            if(!options.criteria || ['contains', 'starts_with', 'ends_with', 'exact'].indexOf(options.criteria.toLowerCase()) === -1) options.criteria = 'contains';
127
            options.criteria = options.criteria.toLowerCase();
128
            settings = $.extend(true, {}, dataTablesDefaults, {
129
                        "paging": true,
130
                        'serverSide': true,
131
                        'searching': true,
132
                        'processing': true,
133
                        'language': {
134
                            'emptyTable': (options.emptyTable) ? options.emptyTable : "No data available in table"
135
                        },
136
                        'pagingType': 'full',
137
                        'ajax': {
138
                            'type': 'GET',
139
                            'cache': true,
140
                            'dataSrc': 'data',
141
                            'beforeSend': function(xhr, settings) {
142
                                this._xhr = xhr;
143
                                if(options.embed) {
144
                                    xhr.setRequestHeader('x-koha-embed', Array.isArray(options.embed)?options.embed.join(','):options.embed);
145
                                }
146
                            },
147
                            'dataFilter': function(data, type) {
148
                                var json = {data: JSON.parse(data)};
149
                                if(total = this._xhr.getResponseHeader('x-total-count')) {
150
                                    json.recordsTotal = total;
151
                                    json.recordsFiltered = total;
152
                                }
153
                                if(total = this._xhr.getResponseHeader('x-base-total-count')) {
154
                                    json.recordsTotal = total;
155
                                }
156
                                return JSON.stringify(json);
157
                            },
158
                            'data': function( data, settings ) {
159
                                var length = data.length;
160
                                var start  = data.start;
161
162
                                var dataSet = {
163
                                    _page: Math.floor(start/length) + 1,
164
                                    _per_page: length
165
                                };
166
167
                                var filter = data.search.value;
168
                                var query_parameters = settings.aoColumns
169
                                .filter(function(col) {
170
                                    return col.bSearchable && typeof col.data == 'string' && (data.columns[col.idx].search.value != '' || filter != '')
171
                                })
172
                                .map(function(col) {
173
                                    var part = {};
174
                                    var value = data.columns[col.idx].search.value != '' ? data.columns[col.idx].search.value : filter;
175
                                    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?'%':'')};
176
                                    return part;
177
                                });
178
179
                                if(query_parameters.length) {
180
                                    query_parameters = JSON.stringify(query_parameters.length === 1?query_parameters[0]:query_parameters);
181
                                    dataSet.q = query_parameters;
182
                                }
183
184
                                dataSet._match = options.criteria;
185
186
                                if(options.columns) {
187
                                    var order = data.order;
188
                                    order.forEach(function (e,i) {
189
                                        var order_col      = e.column;
190
                                        var order_by       = options.columns[order_col].data;
191
                                        var order_dir      = e.dir == 'asc' ? '+' : '-';
192
                                        dataSet._order_by = order_dir + (!order_by.includes('.')?'me.'+order_by:order_by);
193
                                    });
194
                                }
195
196
                                return dataSet;
197
                            }
198
                        }
199
                    }, options);
200
        }
201
202
        var counter = 0;
203
        var hidden_ids = [];
204
        var included_ids = [];
205
206
        $(columns_settings).each( function() {
207
            var named_id = $( 'thead th[data-colname="' + this.columnname + '"]', this ).index( 'th' );
208
            var used_id = settings.bKohaColumnsUseNames ? named_id : counter;
209
            if ( used_id == -1 ) return;
210
211
            if ( this['is_hidden'] == "1" ) {
212
                hidden_ids.push( used_id );
213
            }
214
            if ( this['cannot_be_toggled'] == "0" ) {
215
                included_ids.push( used_id );
216
            }
217
            counter++;
218
        });
219
220
        var exportColumns = ":visible:not(.noExport)";
221
        if( settings.hasOwnProperty("exportColumns") ){
222
            // A custom buttons configuration has been passed from the page
223
            exportColumns = settings["exportColumns"];
224
        }
225
226
        var export_format = {
227
            body: function ( data, row, column, node ) {
228
                var newnode = $(node);
229
230
                if ( newnode.find(".noExport").length > 0 ) {
231
                    newnode = newnode.clone();
232
                    newnode.find(".noExport").remove();
233
                }
234
235
                return newnode.text().replace( /\n/g, ' ' ).trim();
236
            }
237
        };
238
239
        var export_buttons = [
240
            {
241
                extend: 'excelHtml5',
242
                exportOptions: {
243
                    columns: exportColumns,
244
                    format:  export_format
245
                },
246
            },
247
            {
248
                extend: 'csvHtml5',
249
                exportOptions: {
250
                    columns: exportColumns,
251
                    format:  export_format
252
                },
253
            },
254
            {
255
                extend: 'copyHtml5',
256
                exportOptions: {
257
                    columns: exportColumns,
258
                    format:  export_format
259
                },
260
            },
261
            {
262
                extend: 'print',
263
                exportOptions: {
264
                    columns: exportColumns,
265
                    format:  export_format
266
                },
267
            }
268
        ];
269
270
        settings[ "buttons" ] = [
271
            {
272
                fade: 100,
273
                className: "dt_button_clear_filter",
274
                titleAttr: __("Clear filter"),
275
                enabled: false,
276
                text: '<i class="fa fa-lg fa-times"></i> <span class="dt-button-text">' + __("Clear filter") + '</span>',
277
                action: function ( e, dt, node ) {
278
                    dt.search( "" ).draw("page");
279
                    node.addClass("disabled");
280
                }
281
            }
282
        ];
283
284
        if( included_ids.length > 0 ){
285
            settings[ "buttons" ].push(
286
                {
287
                    extend: 'colvis',
288
                    fade: 100,
289
                    columns: included_ids,
290
                    className: "columns_controls",
291
                    titleAttr: __("Columns settings"),
292
                    text: '<i class="fa fa-lg fa-gear"></i> <span class="dt-button-text">' + __("Columns") + '</span>',
293
                    exportOptions: {
294
                        columns: exportColumns
295
                    }
296
                }
297
            );
298
        }
299
300
        settings[ "buttons" ].push(
301
            {
302
                extend: 'collection',
303
                autoClose: true,
304
                fade: 100,
305
                className: "export_controls",
306
                titleAttr: __("Export or print"),
307
                text: '<i class="fa fa-lg fa-download"></i> <span class="dt-button-text">' + __("Export") + '</span>',
308
                buttons: export_buttons
309
            }
310
        );
311
312
        $(".dt_button_clear_filter, .columns_controls, .export_controls").tooltip();
313
314
        return $(this).dataTable(settings);
315
    };
316
317
})(jQuery);
318
- 

Return to bug 38436