|
Lines 1-157
Link Here
|
| 1 |
[% USE TablesSettings %] |
|
|
| 2 |
|
| 3 |
<script> |
| 4 |
|
| 5 |
function _dt_visibility(table_settings, table_dt){ |
| 6 |
let hidden_ids = []; |
| 7 |
if ( table_settings ) { |
| 8 |
var columns_settings = table_settings.columns; |
| 9 |
let i = 0; |
| 10 |
let use_names = $(table_dt.table().node()).data('bKohaColumnsUseNames'); |
| 11 |
if ( use_names ) { |
| 12 |
let hidden_columns = table_settings.columns.filter(c => c.is_hidden); |
| 13 |
if (!hidden_columns.length) return []; |
| 14 |
table_dt.columns(hidden_columns.map(c => "[data-colname='%s']".format(c.columnname)).join(',')).every(function(){ |
| 15 |
hidden_ids.push(this.index()); |
| 16 |
}); |
| 17 |
} else { |
| 18 |
$(columns_settings).each( function(i, c) { |
| 19 |
if ( c.is_hidden == '1' ) { |
| 20 |
hidden_ids.push(i); |
| 21 |
} |
| 22 |
}); |
| 23 |
} |
| 24 |
} |
| 25 |
return hidden_ids; |
| 26 |
} |
| 27 |
|
| 28 |
function KohaTable(selector, dt_parameters, table_settings) { |
| 29 |
|
| 30 |
// By default we include all visible columns in exports and print unless they have the "noExport" class |
| 31 |
var exportColumns = ":visible:not(.noExport)"; |
| 32 |
if( dt_parameters.hasOwnProperty("exportColumns") ){ |
| 33 |
// A custom buttons configuration has been passed from the page |
| 34 |
exportColumns = dt_parameters["exportColumns"]; |
| 35 |
} |
| 36 |
// Data which has the "noExport" class should not appear in print or export |
| 37 |
var export_format = { |
| 38 |
body: function ( data, row, column, node ) { |
| 39 |
var newnode = $(node); |
| 40 |
|
| 41 |
if ( newnode.find(".noExport").length > 0 ) { |
| 42 |
newnode = newnode.clone(); |
| 43 |
newnode.find(".noExport").remove(); |
| 44 |
} |
| 45 |
|
| 46 |
return newnode.text().replace( /\n/g, ' ' ).trim(); |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
// Add a "Clear filter" button to table filter form field |
| 51 |
dt_parameters[ "buttons" ] = [ |
| 52 |
{ |
| 53 |
fade: 100, |
| 54 |
className: "dt_button_clear_filter", |
| 55 |
titleAttr: _("Clear filter"), |
| 56 |
enabled: false, |
| 57 |
text: '<i class="fa fa-lg fa-times" aria-hidden="true"></i> <span class="dt-button-text">' + _("Clear filter") + '</span>', |
| 58 |
action: function ( e, dt, node, config ) { |
| 59 |
dt.search( "" ).draw("page"); |
| 60 |
node.addClass("disabled"); |
| 61 |
} |
| 62 |
}, |
| 63 |
{ |
| 64 |
extend: 'csvHtml5', |
| 65 |
text: _("CSV"), |
| 66 |
exportOptions: { |
| 67 |
columns: exportColumns, |
| 68 |
format: export_format |
| 69 |
}, |
| 70 |
}, |
| 71 |
{ |
| 72 |
extend: 'copyHtml5', |
| 73 |
text: _("Copy"), |
| 74 |
exportOptions: { |
| 75 |
columns: exportColumns, |
| 76 |
format: export_format |
| 77 |
}, |
| 78 |
}, |
| 79 |
{ |
| 80 |
extend: 'print', |
| 81 |
text: _("Print"), |
| 82 |
exportOptions: { |
| 83 |
columns: exportColumns, |
| 84 |
format: export_format |
| 85 |
}, |
| 86 |
} |
| 87 |
]; |
| 88 |
|
| 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 ){ |
| 109 |
dt_parameters[ "buttons" ].push( |
| 110 |
{ |
| 111 |
extend: 'colvis', |
| 112 |
fade: 100, |
| 113 |
columns: included_columns, |
| 114 |
className: "columns_controls", |
| 115 |
titleAttr: _("Columns settings"), |
| 116 |
text: '<i class="fa fa-lg fa-gear" aria-hidden="true"></i> <span class="dt-button-text">' + _("Columns") + '</span>', |
| 117 |
exportOptions: { |
| 118 |
columns: exportColumns |
| 119 |
} |
| 120 |
} |
| 121 |
); |
| 122 |
} |
| 123 |
|
| 124 |
var table = $(selector); |
| 125 |
|
| 126 |
var new_parameters = {} |
| 127 |
$.extend(true, new_parameters, dataTablesDefaults, dt_parameters); |
| 128 |
var default_column_defs = [ |
| 129 |
{ "aTargets": ["string-sort"], "sType": "string" }, |
| 130 |
{ "aTargets": ["anti-the"], "sType": "anti-the" }, |
| 131 |
{ "aTargets": ["NoSort"], "bSortable": false, "bSearchable": false } |
| 132 |
]; |
| 133 |
if (new_parameters["aoColumnDefs"] === undefined) { |
| 134 |
new_parameters["aoColumnDefs"] = default_column_defs; |
| 135 |
} else { |
| 136 |
$.extend(true, new_parameters, default_column_defs); |
| 137 |
} |
| 138 |
|
| 139 |
$(table).data('bKohaColumnsUseNames', dt_parameters.bKohaColumnsUseNames); |
| 140 |
|
| 141 |
table.dataTable(new_parameters); |
| 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 () { |
| 146 |
if (typeof columnsInit == 'function') { |
| 147 |
// This function can be created separately and used to trigger |
| 148 |
// an event after the DataTable has loaded AND column visibility |
| 149 |
// has been updated according to the table's configuration |
| 150 |
columnsInit(); |
| 151 |
} |
| 152 |
}).columns(hidden_ids).visible(false); |
| 153 |
|
| 154 |
return table; |
| 155 |
} |
| 156 |
|
| 157 |
</script> |
| 158 |
- |