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