2017-10-16 30 views
0

我确实有2种排序值。点击我应该需要更新我的排序顺序,并需要更新HTML。但目前我没有得到任何更新。如何更新不同值的排序?

Live Demo for update in Twiddle

一个是数情形,另一个是由日期( “2017年7月7日”)。

这里是我的尝试: //根本不工作!

sortByDateOfPurchase: Ember.computed(function(){ 
      return privateArray.sort(function(a,b){ 
        console.log(b['date_of_purchase']); 
        return parseInt(b['date_of_purchase']) - parseInt(a['date_of_purchase']);//07/07/2017 

      }) 
     }), 

//works but not constant 
     sortByAmountSpent:Ember.computed(function(){ 
      return privateArray.sort(function(a,b){ 
        return parseInt(b['amount-spent']) - parseInt(a['amount-spent']); 
      }) 
     }), 

     sortTheCards(title){ 
//changing the sorted array but not working 
      if(title=='amountSpent'){ 
//setting for date value 
        this.set('sortedCards', this.get('sortByAmountSpent')); 
        return; 
      } 

      //setting for number value 
      this.set('sortedCards', this.get('sortByDateOfPurchase')); 


     }, 

    selectedDetails : [], 
    transactionService: Ember.inject.service(), 

     filterValue:Ember.observer('filterBy', function(){ 

      var sortBy = this.get('filterBy'); 
      this.sortTheCards(sortBy); 

     }), 

     init() { 
      this._super(...arguments); 
      this.set('selectedDetails', this.get('transactionService.selectedTransactions')); 

      var sortBy = this.get('filterBy'); 
      var nestedCards = this.get('nestedCards'); 

      this.sortTheCards(sortBy); 


     }, 

回答

1

这里是我用于排序的一个例子。希望这可以帮助。

export default Ember.Component.extend({ 
    reverseSort: true, // default sort in ascending order 
    sortedData: Ember.computed.sort('totalResults', 'sortDefinition'), 
    sortBy: 'date', // default sort by date 
    sortDefinition: Ember.computed('sortBy', 'reverseSort', function() { 
     let sortOrder = this.get('reverseSort') ? 'desc' : 'asc'; 
     return [`${this.get('sortBy')}:${sortOrder}`]; 
    }), 
}) 

这里reverseSort是用于通过任一ascdesc排序它。 sortBy是要用于排序的对象的属性。

这里是一个类似的文章,你可以查阅 ember computed sort