Positional index in Ember.js collections iteration

前端 未结 8 783
梦如初夏
梦如初夏 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}} <br />
        {{/view}}
    {{/each}}
    
    ...OR...
    
    {{#each people in App.peopleController}}
        {{#view}}
            Index {{view._parentView.contentIndex}}: {{people.name}} <br />
        {{/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.

    0 讨论(0)
  • 2020-12-01 11:12

    I have modified a bit ud3323 solution using collection. Check here: http://jsfiddle.net/johngouf/WSwna/13/

    <script type="text/x-handlebars">    
    {{#collection contentBinding="App.peopleController"}}
      {{#view App.PersonView contentBinding="this"}}
        Index {{_parentView.contentIndex}}: {{content.name}} {{adjustedIndex}} <br />
      {{/view}}
    {{/collection}}
    </script>
    
    App = Ember.Application.create();
    App.peopleController = Ember.ArrayController.create({
     content: [ { name: 'Roy' }, { name: 'Mike' }, { name: 'Lucy' } ]
    });
    
    App.PersonView = Ember.View.extend({
     content: null,
     // Just to show you can get the current index here too...
     adjustedIndex: function() {
        return this.getPath('_parentView.contentIndex') + 1;
     }.property()
    });
    

    ​​

    0 讨论(0)
  • 2020-12-01 11:14

    In RC6 CollectionView provides contentIndex propery for each rendered view of collection. Each helper uses CollectionView in its implementation so uou can access index in this way:

    {{#each itemsArray}}
        {{_view.contentIndex}}
    {{/each}}
    
    0 讨论(0)
  • 2020-12-01 11:14

    Actually yes you can get the position of the current index using the {{each}} helper. You have to create a view for every item in a list and then use {{_parentView.contentIndex}}.

    <script type="text/x-handlebars">
    {{#each App.peopleController}}
      {{#view App.PersonView contentBinding="this"}}
        Index {{_parentView.contentIndex}}: {{content.name}} {{adjustedIndex}} <br />
      {{/view}}
    {{/each}}
    </script>
    

    App = Ember.Application.create();
    
    App.peopleController = Ember.ArrayController.create({
      content: [ { name: 'Roy' }, { name: 'Mike' }, { name: 'Lucy' } ]
    });
    
    App.PersonView = Ember.View.extend(Ember.Metamorph, {
      content: null,
      // Just to show you can get the current index here too...
      adjustedIndex: function() {
        return this.getPath('_parentView.contentIndex') + 1;
      }.property()
    });
    

    See this jsFiddle for a working example.

    0 讨论(0)
  • 2020-12-01 11:19

    This isn't currently a feature of Handlebars or Ember.Handlebars. We have contentIndex available inside #collection/Ember.CollectionView. I think it's useful to support in #each too. Please file an issue at the GitHub repository: https://github.com/emberjs/ember.js/issues

    0 讨论(0)
  • 2020-12-01 11:23

    As of Ember 1.11.0, index is an optional parameter in each blocks:

    {{#each items as |item index|}}
      {{item.name}} is at index {{index}}
    {{/each}}
    
    0 讨论(0)
提交回复
热议问题