2013-07-01 74 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);