【问题标题】:hide columns in jQuery dataTable隐藏 jQuery dataTable 中的列
【发布时间】:2018-04-04 12:43:35
【问题描述】:

我正在使用 jQuery Datatable 在 jsp 页面中呈现表格。表中的列数可能因服务器端而异。 我的代码是:

<th width="10%">Beneficiary Bank</th>
<th width="10%">Depositor's Bank</th>
<th width="10%">Type</th>
<th width="8%">Reference No</th>
<th width="8%">Amount</th>  
<th width="8%">Date</th>
<th width="8%">Bonus?</th>
<th width="8%">Remarks</th>

我有 8 列,但根据某些条件,我必须隐藏其中的一些。 我正在渲染这样的表格:

jQuery('#gridTable').dataTable( {
            "aaData":data,
            "aoColumns": [
                      { "mDataProp": "prop1", 
                        "bVisible" : function() {
                            if(condition1) {
                                return true;
                            } else {
                                return false;
                            }
                        }
                      },

                      { "mDataProp": "prop2"
                      },

                      { "mDataProp": "prop3"
                      },

                      { "mDataProp": "propp8",
                        "bVisible" : function(){
                            if(condition2) {
                                return true;
                            } else {
                                return false;
                            }
                        }
                      },

                      { "mRender": "prop9",
                        "bVisible" : function(){
                            if(condition2) {
                                return true;
                            } else {
                                return false;
                            }
                        }
                      },

                      { "mDataProp": "prop10",
                         "bVisible" : function(){
                            if(condition2) {
                                return true;
                            } else {
                                return false;
                            }
                        }
                      },

                      { "mDataProp": "prop11", 
                        "bVisible" : function(){
                            if(condition3) {
                                return false;
                            } else {
                                return true;
                            }
                        }
                      },

                      { "mDataProp": "prop12", 
                        "bVisible" : function(){
                                if(condition3) {
                                    return false;
                                } else {
                                    return true;
                                }
                        }
                      },

                  ]  ,
                "order": [] 
              } );

根据我的代码,取决于condition1 第一列应该隐藏。同样,第 4-8 列也应根据某些条件隐藏/显示。但他们并没有隐藏。在表格中,所有列都显示出来了。

我在互联网上搜索过这个问题,但没有任何解决方案有帮助。

【问题讨论】:

  • 仅供参考,您使用的代码是旧数据表版本。较新的版本有不同的语法,你可能会使用新文件但编写旧代码吗?

标签: javascript jquery datatables


【解决方案1】:

bVisible 属性是一个布尔值,它不支持函数。但是,您可以将逻辑移到 initComplete 函数中,并使用 column().visible() 隐藏所需的列。

【讨论】:

    猜你喜欢
    • 2015-07-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-16
    • 2019-03-18
    • 2021-10-16
    • 2011-07-03
    • 1970-01-01
    相关资源
    最近更新 更多