如何在JSon2HTML库中调用onClick事件上的函数

时间:2016-03-16 07:29:34

标签: javascript html json json2html

我在点击链接时使用Json2hTML库我想调用一个函数。但它不能正常工作

var data = [{
  "testSuite": [{
        "testCase": [{
          "testCaseName": "tc1",
          "testCaseResult": "false",
          "testCaseScreenShot": "imageofTestCase",
          "testStep": [

              {
                "testStepResult": "true",
                "testStepName": "ts1",
                "testStepScreenShot": "image"
              }, {
                "testStepResult": "true",
                "testStepName": "ts2",
                "testStepScreenShot": "image"
              }, {
                "testStepResult": "false",
                "testStepName": "ts3",
                "testStepScreenShot": "image"
              }
            ] //End of TestStep
        }, ], // End of testSuite1
        "testSuiteName": "GoogleTestSuite",
        "testSuiteResult": "false",
        "testSuiteScreenShot": "image"
      },

    ] //End of TestSuite JSON 
}];

Html Library:

 var transform = {
     "testSuite": {

       "tag": "ul",
       "class": "report",
       "children": [{
         "tag": "a",
         "href": "#testSuiteLink",
         "children": [{
           "tag": "font",
           "color": function() {
             if (data[0].testSuiteInformation[0].testSuiteResult == "true") {
               return "green";
             } else {
               return "red";
             }
           },
           "onclick": function() {
             $(".getTestStepClass").toggle();
           },
           "children": function() {

             return (json2html.transform(this.testSuite, transform.getTestSuiteName));
           }
         }]
       }]
     },
     "getTestSuiteName": {
       "tag": "li",
       "id": "testSuiteLink",
       "class": "getTestSuiteNameClass",
       "html": "${testSuiteName}",
       "onclick": function() {
         //This function is not being called
         * *
         return ('<ul>' + json2html.transform(this.testCase, transform.testCase) + '</ul>'); * *

       },

     },

     "testCase": {
       "tag": "li",
       "class": "testCaseNameClass",
       "children": [{
           "tag": "font",
           "color": function() {
             if (this.testCaseResult == "true") {
               return "green";
             } else {
               return "red";
             }
           },
           return (json2html.transform(this, transform.getTestCaseName));
         }
       }]

   },

   "getTestCaseName": {
     "tag": "ul",
     "class": "getTestCaseNameClass",
     "html": "${testCaseName}",
     "id": "testCaseLink",
     "children": function() {
       return (json2html.transform(this.testStep, transform.testStep))
     },

   },

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

   },

   "getTestStep": {
     "tag": "li",
     "class": "getTestStepClass",
     "children": [
       //Apply font color for result of testSuite
       {
         "tag": "font",
         "color": function() {
           if (this.testStepResult == "true") {
             //flag=flag+1;
             return "green";
           } else {
             return "red";
           }
         },

         "html": "${testStepName}",
         "children": function() {
           if (this.testStepResult == "false")
             return ('<ul>' + json2html.transform(this, transform.testStepResultDescription) + '</ul>');

         }
       }
     ]

   },


   "testStepResultDescription": {
     "tag": "li",
     "class": "testStepResultDescriptionClass",
     "children": [{
       "tag": "div",
       "html": "${testStepScreenShot}",
     }]
   },
   "testCaseResultDescription": {
     "tag": "li",
     "children": [{

       "tag": "div",
       "html": "${testCaseScreenShot}",
     }]
   }
 };

预期产出:

GoogleTestSuite(点击此链接应显示以下内容)

  • Tc1(功能结果)
  • Tc2(功能结果)

0 个答案:

没有答案