Positional index in Ember.js collections iteration

前端 未结 8 801
梦如初夏
梦如初夏 2020-12-01 11:08

Is there a way to get positional index during iteration in ember.js?

{{#each itemsArray}}
     {{name}}
{{/each}}

I\'m looking for a way to

8条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-01 11:11

    As of Ember 9.8 and pre-1.0 you can wrap the "contentIndex" with a view in order to get at the virtual parent (the {{#each}}). If you don't add the view, your context ends up being either the main template, an item in your list or whatever you manually set with your {{#with}}. It is not impossible to get at the {{#each}} from the JS side but it is a lot more of a pain flipping through those child views.

    {{#each App.peopleController}}
        {{#view}}
            Index {{view._parentView.contentIndex}}: {{name}} 
    {{/view}} {{/each}} ...OR... {{#each people in App.peopleController}} {{#view}} Index {{view._parentView.contentIndex}}: {{people.name}}
    {{/view}} {{/each}}

    Just in-case you would like a fiddler.

    DEMO

    Note: You can create a view and do a this.get("_parentView.contentIndex") to get at the index if you want to modify the number at all.

提交回复
热议问题