简体   繁体   English

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

[英]How to enable search for hidden column in datatable?

I have table which contains a Name column but I don't want to show that column in the table but I need to add a search filter on it. 我有一个包含Name列的表,但我不想在表中显示该列,但我需要在其上添加一个搜索过滤器。

I tried using the below, but In this case the column and textbox of search filter both are not showing. 我尝试使用下面的内容,但在这种情况下,搜索过滤器的列和文本框都没有显示。

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

When I set "bVisible": true then the text box of filter and column both are showing and also the search works fine. 当我设置"bVisible": true ,过滤器和列的文本框都显示,搜索也正常。

I am using below code to display column filter. 我使用下面的代码来显示列过滤器。

HTML For Filter: 用于过滤的HTML:

<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 for Table : 表格的HTML:

Datatable Javascript After Update: 更新后的数据库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
    });

});

With version 1.9.4 of DataTables this works ( jsFiddle ) 使用版本1.9.4的DataTables( jsFiddle

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

Maybe you are missing the aTargets parameter? 也许你错过了aTargets参数? If you are using aoColumns instead of aoColumnDefs , the array length needs to be equal to the number of columns ( docs ). 如果使用的是aoColumns而不是aoColumnDefs ,则数组长度需要等于列数( docs )。 I'm not sure if the sName parameter affects how this works... 我不确定sName参数是否影响它的工作方式......

Edit (to answer more detailed question): 编辑(回答更详细的问题):

I guess (from trying to replicate your problem here ) that it is the columnFilter plugin that is not working. 我猜(从试图复制你的问题在这里 ),这是不正常的columnFilter插件。 If you have hidden columns you have to set the bUseColVis parameter to true ( docs ). 如果您有隐藏列,则必须将bUseColVis参数设置为truedocs )。 Like so: 像这样:

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

You can also do this via searching on a specific column: 您也可以通过搜索特定列来执行此操作:

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

Because we have chained .data() , this will allow for indexing on column 0 even if visibility is set to false. 因为我们已经链接了.data() ,所以即使可见性设置为false,也允许对列0进行索引。

If you only want to search on visible columns, then omit the .data() . 如果您只想搜索可见列,则省略.data()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM