如何在数据表中启用搜索隐藏列?

时间:2015-10-20 12:00:37

标签: javascript jquery datatable

我的表格中包含Name列,但我不想在表格中显示该列,但我需要在其上添加搜索过滤器。

我尝试使用下面的内容,但在这种情况下,搜索过滤器的列和文本框都没有显示。

{
    "sName": "Name", 
    "bVisible": false, 
    "bSearchable": true
}

当我设置"bVisible": true时,显示过滤器和列的文本框,并且搜索也正常。

我使用下面的代码显示列过滤器。

HTML For Filter:

<div class="col-xs-12 col-sm-6 col-md-4">
     <div class="form-group">
          <label class="col-sm-5 control-label">Customer Name </label>
                 <div class="col-sm-7" id="serName"></div>
      </div><!-- form-group -->
</div>

表格的HTML:

更新后的数据库Javascript:

$(document).ready(function () {
    dTable = $('#exRowTable').dataTable({            
        iDisplayLength: 10,
        sAjaxSource: AjaxSource,
        aoColumns: [               
//            {"sName": "Name", "bVisible": false, "bSearchable": true, "aTargets": [7]},
            {"sName": "Name"}
        ],
        aoColumnDefs: [                            
            {
                "bSearchable": true, 
                "bVisible": false, 
                "aTargets": [ 7 ]
            }
        ],            
        "aaSorting": [[0, 'desc']],
        sPaginationType: "full_numbers"});

    $('#exRowTable').dataTable().columnFilter({
        //sPlaceHolder: "head:after",
        aoColumns: [
            {type: "date-range", sSelector: "#serPickupDate"},
            {type: "text", sSelector: "#serCustId"},
            null,
            null,
            null,
            null,
            null,
            {type: "text", sSelector: "#serName"}
        ],
        bUseColVis: true
    });

});

2 个答案:

答案 0 :(得分:2)

使用DataTables的1.9.4版本(jsFiddle

$('#example').dataTable( {
    "aoColumnDefs": [
        { 
            "bSearchable": true, 
            "bVisible": false, 
            "aTargets": [ 2 ]
        },
    ] 
});

也许您错过了aTargets参数?如果您使用aoColumns而不是aoColumnDefs,则数组长度必须等于列数(docs)。我不确定sName参数是否影响其工作原理......

编辑(回答更详细的问题):

我猜(试图复制你的问题here),它是columnFilter插件无法正常工作。如果您有隐藏列,则必须将bUseColVis参数设置为truedocs)。像这样:

$('#exRowTable').dataTable().columnFilter({     
        //sPlaceHolder: "head:after",
        aoColumns: [    
            { type: "date-range", sSelector: "#serPickupDate" },
            { type: "text", sSelector: "#serCustId" },
            null,
            null,
            null,
            null,
            null,
            { type: "text", sSelector: "#serName"},
            { type: "select", sSelector: "#serTimeslotsId", values: TimeSlotsDP},
            { type: "select", sSelector: "#serPickUpPin", values: PincodeDp },
            { type: "select", sSelector: "#serDeliveryPin", values: PincodeDp },
            { type: "date-range", sSelector: "#serRequestDateTime" },
            { type: "select", sSelector: "#serPickedUpStatus", values: ['Yes','No'] },                                
            { type: "select", sSelector: "#serStaffUser", values: StaffUserDp }
        ],
        bUseColVis: true
    });

答案 1 :(得分:1)

您也可以通过搜索特定列来执行此操作:

$("#table").DataTable().column(0).data().search("example");

因为我们已经链接.data(),所以即使可见性设置为false,这也允许对列0进行索引。

如果您只想搜索可见列,请省略.data()