簡體   English   中英

如何在Ember.js模板中顯示項目數?

[英]How to display number of item in Ember.js template?

我可以像這樣顯示來自對象的數據:

 <ul>
  {{#each people}}
    <li>Hello, {{name}}!</li>
  {{/each}}
</ul>

但我想要添加這樣的記錄號:

<ul>
  {{#each people}}
    {{NUMBER_OF_PERSON}} <li>Hello, {{name}}!</li>
  {{/each}}
</ul>

這個怎么做?

我假設你的意思是顯示索引。 目前,您需要使用車把助手。 這個要點顯示了一個示例https://gist.github.com/burin/1048968滾動瀏覽評論看起來有幾種方法。

Handlebars有@index和@key,但目前這些都可以使用emberjs。

在控制器中,您可以編寫如下函數:

no_of_person: function(){
                 return this._parentView.contentIndex;
              }.property('people')

在模板中, {{no_of_person}}將顯示記錄的索引號。

希望這可以幫助!

您可以使用CSS計數器 它也適用於表格。 以下是從MDN獲取的示例:

 ul { counter-reset: section; /* Creates a new instance of the section counter with each ol element */ list-style-type: none; } li:before { counter-increment: section; /* Increments only this instance of the section counter */ content: counters(section,".") " "; /* Adds the value of all instances of the section counter separated by a ".". */ } 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM