How turn on columnDefs in jquery DataTable

2019-02-26 08:21发布

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);
                 });


           });

      });

2条回答
混吃等死
2楼-- · 2019-02-26 08:34

Have you tried storing that target list?

Then only update the list in the each funciton? Something like this?

$(document).ready(function () {
    var targetArr = [4,5,6,7,8,9,10,14,15,16,17,18,19,20,21,22,23,24,25,26,27];
    var table = $('#DataLegal').DataTable({
            "columnDefs": [{
                 "targets": targetArr,
                 "visible": false
                 // "searchable": false
             }]
     });    

    $(ms).on('selectionchange', function(e,m){
        // Turn on columns
        $.each(table.columns()[0], function(index) {
            if($.inArray(item, targetArr)){
                table.column(item).visible(true); //in case some values were false set all to true
            } else {
                table.column(item).visible(false);//in case some values were true set all to false
            }
        });

        $.each(this.getValue(), function(index, item) {
            table.column(item).visible(false);
        });
    });
 });
查看更多
唯我独甜
3楼-- · 2019-02-26 08:55

You can extract the columnDefs settings by table.init().columnDefs,

table.init().columnDefs[0].targets

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 be

show / hide column :<select id="columns"></select>

populate the hidden columns

table.init().columnDefs[0].targets.forEach(function(column) {
    $("#columns").append('<option value="'+column+'">show / hide column #'+column+'</option>');
})  

show / hide columns when the user select a column in the select box

$("#columns").on('change', function() {
    table.column(this.value).visible(!table.column(this.value).visible())
})  

demo -> http://jsfiddle.net/dwhwftxc/

查看更多
登录 后发表回答