2015-07-10 78 views
1

我尝试动态构建HTML表格是徒劳的。 代码工作,但它不输出我所需要的。 只有2我的JSON输出中的属性:locationcompleted_on 非但没有这样的:如何使用来自AJAX调用的JSON数据构建表?

+-------------------+---------------------+ 
|  Location  | Completed on  | 
+-------------------+---------------------+ 
| 10 Duskwood Dr. | 2015-07-06 14:10:42 | 
| 2 Booty Bay Blvd. | 2015-07-09 13:44:38 | 
+-------------------+---------------------+ 

代码显示了这一点(没有THEAD可见):

[ 
{ 
" 
l 
o 
c 
a 
... 
] 
... prints the whole JSON character by character inside many <td> tags... 

这是一个典型的例子JSON数据我必须使用:

[{"location":"10 Duskwood Dr.","completed_on":"2015-07-06 14:10:42"}, 
{"location":"2 Booty Bay Blvd.","completed_on":"2015-07-09 13:44:38"}] 

这是表格标记我硬编码到HTML和更新使用JSON:

<table id="completed_locations" class="table table-striped"> 
    <thead> 
     <tr> 
      <th>Location</th> 
      <th>Completed on</th> 
     </tr> 
    </thead> 
    <tbody> 
     // JSON data goes here 
    </tbody> 
</table> 

我用这个代码来构建<tr><td>

// this is called within success of ajax to build table 
var oldTable = document.getElementById('completed_locations'), 
    newTable = oldTable.cloneNode(); 
for(var i = 0; i < output.length; i++){ 
    var tr = document.createElement('tr'); 
    for(var j = 0; j < output[i].length; j++){ 
     var td = document.createElement('td'); 
     td.appendChild(document.createTextNode(output[i][j])); 
     tr.appendChild(td); 
    } 
    newTable.appendChild(tr); 
} 
oldTable.parentNode.replaceChild(newTable, oldTable); 
+0

Cerbrus有已经指出你的第一个问题(你有一个JSON字符串,你需要解析它来获得实际的JavaScript数组)。接下来你需要修正的是'​​'元素的创建,如果你有一个对象数组,而不是一个数组数组,那么这个元素看起来是错误的。 –

回答

2

我怀疑你output变量仍然是在它的JSON字符串格式。
这将导致for循环遍历字符串中的每个单个字母,而不是遍历数组中的每个条目。

尝试增加:

output = JSON.parse(output); 

for循环之前。

这个不会引发任何错误的原因是因为外层循环迭代JSON字符串的全长,然后在内层循环中,output[i].length总是1,因为它是1个字符的字符串。内部循环将始终访问output[i][0],然后终止该循环。现在

,解决实际创建表:

var output = '[{"location":"10 Duskwood Dr.","completed_on":"2015-07-06 14:10:42"},{"location":"2 Booty Bay Blvd.","completed_on":"2015-07-09 13:44:38"}]', 
 
    oldTable = document.getElementById('completed_locations'), 
 
    newTable = oldTable.cloneNode(); 
 

 
output = JSON.parse(output); 
 

 
for(var i = 0; i < output.length; i++){ 
 
    var tr = document.createElement('tr'); 
 
    // No need to loop here with only 2 properties 
 
    var td = document.createElement('td'); 
 
    td.appendChild(document.createTextNode(output[i].location)); 
 
    tr.appendChild(td); 
 

 
    td = document.createElement('td'); 
 
    td.appendChild(document.createTextNode(output[i].completed_on)); 
 
    tr.appendChild(td); 
 

 
    newTable.appendChild(tr); 
 
} 
 
oldTable.parentNode.replaceChild(newTable, oldTable);
<table id="completed_locations" class="table table-striped"> 
 
    <thead> 
 
     <tr> 
 
      <th>Location</th> 
 
      <th>Completed on</th> 
 
     </tr> 
 
    </thead> 
 
    <tbody> 
 
     // JSON data goes here 
 
    </tbody> 
 
</table>

如果你有更多的列,你可以遍历它们,而不是:

var columns = ['location', 'completed_on']; 
for(var i = 0; i < output.length; i++){ 
    var tr = document.createElement('tr'), 
     td; 

    for(var j = 0; j < columns.length; j++){ 
     td = document.createElement('td'); 
     td.appendChild(document.createTextNode(output[i][columns[j]])); 
     tr.appendChild(td); 
    } 

    newTable.appendChild(tr); 
} 
+0

这使得我的'alert(output)'显示'[object Object],[object Object]',现在什么都没有出现在表中。 – BaddieProgrammer

+0

好,这意味着'输出'现在是一个实际的对象,只是一个JSON字符串。现在我们只需要遍历它。 – Cerbrus

+0

最后,我真的看到了。谢谢。 – BaddieProgrammer

相关问题