Angular Material 2 DataTable Sorting with nested objects

前端 未结 11 917
孤街浪徒
孤街浪徒 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 18:02

    I customized for multiple nested object level.

    this.dataSource.sortingDataAccessor =
      (data: any, sortHeaderId: string): string | number => {
        let value = null;
        if (sortHeaderId.includes('.')) {
          const ids = sortHeaderId.split('.');
          value = data;
          ids.forEach(function (x) {
            value = value? value[x]: null;
          });
        } else {
          value = data[sortHeaderId];
        }
        return _isNumberValue(value) ? Number(value) : value;
      };
    

提交回复
热议问题