简体   繁体   English

如何使用Json2HTML库向父级添加多个子级

[英]How to add multiple children to parent using Json2HTML library

I want to add multiple children to parent using 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);

HTML given by the JSON2Html library 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>

But I want HTMl like this: 但是我想要这样的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>

I am new to json2Html library finding difficult to add multiple children to same parent.Any help is much appreciated! 我是json2Html库的新手,发现很难将多个子代添加到同一父代中。任何帮助将不胜感激!

If you're having trouble with building the transform, try using the transform builder 如果您在构建转换时遇到问题,请尝试使用转换生成器

JSON2HTML Transform Builder Click on "Builder" tab JSON2HTML转换生成器单击“生成器”选项卡

Here's the transform I get for your html, I've edited it to include the proper syntax for jquery events. 这是我为您的html获得的转换,我对其进行了编辑,以包括针对jquery事件的正确语法。 Also if you're using events make sure to include the jquery plugin jquery.json2html.js 另外,如果您使用事件,请确保包括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)"}
                                  ]}
                              ]}
                          ]}
                      ]}
                  ]}
              ]}
          ]}
      ]}
  ]}

]} ]}

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 
};

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

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