使用jQuery ajax创建动态表

时间:2012-08-17 00:57:58

标签: php jquery html-table

我面临一个奇怪的问题......我正在使用来自ajax调用的数据动态创建表格。该代码在Firefox中运行,但在Chrome中无效...

HTML结构我打算创建:

<title>Offers MADE<title>    
 <Table>
   proposed offer table
 </table>

<title>Offers Received<title>    
  <Table>
   Received offer table
 </table>

但相反,这是我在Chrome上获得的结果,但它适用于Firefox ..

<title>Offers MADE<title>       

<title>Offers Received<title>  

  <Table>
   proposed offer table
 </table>

  <Table>
   Received offer table
 </table>

我认为它必须对ajax调用响应时间做一些事情; 因为如果我放置一个断点,它总能解决..

为了确保ajax调用顺序正确,我在第一次AJAX调用的success()函数中进行第二次AJAX调用。

 $.ajax{
    ::
    url : get_Proposed_Offers.php
    ::

    success : function(data)
      {
          //I make sure that the Proposed Offer Table gets populated
          //I populate the "div" tag with required HTML
          populate_Proposed_OfferTable();


          //Here's where I make another ajax call to populate 
          get_Received_OfferData();
      }

 }

 function get_Received_OfferData()
 {
    $.ajax{
    ::
    url : get_Received_Offers.php
    ::

    success : function(data)
      {
          populate_Received_OfferTable();              
      }

    }

 }

任何人都可以指出我在这里做错了什么吗?

我知道如果我开始在不同标签中填充“建议”和“已收到”商品,而不是使用相同的标签,这应该可以解决我的问题。但我想知道为什么这种方法不起作用。

1 个答案:

答案 0 :(得分:3)

为什么你需要两个ajax调用?我将进行一次ajax调用,让我的php在JSON中发送两个数组的数据

 $.ajax({

   url:'populate_tables.php',
   dataType :'JSON',
   success: function(data){

   $.map(data.proposed.records,function(proposed_row,i){
      populate_proposed_table here
   }
   $.map(data.recieved.records,function(received_row,i){
      populate_received_table here
   }
  }
})

有了这个,你甚至可以为你的作品添加其他功能和效果

更新:您可以将任意数量的参数传递到一个调用中。

$.ajax({
 ........
 data:{param1:value1,param2:value2....},
})

和php将数据视为数组

<?php
   $param1 = $_REQUEST['param1'];//not sure if you are using post or get
   $param2 = $_REQUEST['param2']...

从这里运行您的查询