I am using Datatable in this link to show a grid. https://datatables.net/examples/basic_init/hidden_columns.html
I show couple default column using (columnDefs.targets) then I have added the ability to show and hide the column from this link:
https://datatables.net/examples/api/show_hide.html
first I load the page is correct and shows the default columns, when I try to show/hide , it show all the column instead of default one, I am not sure how to show only default one there.
this is my code:
$(document).ready(function () {
var table = $('#DataLegal').DataTable({
"columnDefs": [
{
"targets": [ 4,5,6,7,8,9,10,14,15,16,17,18,19,20,21,22,23,24,25,26,27],
"visible": false
// "searchable": false
}
]
} );
//This is show/Hide part
var ms = $('#magicsuggest').magicSuggest({
// Converts our C# object in a JSON string.
data: @Html.Raw(Newtonsoft.Json.JsonConvert.SerializeObject(columns))
});
$(ms).on('selectionchange', function(e,m){
// Turn on columns
$.each(table.columns()[0], function(index) {
table.column(index).visible(true);
//here how I can only turned on the DefColumns?
});
// Turn off each column in the value array... Value = int[0,1, 2, ...]
$.each(this.getValue(), function(index, item) {
table.column(item).visible(false);
});
});
});
Have you tried storing that target list?
Then only update the list in the each funciton? Something like this?
You can extract the
columnDefs
settings bytable.init().columnDefs
,will return the above
[ 4,5,6,7,8,9,10,14,15,16...]
array. A quick way to create a show / hide select box holding values of the hidden columns could bepopulate the hidden columns
show / hide columns when the user select a column in the select box
demo -> http://jsfiddle.net/dwhwftxc/