首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Angular2 NG2-智能表排序

Angular2 NG2-智能表排序
EN

Stack Overflow用户
提问于 2017-11-08 07:56:57
回答 2查看 8K关注 0票数 6

在角2的ng2-smart-table中,排序功能区分大小写。是否有使排序表数据不区分大小写的选项?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-11-08 08:39:58

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

示例:

代码语言:javascript
复制
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使用以下默认比较函数:

代码语言:javascript
复制
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]);
    });
  }
}
票数 6
EN

Stack Overflow用户

发布于 2017-11-08 08:50:45

如果您实现这一点,以确保在compareFunction之后添加a:,则只想抛出它。如下所示..。

代码语言:javascript
复制
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;
        }
    }
}
票数 11
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47174365

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档