如何按不同的值更新“排序”顺序?

How to update the `sorting` oder by different value?

我有两种类型的排序值。单击我应该需要更新我的排序顺序并要求更新 html。但是目前我没有收到任何更新。

Live Demo for update in Twiddle

一个是数字场景,另一个是按日期(“07/07/2017”)。

这是我的尝试: //根本不起作用!!

   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 );


      },

这是我用来排序的例子。希望这有帮助。

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