从AJAX回复查询创建动态表

时间:2012-03-05 12:10:16

标签: jquery ajax html-table

我有以下代码:

            var htmlname='';
            var names = result.names
            $.each(names, function( i, name){

               htmlname = "<td>" + name + "</td>";

               $('#adminall tr:last').html(htmlname);

                var row = document.createElement('tr');
                var tableCurrentPoints = document.getElementById("adminall"); // find table to append to
                tableCurrentPoints.appendChild(row); // append row to table


            });


            var htmlemail='';
            var emails = result.emails
            $.each(emails, function( ii, email){

               htmlemail = "<td>" + email + "</td>";

               $('#adminall tr:last').html(htmlemail);

                var row = document.createElement('tr');
                var tableCurrentPoints = document.getElementById("adminall"); // find table to append to
                tableCurrentPoints.appendChild(row); // append row to table


            });

然后是附加的表格:

        <table id="adminall" style="margin:10px 0px 0px 0px;" width="100%" border="0" cellpadding="0" cellspacing="0">
        <tr id="adminallheader">

        </tr>
        <tr></tr>

    </table>

这是来自AJAX请求中的成功处理程序。我试图创建一个填充了查询数据的表,我猜它应该在Javascript中创建,因为我在这里尝试,但我无法让它工作。目前它在一列中包含所有数据(名称和电子邮件),但需要将它们分成两列。

有什么想法吗?

1 个答案:

答案 0 :(得分:2)

您应该执行类似

的操作
       var names = result.names
       $.each(names, function( i, name){
           //get the corrsponding email
           var email = result.emails[i];
           var htmlname = "<td>" + name + "</td><td>"+email"</td>";

           $('#adminall tr:last').append(htmlname);

            var row = document.createElement('tr');
            var tableCurrentPoints = document.getElementById("adminall"); // find table to append to
            tableCurrentPoints.appendChild(row); // append row to table


        });

并且只循环一次而不是两次。