数据表:根据数据库值隐藏列

时间:2013-07-01 10:42:58

标签: php jquery mysql ajax datatables

我正在尝试根据数据库值显示/隐藏列。我正在使用Jquery,PHP和MySQL。

我使用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 个答案:

答案 0 :(得分: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 );