【问题标题】:Datatables: Hide Column based on database value数据表:根据数据库值隐藏列
【发布时间】:2013-06-28 11:06:11
【问题描述】:

我正在尝试根据数据库值显示/隐藏列。我正在使用 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 中隐藏/显示带有数据表的表数据?

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

【问题讨论】:

    标签: php jquery mysql ajax datatables


    【解决方案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 );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-16
      • 1970-01-01
      • 2020-05-23
      • 1970-01-01
      • 2021-10-04
      • 1970-01-01
      相关资源
      最近更新 更多