简体   繁体   English

使用jQuery或javascript将json呈现到多列表中

[英]Using jQuery or javascript to render json into multi-column table

I am trying to render a JSON into a HTML table. 我正在尝试将JSON呈现到HTML表中。 But the difficulty is making it so it loops through JSON and renders multiple columns if necessary. 但是困难在于如何使它遍历JSON并在必要时呈现多列。

For the example below, what I want is this: 对于下面的示例,我想要的是:

Result wanted 想要的结果

Result Wanted 想要的结果

<table>
<tr><th>AppName</th><td>App 1</td><td>App 2</td></tr>
<tr><th>Last Modified</th><td>10/1/2012</td><td></td></tr>
<tr><th>App Logo</th><td>10/1/2012</td><td></td></tr>
blahblah
</table>

<table>
<tr><th>AppName</th><td>App 1</td></tr>
blahblah
</table>

JSON Example JSON范例

"Records": [
    {
        "AppName": "App 1",
        "LastModified": "10/1/2012, 9:30AM",
        "ShipTo_Name": "Dan North",
        "ShipTo_Address": "Dan North",
        "ShipTo_Terms": "Dan North",
        "ShipTo_DueDate": "Dan North",
        "Items 1": [
            {
                "Item_Name": "Repairs",
                "Item_Description": "Repair Work"
            }
        ]
    },
    {
        "AppName": "App 2",
        "AppLogo": "http://www.google.com/logo.png",
        "LastModified": "10/1/2012, 9:30AM",
        "BillTo_Name": "Steve North",
        "Items 1": [
            {
                "Item_Name": "Repairs",
                "Item_Description": "Repair Work"
            }
        ]
    }
],
"Records": [
    {
        "AppName": "App 1",
        "LastModified": "10/1/2012, 9:30AM",
        "ShipTo_Name": "222",
        "ShipTo_Address": "333 ",
        "ShipTo_Terms": "444",
        "ShipTo_DueDate": "5555",
        "Items 1": [
            {
                "Item_Name": "Repairs",
                "Item_Description": "Repair Work"
            }
        ]
    }
],

Code I am using now 我现在正在使用的代码

function CreateComparisonTable (arr,level,k) {
    var dumped_text = "";
    if(!level) level = 0;

    //The padding given at the beginning of the line.
    var level_padding = "";
    for(var j=0;j<level+1;j++) level_padding = "--";

    if(typeof(arr) == 'object') { //Array/Hashes/Objects
        for (var item in arr) {
            var value = arr[item];
            if (typeof(value) == 'object') { //If it is an array,
                if(item !=0) {
                    dumped_text += '<tr><td>' + item + '<br>';
                    dumped_text += CreateComparisonTable(value,level+1);
                    dumped_text += '</td></tr>';
                } else {
                    dumped_text += CreateComparisonTable(value,level, value.length);
                }
            } else {
              dumped_text += '<tr><td>' + level_padding + item + '</td><td>' + value + '</td></tr>';
            }
        }
    } 
    return dumped_text;
}

Jsfiddle here 在这里

DataTables , a plug-in for jQuery, is a good candidate for this scenario, and it's got a lot of features "baked into" its code. DataTables是jQuery的一个插件,非常适合这种情况,并且它的许多功能都“嵌入”了其代码。

I've used it, and it handled just about everything I threw at it. 我已经用过了,它处理了我扔给它的几乎所有东西。

You might want to look into using a templating language such as Mustache 您可能需要研究使用模板语言(例如Mustache)

You might find this question useful: How to structure JSON and build HTML via jQuery 您可能会发现此问题很有用: 如何通过jQuery构建JSON和构建HTML

I will recommend jtemplates. 我会推荐jtemplates。 a jQuery plugin http://jtemplates.tpython.com/ It's a powerful template language and the code can be stored anywhere. jQuery插件http://jtemplates.tpython.com/这是一种强大的模板语言,代码可以存储在任何地方。 Inline in the page, in a separate file wherever is best for you. 内联在页面中,在最适合您的单独文件中。 It looks a lot like asp classic. 它看起来很像ASP Classic。 Take a look 看一看

{#template MAIN}
 <div id="header">{$T.name}</div>
 <table>
 {#foreach $T.table as r}
  {#include row root=$T.r}
 {#/for}
 </table>
{#/template MAIN}

{#template row}
 <tr bgcolor="{#cycle values=['#AAAAEE','#CCCCFF']}">
  <td>{$T.name.bold()}</td>
  <td>{$T.age}</td>
  <td>{$T.mail.link('mailto:'+$T.mail)}</td>
 </tr>
{#/template row}

Have a look at this comparison chart on jsfiddle I've just made. 看看我刚刚制作的jsfiddle上的比较表。 The individual cells are populated dynamically when the select event is detected from the dropdown menu. 当从下拉菜单中检测到选择事件时,将动态填充各个单元格。 Maybe you can fork the fiddle and use some of the code to acomplish what you want to do with your table. 也许您可以拨弄小提琴,并使用一些代码来完成您要对表执行的操作。

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

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