2010-11-18 1 views
7

Sto usando il modello di plugin per jQuery e non so come ottenere l'indice degli articoli: http://api.jquery.com/category/plugins/templates/Get Index in jQuery modello

Ecco il mio codice:

<script id="optionTmpl" type="text/x-jquery-tmpl"> 
    <table width="100%" border="0" cellspacing="0" cellpadding="0"> 
    {{each Answers}} 
     <tr> 
      <th><input type="radio" name="group1" value="${this.AnswerID}" /></th> 
      <td>${this.AnswerText}</td><!-- add number in this line---> 
     </tr> 
    {{/each}} 
    </table> 
</script> 

voglio mostrare la risposta nel formato come questo

1) answer1, 2) answer2, 3) answer3

o

a) answer1, b) answer2, c) answer3

Cosa devo fare?

risposta

21

C'è un implicito $index (e $value) disponibile all'interno della {{each}} loop, è possibile utilizzare tale qui:

<script id="optionTmpl" type="text/x-jquery-tmpl"> 
    <table width="100%" border="0" cellspacing="0" cellpadding="0"> 
    {{each Answers}} 
     <tr> 
      <th><input type="radio" name="group1" value="${this.AnswerID}" /></th> 
      <td>${this.AnswerText} ${$index + 1}</td> 
     </tr> 
    {{/each}} 
    </table> 
</script> 

probabilmente si vorrà aggiungere 1 perché è 0 based, come ho sopra.