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

标签 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 中数据表的表数据?

任何帮助或建议,我们将不胜感激!

最佳答案

我之前没看过这篇文章! 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 );

关于php - 数据表:根据数据库值隐藏列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17402581/

相关文章:

php - Laravel/Lumen:View::share() 替代方案?

php - 如何在使用 UNION 时删除重复记录

javascript - 如何指示动态表列表中 <tr> 表行的位置

mysql - 运行sql语句时出错

php - 从 PHP/Javascript 编码的网站/页面打开 iphone 应用程序

php - 如何用php递归更新xml属性值?

php - PDO 事务中的意外结果

php - 自动部署 PHP 应用程序

jquery - CSS 背景图片加载检查

javascript - 将表单重置为默认值