Ember.js Grouping Results

后端 未结 3 714
长发绾君心
长发绾君心 2021-02-04 18:49

I\'m trying to achieve a \"group by\" functionality in Ember.js and its proving more difficult than I originally thought.

[{date: \'2014-01-15T16:22:16-08:00\',          


        
3条回答
  •  北恋
    北恋 (楼主)
    2021-02-04 19:10

    You can use Ember.computed.groupBy like this:

    ((Ember) ->
      # Example:
      #
      #   productsByCategory: Em.computed.groupBy 'products', 'category'
      #
      #   each view.productsByCategory
      #     category.name
      #     each content
      #       content.name
      #
      Ember.computed.groupBy = (collection, groupBy) ->
        dependentKey = "#{collection}.@each.#{groupBy}"
    
        Ember.computed dependentKey, ->
          result = []
          @get(collection).forEach (item) ->
            unless result.findBy groupBy, item.get(groupBy)
              data = content: []
              data[groupBy] = item.get(groupBy)
              result.pushObject Ember.Object.create(data)
            result.findBy(groupBy, item.get(groupBy)).get('content').pushObject item
          result
    ) Ember
    

提交回复
热议问题