简体   繁体   English

如何获得成功的Ajax响应后动态创建表?

[英]How do I create a table dynamically upon getting a successful ajax response?

I'm returning data from our database and creating a table dynamically. 我正在从数据库中返回数据并动态创建表。 If the call to page1.php is successful, we create a table with one header row and then loop through the results and make some rows. 如果对page1.php的调用成功,我们将创建一个包含一个标题行的表,然后遍历结果并添加一些行。 How do I add a single header row? 如何添加单个标题行? thx! 谢谢!

$.post("page1.php", {user: "homer"}, 
function(data){
// output the header row
  function(test){
  var tblHdr ='<th>'+'Username'+'</th>';
  $(tblHdr).appendTo("#tble");
  };

// output the user data
   $.each(data.userdata, 
     function(i,details){

     var tblRow ='<tr>'+'<td>'details.name+'</td>'+'</tr>';
         $(tblRow).appendTo("#tble");

    });
   }, "json"
);

why do you even need or want that function in there? 您为什么甚至需要或想要那里的功能? If you remove the whole function declaration from that block and just append the th as defined then you should be fine. 如果您从该块中删除整个函数声明,然后仅按定义添加th,那么您应该可以。 assuming tblHdr and tblRow are defined somewhere. 假设在某处定义了tblHdr和tblRow。

$.post("page1.php", {user: "homer"}, 
function(data){
// output the header row

  var tblHdr ='<th>'+'Username'+'</th>';
  $(tblHdr).appendTo("#tble");


// output the user data
   $.each(data.userdata, 
     function(i,details){

     var tblRow ='<tr>'+'<td>'details.name+'</td>'+'</tr>';
         $(tblRow).appendTo("#tble");

    });
   }, "json"
);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM