Read Value of Hidden Column in JQuery

前端 未结 8 602
青春惊慌失措
青春惊慌失措 2020-12-20 13:09

I need to hide a column on the table, but after that I cannot read selected row\'s hidden column value.

 dtTable = $(\'#lookupTable\').DataTable({
       \"c         


        
8条回答
  •  北荒
    北荒 (楼主)
    2020-12-20 13:43

    Go through the dataTables API and you have multiple ways to retrieve data from hidden columns the correct way. For example you can use cells. As you see in the link you can use all kind of selectors with cells, like a jQuery selector.

    Here a very simple example that logs out the values of the first column that has been hidden :

    var dtTable = $('#example').DataTable()  
    
    dtTable.columns([0,1,2]).visible(false);
    
    for (var i=0;i<10;i++) {
        console.log(dtTable.cells({ row: i, column: 0 }).data()[0]);
    }    
    

    http://jsfiddle.net/oumtdd6k/

    It cannot be emphasized enough : Always go through the API, do not try to use traditional jQuery on an initialised dataTable!!

    In this case the reason is obvious : jQuery can only access elements that actually is in the DOM. When you hide columns in dataTables they are not hidden as in display: none, they are simply not rendered!

提交回复
热议问题