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 交替优化波束形成和ris反射角使保密速率最大化
  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程