Column visibility / export options - General structure setup in multi-language environment
Column visibility / export options - General structure setup in multi-language environment
Hello community,
I'm still a beginner and I use datatables in an asp.net project in various views.
Since my data contains some columns that are language based I have created below functions to set the visibility based on the current language, which is retrieved from a cookie.
In total I have three different datatables regarding column quantity.
I moved these functions to my site.js file to call them from the view (see below).
function setEnglishDT(dttable) {
dttable.column(0).visible(true);
dttable.column(1).visible(true);
dttable.column(2).visible(false);
dttable.column(3).visible(false);
}
function setOtherDT(dttable) {
dttable.column(0).visible(false);
dttable.column(1).visible(false);
dttable.column(2).visible(true);
dttable.column(3).visible(true);
}
In every view I manage the basic setup, like column width, filtering etc. and then set the column visibility as explained above.
$(document).ready(function () {
...other stuff...
});
function LoadDataTable() {
var dttable = $('#DataTable').DataTable({
bFilter: true, bInfo: true,
"bAutoWidth": false,
dom: 'Bfrtip',
"aoColumnDefs": [
{ "bSortable": true, "sWidth": "15%", "aTargets": [0] }, // Engl. A
{ "bSortable": true, "sWidth": "15%", "aTargets": [1] }, // Engl. B
{ "bSortable": true, "sWidth": "15%", "aTargets": [2] }, // Other A
{ "bSortable": true, "sWidth": "15%", "aTargets": [3] }, // Other B
]
});
if ($.cookie("_culture")) {
var lan = $.cookie("_culture", lan, { expires: 21 });
if (lan === "en-US") { setEnglishDT(dttable); }
else { setOtherDT(dttable); }
}
else {setOtherDT(dttable); }
}
This setup worked fine until now. I want to integrate copy & print options using the buttons extension and noticed that the hidden columns are also shown in the print preview. This also explains why I was able to find a search string that wasn't visible nor to be found in the html.
For each language I would need to be able to use something like this in order to show the correct columns:
buttons: [
{
extend: 'print',
exportOptions: {
columns: [ 0, 1 ]
}
I was also playing around with the extend function to set defaults but I don't know where to put the logic so that it will ideally affect every datatable in all of my views.
$.extend(true, $.fn.dataTable.defaults, {
"searching": false,
"ordering": false
})
I believe I have to change the whole structure in order to get this to work.
Can you show me how?
Thank you.