3gwebtrain
3gwebtrain

Reputation: 15299

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

I do have 2 type of sorting values. on click I should need to update my sorting order and require to update html. But at present I am not getting any update.

Live Demo for update in Twiddle

one is number scenario and other one is by date ( "07/07/2017" ).

here is my try: //not at all works!!

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


      },

Upvotes: 0

Views: 109

Answers (1)

Sandeep
Sandeep

Reputation: 605

Here is an example that I used for sorting. Hope this helps.

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}`];
  }),
})

Here reverseSort is for sorting it by either asc or desc. sortBy is attr of the object that you want to use to sort.

Here is a similar article you can check ember computed sort

Upvotes: 1

Related Questions