Angular Material 2 DataTable Sorting with nested objects

前端 未结 11 919
孤街浪徒
孤街浪徒 2020-11-29 17:25

I have a normal Angular Material 2 DataTable with sort headers. All sort are headers work fine. Except for the one with an object as value. These doesn\'t sort at all.

11条回答
  •  没有蜡笔的小新
    2020-11-29 17:49

    Just add this to your data source and you will be able to access the nested object

    this.dataSource.sortingDataAccessor = (item, property) => {
        // Split '.' to allow accessing property of nested object
        if (property.includes('.')) {
            const accessor = property.split('.');
            let value: any = item;
            accessor.forEach((a) => {
                value = value[a];
            });
            return value;
        }
        // Access as normal
        return item[property];
    };
    

提交回复
热议问题