如何使用Json2HTML库将多个子项添加到父项

时间:2016-02-24 06:23:33

标签: javascript json json2html

我想使用json2html向父级添加多个子级。

jsonTestSuiteTemplate={
        "testSuiteNames": [{"testSuiteName": "TS1"}],
    };
var testSuiteHtmlTemplate = {
  "tag": "table",
  "border":"0",
  "children": [{"tag": "tr",
       "children": [{
          "tag": "td",

          "children": [{
              "tag": "ul",
              "class": "report",
              "children":[{
              "tag":"LI",
              "nowrap":"false",
              "class":"closed",
              "children": 
              [{
                   "tag":"a",
                   "href":"#testsuite",
                   "onclick":function(){ return toggle(this);},
                    "children":[{
                        "tag":"big",
                            "children":[{
                                "tag":"font",
                                "color":"green",
                                'html':"ts1 - TS (ok)"}           
                                }] //End of font tag
                                }]  //End of big tag               
              }], //End of Anchor Tag  
                {        
                           "children":[{
                          "tag":"LI",
                          "nowrap":"false",
                          "class":"closed",
                          "children": 
              [{
                   "tag":"a",
                   "href":"#testsuite",
                   "onclick":function(){ return toggle(this);},
                    "children":[{
                        "tag":"big",
                            "children":[{
                                "tag":"font",
                                "color":"green",
                                'html':"t1 - TC (ok)"}            
                                }] //End of font tag
                                }]  //End of big tag               
              }]
                           }] //End of inner List
                }   
               }]  // End of Link tag

             }]   //End of UI tag
        }]  // End of TD tag

    }]  // End of TR tag 
};
    var result = json2html.transform(jsonTestSuiteTemplate, testSuiteHtmlTemplate);

JSON2Html库提供的HTML

<table border="0"><tr><td><ul class="report"><LI nowrap="false" class="closed"><LI nowrap="false" class="closed"><a href="#testsuite"><big><font color="green">TS1</font></big></a></LI></LI></ul></td></tr></table>

但我希望HTMl像这样:

<table border="0">
               <tr>
                 <td>
                    <ul class="report">
                       <LI nowrap="true" class="closed">
                          <A HREF="#testsuite" onclick="toggle(this)"><big><font color="green">ts1 - TS (ok)</font></big></A> - 0:00:03.800
                          <ul>
                             <LI nowrap="true" class="closed">
                                <A HREF="#testcase" onclick="toggle(this)"><big><font color="green">t1 - TC (ok)</font></big></A> - 0:00:03.800
                             </LI>
                          </ul>
                       </LI>
                    </ul>
                 </td>
              </tr>
           </table>
        </td>
     </tr>
  </table>

我是json2Html图书馆的新手,发现很难将多个孩子添加到同一个家长。非常感谢任何帮助!

2 个答案:

答案 0 :(得分:1)

如果您在构建转换时遇到问题,请尝试使用转换构建器

JSON2HTML Transform Builder点击“构建器”标签

这是我为你的html获得的转换,我编辑它以包含jquery事件的正确语法。此外,如果您正在使用事件,请确保包含jquery插件jquery.json2html.js

{"tag":"table","border":"0","children":[
{"tag":"tbody","children":[
    {"tag":"tr","children":[
        {"tag":"td","children":[
            {"tag":"ul","class":"report","children":[
                {"tag":"li","nowrap":"true","class":"closed","children":[
                    {"tag":"a","href":"#testsuite","onclick":function(){toggle(this);},"children":[
                        {"tag":"big","children":[
                            {"tag":"font","color":"green","html":"ts1 - TS (ok)"}
                          ]}
                      ]},
                    {"tag":"ul","children":[
                        {"tag":"li","nowrap":"true","class":"closed","children":[
                            {"tag":"a","href":"#testcase","onclick":function(){toggle(this);},"children":[
                                {"tag":"big","children":[
                                    {"tag":"font","color":"green","html":"t1 - TC (ok)"}
                                  ]}
                              ]}
                          ]}
                      ]}
                  ]}
              ]}
          ]}
      ]}
  ]}

]}

答案 1 :(得分:-1)

var jsonTestSuiteTemplate={
        "testSuiteNames": [{"testSuiteName": "TS1"},
                         ],
    };
var testSuiteHtmlTemplate = {
  "tag": "table",
  "border":"0",
  "children": [{"tag": "tr",
       "children": [{
          "tag": "td",

          "children": [{
              "tag": "ul",
              "class": "report",
              "children":[{
              "tag":"LI",
              "nowrap":"false",
              "class":"closed",
              "children": 
              [{  //First Child
                   "tag":"a",
                   "href":"#testsuite",
                   "onclick":function(){ return toggle(this);},
                    "children":[{
                        "tag":"big",
                            "children":[{
                                "tag":"font",
                                "color":"green",
                                'html':function()
                                {
                                    return this.testSuiteName;
                                }
                                }] //End of font tag
                                }]  //End of big tag               
              }]
              }, //End of Anchor Tag and End of First Child
                {        //Second child

                           "children":[{
                          "tag":"LI",
                          "nowrap":"false",
                          "class":"closed",
                          "children": 
              [{
                   "tag":"a",
                   "href":"#testsuite",
                   "onclick":function(){ return toggle(this);},
                    "children":[{
                        "tag":"big",
                            "children":[{
                                "tag":"font",
                                "color":"green",
                                'html':"t1 - TC (ok)"

                                }] //End of font tag
                                }]  //End of big tag               
              }]
                           }] //End of inner List

               }   // Second child

           ]  // End of Link tag

             }]   //End of UI tag
        }]  // End of TD tag

    }]  // End of TR tag 
};