Ember.js集合迭代中的位置索引 [英] Positional index in Ember.js collections iteration
问题描述
{{each itemsArray}}
{{name}}
{{/ each}}
寻找一种方式来获得类似的东西:
{{#items items)}
{{name}} - {{index}} th。
{{/ each}}
更新: p>
根据@ebryn的评论,以下代码的作品不使用每个项目的嵌套视图:
< script type =text / x-handlebars>
{{#collection contentBinding =App.peopleController}}
索引{{contentIndex}}:{{content.name}}< br />
{{/ collection}}
< / script>
a href =http://jsfiddle.net/WSwna/14/> http://jsfiddle.net/WSwna/14/
虽然要有一些像adjustIndex这样的东西,就需要一个嵌套的视图。
这是老问题,但仍然有很多点击。在当前版本的Ember.JS中,可以使用 _view.contentIndex
在循环中显示当前索引。
{{#each}}
/ pre>
索引:{{_view.contentIndex}}
{{/ each}}
如果您需要调整后的索引(例如从1开始),则可能会创建可重用的助手
increment
Ember.Handlebars.registerBoundHelper('increment',function(integer){
return integer + 1 ;
});
那么你可以用以下方式使用它
{{/ each}}
更新
从ember 1.11开始也可以
{{#each people as | person index |}}
索引:{{increment index} }
{{/ each}}
Is there a way to get positional index during iteration in ember.js?
{{#each itemsArray}}
{{name}}
{{/each}}
I'm looking for a way to have something like:
{{#each itemsArray}}
{{name}} - {{index}}th place.
{{/each}}
Update:
As per the comment by @ebryn the below code works without using a nested view for each item:
<script type="text/x-handlebars">
{{#collection contentBinding="App.peopleController"}}
Index {{contentIndex}}: {{content.name}} <br />
{{/collection}}
</script>
Although to have something like adjustedIndex, a nested view would be required.
It's old question but still gets a lot of hits. In the current version of Ember.JS one can use _view.contentIndex
to display current index inside the loop.
{{#each}}
Index: {{_view.contentIndex}}
{{/each}}
If you need an adjusted index (for instance starting from 1) then there is a possibility of creating reusable helper increment
Ember.Handlebars.registerBoundHelper('increment', function(integer) {
return integer + 1;
});
then you would use it in the following way
{{#each}}
Index: {{increment _view.contentIndex}}
{{/each}}
Update
Starting with ember 1.11 you can do as well
{{#each people as |person index|}}
Index: {{increment index}}
{{/each}}
这篇关于Ember.js集合迭代中的位置索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!