Sort

28 views
Skip to first unread message

Marc Roussel

unread,
Nov 30, 2021, 1:18:41 PM11/30/21
to Angular and AngularJS discussion
Hi,

Is there someone who can tell me why the sort isn't working in this scenario ?
StackBlitz Example

Thank you!

Marc Roussel

unread,
Nov 30, 2021, 1:45:47 PM11/30/21
to Angular and AngularJS discussion
Solved by having my  displayedColumns with path to the property as well as matColumnDef matching the path.property

HTML:
matColumnDef="Address.ADD_Company"

CODE:
    displayedColumns: string[] = [
        "Address.ADD_Company",
        "Address.ADD_StreetNo",    
        "Address.ADD_StreetName",
        "Address.ADD_City",
        "Address.ADD_State",
        "Address.ADD_Country",
        "Address.ADD_Zip",
        "Address.ADD_CloseTime",
        "Contacts",
        "Action"
    ];

and using a generic found here : https://stackoverflow.com/a/52214226/2966506

    pathDataAccessor(item: any, path: string): any {
        return path.split('.')
          .reduce((accumulator: any, key: string) => {
            return accumulator ? accumulator[key] : undefined;
          }, item);
    }
       
    //-----------------------
    // Get the addresses list
    //-----------------------
    getData() {
        this.isProcessing = true;

        this.addressBookService.getAddresses().subscribe((getAddressBookResult: GetAddressBookResult) => {
            if (getAddressBookResult !== null) {
                this.dataSource = new MatTableDataSource(getAddressBookResult.AddressesContainer);
                this.dataSource.sortingDataAccessor = this.pathDataAccessor;
                this.dataSource.sort = this.sort;          
            }

            this.isProcessing = false;
        });
    }

Reply all
Reply to author
Forward
0 new messages