2015-04-21 23 views
1

您好我正在使用Mustache JS來使用JSON獲取數據。我的模板如下。

   <script id="patientTabl" type="text/template"> 
      <table class="bordered"> 
      <thead> 
      <tr> 
      <th>First Name</th> 
      <th>Last Name</th> 
      <th>Phone Number</th> 
      <th>Patient Id</th> 
      <th>SSN</th> 
      </tr> 
      </thead> 
      <tbody> 
      {{#patients}} 
      <tr> 

      <td data-title="First Name">{{firstName}}<div href="#" class="tool-tip"> 

      <span class="right-arrow"> 
      <img class="callout" src="images/callout.gif" /> 
      <ul> 
      <li><a href="update-patient.html?id={{id}}">View/Update Patient</a></li> 
      <li><a href="JavaScript:getInsurPlans()" id="viewInsurPlans">View Insurance Plans</a></li> 
      <li><a href="patient-admission.html?id={{id}}">Create Patient Admission</a></li> 
      </ul> 

      </span> 
      </td> 
      <td data-title="Last Name">{{lastName}}</td> 
      <td data-title="Phone Number">&nbsp;{{phones}}</td> 
      <td data-title="Patient Id">{{id}}</td> 
      <td data-title="SSN">{{ssn}}</td> 

      </tr> 
      {{/patients}} 
      </tbody> 
      </table> 
      </script> 

爲此,我正在寫jQuery代碼以將數據提取到模板中。我得到的迴應是數據沒有插入到模板中。請幫忙

  $.getJSON('/ePhase-web/patients', function (data) { 


      var template = $('#patientTabl').html(); 
      var html = Mustache.to_html(template, data); 

      $('#sampleArea').html(html); 
      }); 

回答

0

很難說出什麼問題。我沒有看到任何明顯的錯誤。所以通用的調試是按順序的。

大多數瀏覽器都有某種網絡選項卡。它是否顯示正在提出的請求?請求是否成功?只有在請求成功時纔會調用方法。

如果請求成功,您可以調試代碼本身。在開發控制檯打開的情況下,您可以將單詞調試器放置在執行暫停的位置,以便您可以檢查變量的值(通過鼠標懸停在它們上面)

是否jQuery選擇的patientTab提供了正確的元素結果是?

「數據」是什麼樣的?調用mustache.to_html時是否引發錯誤? html的樣子是什麼?

$(「#sampleArea」)是否找到任何元素?

我認爲一個接一個地回答這些問題會指導您修復錯誤或詢問更加可解的答案。另請參見this article on debugging in chrome

+0

感謝您的回覆Jauco。我想問題是對象的數組。你有任何想法如何獲取對象的數組? – Sundeep

+0

你的意思是說「數據」包含一個數組,並且你想要一個數組中的每個項目的耐心表? – Jauco

+0

我能夠在有單個對象,但獲取的時候就像下面我不能去取[{ 「菜單」:「文件」, },{ 「菜單」:「文件1」, }] – Sundeep