Angular2 ng2智能表排序

时间:2017-11-08 07:56:57

标签: angular ng2-smart-table

angular 2的 ng2-smart-table 排序功能区分大小写。是否有任何选项可以将排序表数据设置为不区分大小写

2 个答案:

答案 0 :(得分:7)

如果你实现这个以确保在compareFunction之后添加:只是想扔掉。如下图所示......

columns: {
    group_name: {
        title: 'Groupname',
        compareFunction:(direction: any, a: any, b: any) => {
            // Converting strings to lowercase
            let first = typeof a === 'string' ? a.toLowerCase() : a;
            let second = typeof b === 'string' ? b.toLowerCase() : b;

            if (first < second) {
                return -1 * direction;
            }
            if (first > second) {
                return direction;
            }
            return 0;
        }
    }
}

答案 1 :(得分:4)

  

您可以将自定义排序功能作为sort()方法中的第4个参数。

示例:

let COMPARE_INSENSITIVE = (direction: any, a: any, b: any) => {
  // Converting strings to lowercase
  let first = typeof a === 'string' ? a.toLowerCase() : a;
  let second = typeof b === 'string' ? b.toLowerCase() : b;

  if (first < second) {
     return -1 * direction;
  }
  if (first > second) {
    return direction;
  }
  return 0;
}

ng2-smart-table 使用以下默认COMPARE功能:

export class LocalSorter {

  protected static COMPARE = (direction: any, a: any, b: any) => {
    if (a < b) {
     return -1 * direction;
    }
    if (a > b) {
      return direction;
    }
    return 0;
  }

  static sort(data: Array<any>, field: string, direction: string, customCompare?: Function): Array<any> {

    const dir: number = (direction === 'asc') ? 1 : -1;
    const compare: Function = customCompare ? customCompare : this.COMPARE;

    return data.sort((a, b) => {
      return compare.call(null, dir, a[field], b[field]);
    });
  }
}