如何在jquery DataTable中打开columnDefs

2023-12-03

我在此链接中使用数据表来显示网格。https://datatables.net/examples/basic_init/hidden_​​columns.html

我使用 (columnDefs.targets) 显示几个默认列,然后我添加了从此链接显示和隐藏该列的功能:

https://datatables.net/examples/api/show_hide.html

首先,我加载的页面是正确的并显示默认列,当我尝试显示/隐藏时,它显示所有列而不是默认列,我不确定如何仅显示默认列。

这是我的代码:

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


           });

      });

您是否尝试过存储该目标列表?

然后只更新each函数中的列表?像这样的东西吗?

$(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);
        });
    });
 });
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在jquery DataTable中打开columnDefs 的相关文章

随机推荐