6

在Angular 2的ng2 -smart-table排序功能是区分大小写的。是否有任何选项可以使排序表数据不区分大小写

4

2 回答 2

11

如果你实现这个以确保你在 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;
        }
    }
}
于 2017-11-08T08:50:45.630 回答
6

您可以提供自定义排序函数作为 sort() 方法中的第四个参数。

例子:

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]);
    });
  }
}
于 2017-11-08T08:39:58.133 回答