Ascending and Descending Sort in Angular 4

前端 未结 1 920
轻奢々
轻奢々 2021-01-03 03:42

Why is it that sort function is working :

Transaction Date 

        
1条回答
  •  礼貌的吻别
    2021-01-03 04:23

    The issue here is :

    Nested property of object, orderBy must be providing the sorting on the basis of first level of properties

    I am considering, inner should be look like this,

    {
        transaction_date : '10/12/2014'
        user : {
            name : 'something',
            ...
        }
    }
    

    try to make this object like, take all sortable property on first level ( OR you must have to change the orderBy that way )

    {
        transaction_date : '10/12/2014'
        user_name : 'something',
        user : {
            name : 'something',
            ...
        }
    }
    

    And try.

    
        User 
    
    

    Add records.map(record => record['user_name'] = record.user.name); , to your transform function , like this :

    This will make the object as I suggested :

    export class OrderByPipe implements PipeTransform {
    
      transform(records: Array, args?: any): any {
        if(records && records.length >0 ){
    
        records.map(record => record['user_name'] = record.user.name); // add here
    
        return records.sort(function(a, b){
            ....
          }
        };
    }
    

    0 讨论(0)
提交回复
热议问题