角材料表排序

时间:2020-04-01 15:20:50

标签: angular typescript angular-material

  ngAfterViewInit() {
    this.isInitCompleted = true;
    this.dataSource.sortingDataAccessor = (item, property) => {
      if (this.tableConfig.hasOwnProperty('dateColumns') && ( this.tableConfig.dateColumns.indexOf(property) > -1)) {

        if (item[property]) {
          //if date format in yyyy-mm-dd or  yyyy-mm-dd hr:min:sec
          if (item[property].includes('-')) {
            const dateParts = item[property].split('-');
            let dateValue = dateParts[2]
            //if date format in date time format ie yyyy-mm-dd hr:min:sec
            if (dateValue.includes(' ')) {
              dateValue = dateValue.split(' ')[0]
            }
            // month is 0-based, that's why we need dataParts[1] - 1
            const dateObject = new Date(+dateParts[0], dateParts[1] - 1, +dateValue);
            const newDate = new Date(dateObject);
            return newDate;
          }
          else {
            //date format in dd.mm.yyyy
            const dateParts = item[property].split('.');
            // month is 0-based, that's why we need dataParts[1] - 1
            const dateObject = new Date(+dateParts[2], dateParts[1] - 1, +dateParts[0]);
            const newDate = new Date(dateObject);
            return newDate;
          }

        }
        return item[property];
      } else {


        if (typeof item[property] == 'string') {
          // if(item[property]=='MLT'){
          //   item[property]= "777684"
          // }
          //if its string then check if string is a number. If it is Number convert string to number for sorting.Eg.amount:'12235.9' convert the string to number ie 12235.9 so sorting will work
          if (item[property] && !isNaN(item[property])) {
            return Number(item[property])
          }
          return item[property].toLowerCase();
        }
        else {
          return item[property];
        }// this is for non string data values
      }
    };
  }

我有一张表,其中有一列,并且我有这样的值: var myArray = [“ MLT”,“ 3343”,“ 8776”,“ MLT”,“ MLT”,“ 3433”] 但是排序不起作用,因为列中存在字符和数字,因此我之前在表中具有数字或字符串的数据。

0 个答案:

没有答案