简体   繁体   English

如何通过jQuery使用JSON数据动态创建表

[英]How to dynamically create a table using JSON data with jQuery

I have JSON data which i need to displayed in a table and then apply datatable on that table. 我有需要显示在表中的JSON数据,然后将datatable应用于该表。 Some part of the table is static while others must be created dynamically. 该表的某些部分是静态的,而其他部分则必须动态创建。 There will be dynamic headers and offcource the data data to be shown will be taken from JSON. 将有动态标头,并且将要显示的数据数据从JSON中移出。 My Static HTML Code is as follows 我的静态HTML代码如下

<table border="1" align="center" id="info-table">
<thead>
<tr>
  <th>Roll No</th>
  <th>Student Name</th>
  <th>Student ID</th>
  <th>Class</th>

Now i have to dynamically add more headers so for that i am using $.each. 现在我必须动态添加更多的标头,因此我正在使用$ .each。 After that i need to add The TD's to show the data. 之后,我需要添加TD以显示数据。 The code is as follows 代码如下

obj = $.parseJSON(json.responseText);
    if (obj.collection.response.error) {
        displayError(obj.collection.response.error);
      } else {
          //Prepare fields for Attendance codes
          $.each(obj.collection.response.attendanceCodes, function(key, value){
              $('#info-table tr').append("<th>"+value.title+"</th>");
          });
          //Add the static headers
          $('#info-table tr').append("<th>Teacher Comment</th><th>Admin Comment</th></tr></thead><tbody>");
          //Prepare fields for EachStudent
        $.each(obj.collection.response, function(i, val){
            if(i != 'attendanceCodes'){
                $('#info-table').append("<tr><td>"+val.rollNo+"</td><td>"+val.studentName+"</td><td>"+val.studentId+"</td><td>"+val.className+"</td><td align=\"center\"><div  class=\"radio-green\"><input type=\"radio\" checked=\"checked\" name=\"attend-"+val.studentId+"\" /></div></td><td align=\"center\"><div class=\"radio-red\"><input type=\"radio\"  name=\"attend-"+val.studentId+"\" /></div></td><td><input type=\"text\" style=\"width:200px;\" name=\"teacher-comment-"+val.studentId+"\" /></td><td>- - -</td><td></td><td></td><td></td><td></td></tr>");
            }
        });
        //$('#info-table').dataTable();
        }
    },
    dataType:"JSON"

But this code is not working and i am getting error in console that says: Uncaught TypeError: Cannot read property 'childNodes' of null 但是此代码无法正常工作,并且我在控制台中看到以下错误:Uncaught TypeError:无法读取null的属性'childNodes'

It will be better if you use any jquery plugin for that.Like datatable or jqgrid.It will give good support to JSON and XML based data. 如果您为此使用任何jquery插件会更好,例如datatable或jqgrid。它将为基于JSON和XML的数据提供良好的支持。

http://trirand.com/blog/jqgrid/jqgrid.html http://trirand.com/blog/jqgrid/jqgrid.html

Yes i got the answer... 是的,我得到了答案...

We will have to remove the closing tr and thead and the new code for that area will be and it worked. 我们将必须删除结束tr和thead,并且该区域的新代码将成功运行。

 //Add the static headers
          $('#info-table tr').append("<th>Teacher Comment</th><th>Admin Comment</th>");

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

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