【问题标题】:How to update the `sorting` oder by different value?如何按不同的值更新“排序”顺序?
【发布时间】:2017-10-16 12:57:06
【问题描述】:

我确实有 2 种排序值。单击时,我应该需要更新排序顺序并需要更新 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 );


      },

【问题讨论】:

    标签: ember.js


    【解决方案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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-02-22
      • 2017-11-12
      • 1970-01-01
      • 1970-01-01
      • 2021-10-07
      • 2021-03-24
      • 1970-01-01
      相关资源
      最近更新 更多