2013-02-18 82 views
2

我想只顯示第一個tr與id =「firstTr-1」,但它應該從同一個對象獲得數據,其他tr應該有除第一個以外的數據。我應該添加任何註冊表幫助或他們是一個內置的幫手方法!我檢查了文件,但無法弄清楚。使用每個手把模板

模板:

 <script id="firsttemp" type="text/x-handlebars-template"> 
     <tr id="firstTr-1"> 

     <td class="tableFirstCol"><input type="checkbox" class="check" id="childcheck" /></td> 
     <td class="tableFirstCol"><input type="checkbox" class="star" id="childstar" /></td> 
     <td class="tableFirstCol"><input type="checkbox" class="alarm" id="childalarm" /></td> 
     <td class="tableFirstCol">Hero Honda<span>Hero Honda</span></td> 

     </tr> 
     {{#each objects}} 
     <tr> 

     <td class="tableFirstCol"><input type="checkbox" class="check" id="childcheck" /></td> 
     <td class="tableFirstCol"><input type="checkbox" class="star" id="childstar" /></td> 
     <td class="tableFirstCol"><input type="checkbox" class="alarm" id="childalarm" /></td> 
     <td class="tableFirstCol">{{name}}<span>{{name}}</span></td> 

     </tr>{{/each}} 
</script> 

JSON:

var company= [ 

     { 
     "name": "Hero Honda", 
     "lastprice": 310.2, 
     "dayschange": 20.1, 
     "dayshigh": 220.9, 
     "volume": 140.3, 
     "52weekhigh": 200.3, 
     "name2": "herohonda", 
     "dataset": [1200,3000,3200], 
     "lastprice2": "1:10pm", 
     "dayschange2": "8%", 
     "dayshigh2": "1.5%", 
     "volume2": "1:10 Pm", 
     "52weekhigh2": "1:10 Pm" 
     }, 
     { 
     "name": "Hero Honda", 
     "lastprice": 310.2, 
     "dayschange": 20.1, 
     "dayshigh": 220.9, 
     "volume": 140.3, 
     "52weekhigh": 200.3, 
     "name2": "herohonda", 
     "dataset": [3200,3500,5000], 
     "lastprice2": "1:10pm", 
     "dayschange2": "8%", 
     "dayshigh2": "1.5%", 
     "volume2": "1:10 Pm", 
     "52weekhigh2": "1:10 Pm" 
     }] 

回答

2

我覺得你過於複雜的事情。把手更喜歡在把手看到它之前將你的邏輯應用到數據上。因此,不要搞亂一個可能只能在一個地方使用的自定義幫助器,請更改數據並用布爾標誌標記第一個數據,以便您可以在模板中執行一個簡單的{{#if _first}}檢查。

模板應該是這樣的:

<script id="firsttemp" type="text/x-handlebars-template"> 
    {{#each objects}} 
     <tr{{#if _first}} id="firstTr-1"{{/if}}> 
      <!--...--> 
     </tr> 
    {{/each}} 
</script> 

,你可以設置_first標誌是這樣的:

company[0]._first = true; 

演示:http://jsfiddle.net/ambiguous/3Cq9K/


一旦你說出來你的手上還有另一個問題:你正在複製E在您的複選框id屬性,但id attributes must be unique

必須不具有相同id值在文檔中的多個元素。

如果您想避免各種奇怪和令人困惑的問題,您需要在每個複選框上單獨使用id s。

+0

您的回答非常有幫助!謝謝.... – 2013-02-19 05:41:53