|
Line 0
Link Here
|
| 0 |
- |
1 |
/* global dataTablesDefaults columnsInit __ */ |
|
|
2 |
/* exported KohaTable */ |
| 3 |
|
| 4 |
function KohaTable(id_selector, dt_parameters, columns_settings, add_filters) { |
| 5 |
var counter = 0; |
| 6 |
var hidden_ids = []; |
| 7 |
var included_ids = []; |
| 8 |
var selector = '#' + id_selector; |
| 9 |
|
| 10 |
$(columns_settings).each(function () { |
| 11 |
var named_id = $('thead th[data-colname="' + this.columnname + '"]', selector).index(selector + ' th'); |
| 12 |
var used_id = dt_parameters.bKohaColumnsUseNames ? named_id : counter; |
| 13 |
if (used_id == -1) return; |
| 14 |
|
| 15 |
if (this['is_hidden'] == "1") { |
| 16 |
hidden_ids.push(used_id); |
| 17 |
} |
| 18 |
if (this['cannot_be_toggled'] == "0") { |
| 19 |
included_ids.push(used_id); |
| 20 |
} |
| 21 |
counter++; |
| 22 |
}); |
| 23 |
|
| 24 |
var exportColumns = ":visible:not(.noExport)"; |
| 25 |
if (dt_parameters.hasOwnProperty("exportColumns")) { |
| 26 |
// A custom buttons configuration has been passed from the page |
| 27 |
exportColumns = dt_parameters["exportColumns"]; |
| 28 |
} |
| 29 |
|
| 30 |
var export_format = { |
| 31 |
body: function (data, row, column, node) { |
| 32 |
var newnode = $(node); |
| 33 |
|
| 34 |
if (newnode.find(".noExport").length > 0) { |
| 35 |
newnode = newnode.clone(); |
| 36 |
newnode.find(".noExport").remove(); |
| 37 |
} |
| 38 |
|
| 39 |
return newnode.text().replace(/\n/g, ' ').trim(); |
| 40 |
} |
| 41 |
}; |
| 42 |
|
| 43 |
var export_buttons = [ |
| 44 |
{ |
| 45 |
extend: 'excelHtml5', |
| 46 |
text: __("Excel"), |
| 47 |
exportOptions: { |
| 48 |
columns: exportColumns, |
| 49 |
format: export_format |
| 50 |
}, |
| 51 |
}, |
| 52 |
{ |
| 53 |
extend: 'csvHtml5', |
| 54 |
text: __("CSV"), |
| 55 |
exportOptions: { |
| 56 |
columns: exportColumns, |
| 57 |
format: export_format |
| 58 |
}, |
| 59 |
}, |
| 60 |
{ |
| 61 |
extend: 'copyHtml5', |
| 62 |
text: __("Copy"), |
| 63 |
exportOptions: { |
| 64 |
columns: exportColumns, |
| 65 |
format: export_format |
| 66 |
}, |
| 67 |
}, |
| 68 |
{ |
| 69 |
extend: 'print', |
| 70 |
text: __("Print"), |
| 71 |
exportOptions: { |
| 72 |
columns: exportColumns, |
| 73 |
format: export_format |
| 74 |
}, |
| 75 |
} |
| 76 |
]; |
| 77 |
|
| 78 |
dt_parameters["buttons"] = [ |
| 79 |
{ |
| 80 |
fade: 100, |
| 81 |
className: "dt_button_clear_filter", |
| 82 |
titleAttr: __("Clear filter"), |
| 83 |
enabled: false, |
| 84 |
text: '<i class="fa fa-lg fa-remove"></i> <span class="dt-button-text">' + __("Clear filter") + '</span>', |
| 85 |
action: function (e, dt, node) { |
| 86 |
dt.search("").draw("page"); |
| 87 |
node.addClass("disabled"); |
| 88 |
} |
| 89 |
} |
| 90 |
]; |
| 91 |
|
| 92 |
if (included_ids.length > 0) { |
| 93 |
dt_parameters["buttons"].push( |
| 94 |
{ |
| 95 |
extend: 'colvis', |
| 96 |
fade: 100, |
| 97 |
columns: included_ids, |
| 98 |
className: "columns_controls", |
| 99 |
titleAttr: __("Columns settings"), |
| 100 |
text: '<i class="fa fa-lg fa-gear"></i> <span class="dt-button-text">' + __("Columns") + '</span>', |
| 101 |
exportOptions: { |
| 102 |
columns: exportColumns |
| 103 |
} |
| 104 |
} |
| 105 |
); |
| 106 |
} |
| 107 |
|
| 108 |
dt_parameters["buttons"].push( |
| 109 |
{ |
| 110 |
extend: 'collection', |
| 111 |
autoClose: true, |
| 112 |
fade: 100, |
| 113 |
className: "export_controls", |
| 114 |
titleAttr: __("Export or print"), |
| 115 |
text: '<i class="fa fa-lg fa-download"></i> <span class="dt-button-text">' + __("Export") + '</span>', |
| 116 |
buttons: export_buttons |
| 117 |
} |
| 118 |
); |
| 119 |
|
| 120 |
var table = $(selector); |
| 121 |
if (add_filters) { |
| 122 |
// Duplicate the table header row for columnFilter |
| 123 |
var thead_row = table.find('thead tr'); |
| 124 |
var clone = thead_row.clone().addClass('filters_row'); |
| 125 |
clone.find("th.NoSort").html(''); |
| 126 |
thead_row.before(clone); |
| 127 |
} |
| 128 |
|
| 129 |
var new_parameters = {}; |
| 130 |
$.extend(true, new_parameters, dataTablesDefaults, dt_parameters); |
| 131 |
var default_column_defs = [ |
| 132 |
{ "targets": ["title-string"], "type": "title-string" }, |
| 133 |
{ "targets": ["string-sort"], "type": "string" }, |
| 134 |
{ "targets": ["anti-the"], "type": "anti-the" }, |
| 135 |
{ "targets": ["NoSort"], "orderable": false, "searchable": false }, |
| 136 |
]; |
| 137 |
if (new_parameters["columnDefs"] === undefined) { |
| 138 |
new_parameters["columnDefs"] = default_column_defs; |
| 139 |
} else { |
| 140 |
$.extend(true, new_parameters, default_column_defs); |
| 141 |
} |
| 142 |
|
| 143 |
table.dataTable(new_parameters); |
| 144 |
table.DataTable().on("column-visibility.dt", function () { |
| 145 |
if (typeof columnsInit == 'function') { |
| 146 |
// This function can be created separately and used to trigger |
| 147 |
// an event after the DataTable has loaded AND column visibility |
| 148 |
// has been updated according to the table's configuration |
| 149 |
columnsInit(); |
| 150 |
} |
| 151 |
}).columns(hidden_ids).visible(false); |
| 152 |
|
| 153 |
if (add_filters) { |
| 154 |
// show a link to activate filtering |
| 155 |
var link = $('<a>') |
| 156 |
.attr('href', '#') |
| 157 |
.attr('id', id_selector + '_activate_filters'); |
| 158 |
$("." + id_selector + "_table_controls").prepend(link); |
| 159 |
deactivate_filters(id_selector); |
| 160 |
} |
| 161 |
|
| 162 |
$(".dt_button_clear_filter, .columns_controls, .export_controls").tooltip(); |
| 163 |
|
| 164 |
return table; |
| 165 |
} |
| 166 |
|
| 167 |
function activate_filters(id) { |
| 168 |
var table = $("#" + id); |
| 169 |
if (table.length == 1) { |
| 170 |
var filters_row = table.find('thead tr.filters_row'); |
| 171 |
|
| 172 |
var aoColumns = []; |
| 173 |
filters_row.find('th').each(function () { |
| 174 |
if (this.className === "NoSort") { |
| 175 |
aoColumns.push(null); |
| 176 |
} else { |
| 177 |
aoColumns.push('text'); |
| 178 |
} |
| 179 |
}); |
| 180 |
|
| 181 |
if (table.find('thead tr.columnFilter').length == 0) { |
| 182 |
table.dataTable().columnFilter({ |
| 183 |
'sPlaceHolder': 'head:after' |
| 184 |
, 'aoColumns': aoColumns |
| 185 |
}); |
| 186 |
filters_row.addClass('columnFilter'); |
| 187 |
} |
| 188 |
filters_row.show(); |
| 189 |
} |
| 190 |
|
| 191 |
$('#' + id + '_activate_filters') |
| 192 |
.html('<i class="fa fa-filter"></i> ' + __('Deactivate filters')) |
| 193 |
.unbind('click') |
| 194 |
.click(function () { |
| 195 |
deactivate_filters(id); |
| 196 |
return false; |
| 197 |
}); |
| 198 |
} |
| 199 |
|
| 200 |
function deactivate_filters(id) { |
| 201 |
var filters_row = $("#" + id).find('thead tr.filters_row'); |
| 202 |
|
| 203 |
filters_row.find('input[type="text"]') |
| 204 |
.val('') // Empty filter text boxes |
| 205 |
.trigger('keyup') // Filter (display all rows) |
| 206 |
.trigger('blur'); // Reset value to the column name |
| 207 |
filters_row.hide(); |
| 208 |
|
| 209 |
$('#' + id + '_activate_filters') |
| 210 |
.html('<i class="fa fa-filter"></i> ' + __('Activate filters')) |
| 211 |
.unbind('click') |
| 212 |
.click(function () { |
| 213 |
activate_filters(id); |
| 214 |
return false; |
| 215 |
}); |
| 216 |
} |