doukao5073 2013-06-12 08:47
浏览 78
已采纳

使用Ajax从数据库隐藏/显示表列

I found a way to connect to my database and retrieve data but it's doesn't seem to be working when I try to hide/show my table column, I dont even know if it's possible with Ajax.

My Html:

<table class="footable" id="masterChart">
            <thead><tr><th data-class="expand" class="account">Account Number</th><th>Account Description</th><th>Level 01</th><th>Level 02</th><th>Level 03</th><th>Level 04</th><th>Tax</th><th>YTD - Current</th><th>YTD - Prior</th><th>MTD - Current</th><th>MTD - Prior</th></tr></thead>

            <?php $index = 0?>

            <?php while ($row = mysql_fetch_assoc($result)):?>

            <tr<?php echo $index++ % 2 ? ' class="even"' : ''?>>
                <td><?php echo $row['accountNumber']?></td>
                <td><?php echo $row['accountDescription']?></td>
                <td><?php echo $row['accountLevel1']?></td>
                <td><?php echo $row['accountLevel2']?></td>
                <td><?php echo $row['accountLevel3']?></td>
                <td><?php echo $row['accountLevel4']?></td>
                <td><?php echo $row['id']?></td>
                <td><?php echo $row['id']?></td>
                <td><?php echo $row['id']?></td>
                <td><?php echo $row['id']?></td>
                <td><?php echo $row['id']?></td>
            </tr>

            <?php endwhile?>

        </table>

My dbconn.php:

 <?php
  include_once('../classes/profile.class.php');

  $host = "localhost";
  $user = "root";
  $pass = "root";

  $databaseName = "accounting";
  $tableName = "login_table_display";

  //--------------------------------------------------------------------------
  // 1) Connect to mysql database
  //--------------------------------------------------------------------------

  $con = mysql_connect($host,$user,$pass);
  $dbs = mysql_select_db($databaseName, $con);

  //--------------------------------------------------------------------------
  // 2) Query database for data
  //--------------------------------------------------------------------------
  $user = $profile->getField('user_id');
  $result = mysql_query("SELECT * FROM '$tableName' WHERE user = '$user'");          //query
  $array = mysql_fetch_row($result);

  echo json_encode($array);
?>

I use the profile.class.php to get the user_id.

My ajax:

$(function () 
  {
    //-----------------------------------------------------------------------
    // 2) Send a http request with AJAX http://api.jquery.com/jQuery.ajax/
    //-----------------------------------------------------------------------
    $.ajax({                                      
      url: 'dbconn.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 column = data[3];           //get column
        var show = data[4];          //display or hide
        //--------------------------------------------------------------------
        // 3) Update html content
        //--------------------------------------------------------------------
        if (show == 0){
       $(table +'td:nth-child('+ column +'),th:nth-child('+ column +')').hide();
        //recommend reading up on jquery selectors they are awesome 
        // http://api.jquery.com/category/selectors/
        }
      } 
    });
}); 

I included the ajax.js file above to my file as:

<script src="ajax/ajax.js" type="text/javascript"></script>

If anyone can help or assist, I'd really appreciate it! If anyone has an alternative method of getting the database info and hiding the specific column, table for the user, i'd also appreciate it.

  • 写回答

2条回答 默认 最新

  • dongnan4571 2013-06-12 10:02
    关注

    Ok so I got it right:

    I added the profile.class.php wrong it was suppose to be ../classes/profile.class.php

    Besides that, the jquery was wrong,

    This:

    $(table +'td:nth-child('+ column +'),th:nth-child('+ column +')').hide();
    

    Needs to be this:

    $('#'+ table +' td:nth-child(' + column + '), #' + table + ' th:nth-child(' + column + ')').hide();
    

    And it's finding the show/hide value based on the users selection :)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀