簡體   English   中英

如何在json2html庫中使用li標簽

[英]How to use li tag in json2html library

我正在使用Json2Html將json轉換為HTML。我發現使用li標簽有困難。非常感謝您的幫助。

 var data = [{
      "testSuite": [{
          "testCase": [{
              "testCaseName": "tc1",
              "testStep": [{
                  "result": "true",
                  "testStepName": "ts1",
                  "screenShot": "image"
                }, {
                  "result": "true",
                  "testStepName": "ts2",
                  "screenShot": "image"
                }] //End of TestStep
            },

          ],
          "testSuiteName": "testSuite1",
        }] // End of testSuite
    }];

var transform = {
    // Printing the Execution stack 

    "testSuite": {

      "tag": "ul",
      "children": function() {

        return (json2html.transform(this.testSuite, transform.testSuiteName));
      }
    },
    "testSuiteName": {
      "tag": "li",
      "html": "${testSuiteName}",
      "children": function() {
        return ('<ul>' + json2html.transform(this.testCase, transform.testCaseNameRetrieval) + '</ul>');
      }
    },

    "testCaseNameRetrieval": {
      "tag": "li",

      "children": function() {
        return (json2html.transform(this, transform.testCaseName));
      }
    },

    "testCaseName": {
      "tag": "li",
      "html": "${testCaseName}",
      "children": function() {
        return (json2html.transform(this.testStep, transform.testStepRetrieval));
      }
    },

    "testStepRetrieval": {
      "tag": "li",
      "children": function() {
        return ('<ul>' + json2html.transform(this, transform.testStep) + '</ul>');
      }
    },

    "testStep": {
      "tag": "li",

      "html": "${testStepName}",
      "children":

        return ('<ul>' + json2html.transform(this, transform.testStepResultDescription) + '</ul>');
    }

  },


  "testStepResultDescription": {
    "tag": "li",
    "children": [{
      "tag": "div",
      "html": "${screenShot}               -              ${result}"
    }]
  }
}; //End of HTML template definition(transform)

$('#json').json2html(data, transform.testSuite);

從以上過渡生成的HTML:

 <li>
       testSuite1
       <ul>
          <li>
          <li>tc1
          <li> 
             <ul>
                <li>
                   ts1
                   <ul>
                      <li>
                         <div>image               -              true</div>
                      </li>
                   </ul>
                </li>
             </ul>
          </li> 
          <li>
             <ul>
                <li>
                   ts2
                   <ul>
                      <li>
                         <div>image               -              true</div>
                      </li>
                   </ul>
                </li>
             </ul>
          </li>
          </li></li>
       </ul>
    </li>

我想要的HTML格式是

<li>
   testSuite1
   <ul>
        <li>tc1
         <ul>
            <li>
               ts1
               <ul>
                  <li>
                     <div>image               -              true</div>
                  </li>
               </ul>
            </li>
         </ul>
<ul>
            <li>
               ts2
               <ul>
                  <li>
                     <div>image               -              true</div>
                  </li>
               </ul>
            </li>
         </ul>
      </li>
       </ul>
</li>

請幫助我刪除json2html過渡中多余的li標簽。

試試下面的代碼。

    var data = [{
      "testSuite": [{
          "testCase": [{
            "testCaseName": "tc1",
            "testStep": [{
                "result": "true",
                "testStepName": "ts1",
                "screenShot": "image"
              }, {
                "result": "true",
                "testStepName": "ts2",
                "screenShot": "image"
              }] //End of TestStep
          }, ],
          "testSuiteName": "testSuite1",
        }] // End of testSuite
    }];

    var transform = {
    // Printing the Execution stack 

        "testSuite": {
            "tag": "ul",
            "children": function() {
                return (json2html.transform(this.testSuite, transform.testSuiteName));
            }
        },

        "testSuiteName": {
            "tag": "li",
            "html": "${testSuiteName}",
            "children": function() {
                return ('<ul>' +json2html.transform(this.testCase, transform.testCaseName) + '</ul>');
            }
        },

        "testCaseName": {
            "tag": "li",
            "html": "${testCaseName}",
            "children": function() {
                return (json2html.transform(this.testStep, transform.testStepRetrieval));
            }
        },

        "testStepRetrieval": {
            "tag": "ul",
            "children": function() {
                return (json2html.transform(this, transform.testStep));
            }
        },

        "testStep": {
            "tag": "li",
            "html": "${testStepName}",
            "children":function(){
              return ('<ul>' + json2html.transform(this, transform.testStepResultDescription) + '</ul>');
             }
         },


    "testStepResultDescription": {
        "tag": "li",
        "children": [{
            "tag": "div",
            "html": "${screenShot}               -              ${result}"
         }]
     }
  };

希望這會幫助你。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM