2016-03-21 9 views
2

HTML oluşturmak için JSON2HTML kitaplığını kullanıyorum. Ben kod bloğu onclick.Following ile sorunu olan yaşıyorumjclm.html'de nasıl kullanılır:

**JavaScript** 
 
    
 

 
    var data =[ 
 
    {   
 
    "testSuite":[ 
 
    { 
 
    "testCase":[ 
 
     { 
 
     "testCaseName":"Login Succesfull TC (nested error)", 
 
     "testCaseResult":"false", 
 
     "testCaseScreenShot":"image", 
 
     "testCaseTimeStamp":"0:00:13.258", 
 
     "testStep":[ 
 
     { 
 
     "testStepresult":"true", 
 
     "testStepName":"ub_cti_inputText - TC (nested error)", 
 
     "testStepScreenShot":"image", 
 
     "testStepTimeStamp":"0:00:13.258", 
 
     }, 
 
     { 
 
     "testStepresult":"true", 
 
     "testStepName":"ub_cti_inputText1111111111 - TC (nested error)", 
 
     "testStepScreenShot":"image", 
 
    \t "testStepTimeStamp":"0:00:13.258", 
 
     }, 
 
    \t { 
 
     "testStepresult":"false", 
 
     "testStepName":"ub_cti_inputText - TC (nested error)", 
 
     "testStepScreenShot":"image", 
 
    \t "testStepTimeStamp":"0:00:13.258", 
 
      } 
 
      ] //End of TestStep 
 
     },  \t \t \t  
 
    \t ],  // End of testSuite1 
 
      "testSuiteName":"LoginAndTicketStatus_suite - TS (nested error)", 
 
    \t \t "testSuiteResult":"false", 
 
    \t \t "testSuiteTimeStamp":"0:00:15.238" 
 
        }, 
 
    \t \t \t 
 
    \t \t \t ] //End of TestSuite JSON 
 
     }, 
 
     
 
    ]; //End of data 
 
    var transform = { 
 
     "testSuite": { 
 
      "tag": "ul", 
 
      "children": function() { 
 
    \t \t return (json2html.transform(this.testSuite, transform.getTestSuiteName)); 
 
      } 
 
     }, 
 
      "getTestSuiteName": { 
 
      "tag": "li", 
 
    \t \t "nowrap":"true", 
 
    \t \t "class":"closed", 
 
    \t "children":[ 
 
       {"tag":"a","href":"#testSuiteLink", 
 
    \t \t    "onclick": 
 
    \t \t function() 
 
    \t \t { 
 
    \t \t $(this).toggleClass('closed'); 
 
    \t  $(this).toggleClass('open'); 
 
    \t \t toggle(this); 
 
    \t \t }, 
 
    \t  "children":[ 
 
    \t \t {"tag":"big", 
 
    \t \t \t "children":[ 
 
    \t \t \t //Apply font color for result of testSuite 
 
        {"tag":"font",  \t \t \t \t 
 
    \t \t \t \t "color":function() 
 
    \t \t \t \t { 
 
    \t if(data[0].testSuite[0].testSuiteResult=="true") 
 
     \t \t { 
 
    \t \t return "Green"; 
 
    \t \t } 
 
    \t \t else 
 
     \t \t { 
 
    \t \t return "red"; 
 
    \t \t }  \t \t \t \t 
 
    \t \t }, 
 
    \t \t "html": "${testSuiteName}", 
 
    \t \t }, 
 
     \t \t ] 
 
    \t \t }] 
 
    \t \t }, 
 
    \t {"tag":"ul","children":function() 
 
    \t \t { 
 
    \t \t return (json2html.transform(this.testCase, transform.testCase)); 
 
    \t \t } 
 
    \t \t }] \t \t \t 
 
    \t \t }, 
 

 
    \t "testCase": { 
 
      "tag": "li", 
 
    \t "nowrap":"true", 
 
    \t "class":"closed",  \t 
 
    \t \t "children":[ 
 
    \t \t { 
 
    \t \t "tag":"a","href":"#testcase","onclick": function() 
 
    \t \t { 
 
    \t \t $(this).toggleClass('closed'); 
 
    \t \t $(this).toggleClass('open'); 
 
    \t \t toggle(this); 
 
    \t \t }, 
 
    \t \t "children":[ 
 
       {"tag":"big","children":[ 
 
       {"tag":"font","color":function() 
 
    \t \t {  \t \t \t \t 
 
    \t \t if(this.testCaseResult=="true") 
 
    \t \t { 
 
    \t \t \t return "Green"; 
 
    \t \t } 
 
    \t \t \t else 
 
    \t \t \t { 
 
    \t \t \t return "red"; 
 
    \t \t \t }  \t \t \t \t 
 
    \t \t },"html":function() 
 
    \t \t \t { 
 
    \t \t \t return this.testCaseName; 
 
    \t \t \t }} 
 
       ]} 
 
      ]}, 
 
    \t \t {"tag":"ul","children":function() 
 
    \t \t { 
 
    \t return ( json2html.transform(this.testStep, transform.testStep)); 
 
    \t \t } 
 
    \t }] 
 
     }, 
 
     
 
    \t "testStep": { 
 
      "tag": "li", 
 
    \t \t "nowrap":"true", 
 
    \t \t "class":"closed", 
 
    \t \t 
 
    \t \t "onclick":function() 
 
    \t \t \t { 
 
    \t \t \t $(this).toggleClass('closed'); 
 
    \t \t \t $(this).toggleClass('open'); 
 
    \t \t \t toggle(this); 
 
    \t \t \t }, 
 
      "children":[ 
 
       {"tag":"a","href":"#testcase","onclick": function() 
 
    \t \t \t { 
 
    \t \t \t $(this).toggleClass('closed'); 
 
    \t \t \t $(this).toggleClass('open'); 
 
    \t \t \t toggle(this); 
 
    \t \t \t }, 
 
    \t \t \t "children":[ 
 
        {"tag":"big","children":[ 
 
    \t \t \t \t {"tag":"font","color":function() 
 
    \t \t \t \t {  \t \t \t 
 
    \t \t \t \t if(this.testStepresult=="true") 
 
     \t \t \t \t { 
 
    \t \t \t \t return "green"; 
 
    \t \t \t \t } 
 
    \t \t \t \t else 
 
    \t \t \t \t { 
 
    \t \t \t \t \t return "red"; 
 
    \t \t \t \t } 
 
    \t \t \t \t 
 
    \t \t \t \t },"html":function() 
 
    \t \t \t \t { 
 
    \t \t \t \t for(element in this) { 
 
    \t \t \t console.log("Element received "+element+" -----44"); 
 
    \t \t \t return this.testStepName ; 
 
    \t \t \t } 
 
    \t \t } 
 
    \t \t } 
 
      ]} 
 
     ]}  \t \t \t \t \t \t \t , 
 
    \t {"tag":"ul","children":[  \t \t \t \t \t \t \t  
 
     { 
 
     "tag":"li","nowrap":"true","class":"closed","children":[ 
 
    \t { 
 
      "tag":"a","href":"#step","onclick":function() 
 
    \t { 
 
    \t \t $(this).toggleClass('closed'); 
 
    \t \t $(this).toggleClass('open'); 
 
    \t \t toggle(this); 
 
    \t \t },"children":[ 
 
    \t \t {"tag":"big","children":[ 
 
    \t \t {"tag":"font","color":function() 
 
    \t \t { \t \t \t \t 
 
    \t \t if(this.testStepresult=="true") 
 
    \t \t \t { 
 
        return "Green"; 
 
    \t \t \t } 
 
    \t \t else 
 
    \t \t {  \t \t \t \t \t 
 
    \t \t \t return "red"; 
 
    \t \t } 
 
    \t \t },"html":function() 
 
    \t \t { 
 
    \t \t  return this.testStepresult ; 
 
    \t \t } 
 
    \t \t  } 
 
       ]} 
 
    \t \t ]} 
 
    \t \t ]} 
 
    \t \t ]} 
 
    \t ]},  \t 
 
     }; //End of HTML template definition(transform) 
 
$(document).ready(function() 
 
{ 
 
$('#json').json2html(data, transform.testSuite); 
 
});
**Css Styling** 
 
\t 
 

 
body { font-family: monospace; } 
 
    \t ul.report, 
 
    \t ul.report ul, 
 
    \t ul.report li { margin: 0; padding: 0; } 
 
    \t ul.report ul { padding-left: 1em; } 
 
    \t ul.report li a { text-decoration: none; } 
 
    a { 
 
     text-decoration: none; 
 
    } 
 
    li::before { 
 
     content: "⊞" 
 
    \t } 
 
    \t 
 
    \t ul.report li { list-style: none; } 
 
    { 
 
     list-style-type: none; 
 
    }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script> 
 
<script src="http://json2html.com/js/jquery.json2html.js"></script> 
 
<script src="http://json2html.com/js/json2html.js"></script> 
 

 
**HTML** 
 

 
<div id="json" style="width:95%;margin:5%"></div>

Beklenen Çıktı: LoginAndTicketStatus_suite üzerinde

tıklayarak - Giriş ilgili çok başarılı TC geçiş TS (iç içe hatası) (yuvalanmış hata

Oturum Açmak Üzere Tıklama Başarılı TC (iç içe hata) geçişi ub_cti_inputText - TC (iç içe hata), ub_cti_inputText1- TC (iç içe hata), ub_cti_inputText - TC (iç içe hata).

Herhangi bir yardım büyük beğeni topluyor!

+0

kod Düzenlendi var –

cevap

1

Öncelikle JSON dönüşümünüzü daha okunabilir bir biçimde düzenlemeyi denemek isteyebilirsiniz.

Sorunuzda, çekirdeğinizdeki json2html.transformu iç içe geçirdiğinizden bu yana, olayları işlemeyi bildiğinden emin olmanız gerekir. Bu jquery olayları işlemek için temel kütüphanesinin söyler böylece

return (json2html.transform(this.testSuite, transform.getTestSuiteName,{'events':true})); 

gibi seçenekleri: Sen {gerçek olayları} belirterek yapabilirsiniz. Aksi takdirde sadece bu yüzden

return ($('div').json2html(this.testSuite, transform.getTestSuiteName)); 

gibi bir kukla div öğesi üzerinde jquery eklentisi diyebiliriz İşte (farklı yöntemlerle hem birlikte) bu etkinlikleri doğru arayacak güncellenen kod

var transform = { 
    "testSuite": { 
     "tag": "ul", 
     "children": function() { 
     return ($('div').json2html(this.testSuite, transform.getTestSuiteName)); 
     } 
    }, 
     "getTestSuiteName": { 
     "tag": "li", 
     "nowrap":"true", 
     "class":"closed", 
     "children":[ 
      {"tag":"a","href":"#testSuiteLink", 
        "onclick": 
     function() 
     { 
      $(this).toggleClass('closed'); 
      $(this).toggleClass('open'); 
      toggle(this); 
     }, 
     "children":[ 
      {"tag":"big", 
      "children":[ 
      //Apply font color for result of testSuite 
       {"tag":"font",     
       "color":function() 
       { 
    if(data[0].testSuite[0].testSuiteResult=="true") 
     { 
     return "Green"; 
     } 
     else 
     { 
     return "red"; 
     }     
     }, 
     "html": "${testSuiteName}", 
     }, 
     ] 
     }] 
     }, 
    {"tag":"ul","children":function() 
     { 
     return (json2html.transform(this.testCase, transform.testCase,{'events':true})); 
     } 
     }]   
     }, 

    "testCase": { 
     "tag": "li", 
     "nowrap":"true", 
     "class":"closed",   
     "children":[ 
     { 
     "tag":"a","href":"#testcase","onclick": function() 
     { 
     $(this).toggleClass('closed'); 
     $(this).toggleClass('open'); 
     toggle(this); 
     }, 
     "children":[ 
      {"tag":"big","children":[ 
      {"tag":"font","color":function() 
     {     
     if(this.testCaseResult=="true") 
      { 
      return "Green"; 
     } 
      else 
      { 
      return "red"; 
      }     
     },"html":function() 
      { 
      return this.testCaseName; 
      }} 
      ]} 
     ]}, 
     {"tag":"ul","children":function() 
     { 
    return ( json2html.transform(this.testStep, transform.testStep,{'events':true})); 
     } 
     }] 
    }, 

    "testStep": { 
     "tag": "li", 
     "nowrap":"true", 
     "class":"closed", 

     "onclick":function() { 
      console.log('here'); 
      $(this).toggleClass('closed'); 
      $(this).toggleClass('open'); 
      toggle(this); 
     }, 
     "children":[ 
      {"tag":"a","href":"#testcase","onclick": function() 
      { 
      $(this).toggleClass('closed'); 
      $(this).toggleClass('open'); 
      toggle(this); 
      }, 
      "children":[ 
       {"tag":"big","children":[ 
       {"tag":"font","color":function() 
       {    
       if(this.testStepresult=="true") 
       { 
       return "green"; 
       } 
       else 
       { 
        return "red"; 
       } 

       },"html":function() 
       { 
       for(element in this) { 
      console.log("Element received "+element+" -----44"); 
      return this.testStepName ; 
      } 
      } 
     } 
     ]} 
    ]}        , 
    {"tag":"ul","children":[          
    { 
    "tag":"li","nowrap":"true","class":"closed","children":[ 
    { 
     "tag":"a","href":"#step","onclick":function() 
    { 
     $(this).toggleClass('closed'); 
     $(this).toggleClass('open'); 
     toggle(this); 
     },"children":[ 
     {"tag":"big","children":[ 
     {"tag":"font","color":function() 
     {     
      if(this.testStepresult=="true") 
      { 
       return "Green"; 
      } 
     else 
     {      
      return "red"; 
     } 
     },"html":function() 
     { 
       return this.testStepresult ; 
     } 
      } 
      ]} 
      ]} 
     ]} 
     ]} 
     ]},  
    }; //End of HTML template definition(transform)