2013-07-01 109 views
0

我試圖根據數據庫值顯示/隱藏列。我使用的是JQuery,PHP和MySQL。Datatables:根據數據庫值隱藏列

我使用AJAX來檢索數據和隱藏列,但它並不隱藏TBODY數據,只有頭是隱藏的:

$(function() 
    { 
    //----------------------------------------------------------------------- 
    // 2) Send a http request with AJAX http://api.jquery.com/jQuery.ajax/ 
    //----------------------------------------------------------------------- 
    $.ajax({          
     url: 'account-number.php',     //the script to call to get data   
     data: '',      //you can insert url argumnets here to pass to api.php 
             //for example "id=5&parent=6" 
     dataType: 'json',    //data format  
     success: function(data)   //on recieve of reply 
     { 
     var user = data[1];    //get id 
     var table = data[2];   //get table name 
     var show = data[4];   //display or hide 
     //-------------------------------------------------------------------- 
     // 3) Update html content 
     //-------------------------------------------------------------------- 
     //recommend reading up on jquery selectors they are awesome 
     // http://api.jquery.com/category/selectors/ 
     if (show == 0) 
     $('#'+ table +'tbody td:nth-child(1), #' + table + 'thead th:nth-child(1)').hide(); 
     //$('#'+ table +'td:nth-child('+ column +'),th:nth-child('+ column +')').hide(); 
     if (show == 1) 
     $('#'+ table +'tbody td:nth-child(1), #' + table + 'thead th:nth-child(1)').show(); 
     } 
    }); 
    }); 

有一個在控制檯沒有錯誤這一點。基於數據庫值,有沒有一種特定的方式來隱藏/顯示Jquery中的數據表的數據表?

任何幫助或建議,將不勝感激!

回答

1

我以前沒有看到這篇文章! jquery datatables hide column

但它幫助了我很多。

我改變了這個:

if (show == 0) 
      $('#'+ table +'tbody td:nth-child(1), #' + table + 'thead th:nth-child(1)').hide(); 

if (show == 1) 
      $('#'+ table +'tbody td:nth-child(1), #' + table + 'thead th:nth-child(1)').show(); 

要這樣:

if (show == 0) 
     oTable.fnSetColumnVis(0, false); 

if (show == 1) 
     oTable.fnSetColumnVis(0, true);