繁体   English   中英

如何在 Angular 中使搜索不区分大小写

[英]How to make search case insensitive in Angular

我有一个数据名称列表,我想搜索它。 无论情况如何,它都应该给出结果。

这就是我所拥有的:

public groups = [{ name: '"Grx-1"', selected: false }, { name: '"Grx-man-2"', selected: false }, { name: '"Grx-up-3"', selected: false }];

queryGroups(groupName) {
        this.groups = this.totalGroupsList.filter((group) => {
            if (group.userId.includes(groupName) || group.dps.includes(groupName) || group.sourceType.includes(groupName)) {
                return true;
            } else {
                let isRole = false;
                group.role.forEach((role) => {
                    if (role.name.includes(groupName)) {
                        isRole = true;
                        return;
                    }
                });
                if (isRole === false) {
                    return false;
                } else {
                    return true;
                }
            }
        });
    }

如果我搜索“Grx”,我会得到所有结果。 我希望如果我搜索“grx”,我应该得到所有结果。

您可以使用toLowerCase()

role.name.toLowerCase().includes(groupName.toLowerCase())

您必须使用一种以上的搜索方法:

  queryGroups(groupName: string) {
    this.groups = this.totalGroupsList.filter((group) => {
      let isExist = this.searchFunc(groupName, group.userId)
        || this.searchFunc(groupName, group.dps)
        || this.searchFunc(groupName, group.sourceType)
      if (isExist) {
        return true;
      } else {
        let isRole = false;
        group.role.forEach((role) => {
          if (this.searchFunc(groupName, role.name)) {
            isRole = true;
            break;
          }
        });
        return isRole !== false;
      }
    });
  }

  private searchFunc(searchKey, searchTarget): boolean {
   if(!searchKey) {
      return false;
    }
    return (searchTarget.toLocaleUpperCase().includes(searchKey.toLocaleUpperCase())) ||
      (searchTarget.toUpperCase().includes(searchKey.toUpperCase())) ||
      (searchTarget.includes(searchKey.toLocaleUpperCase())) ||
      (searchTarget.includes(searchKey.toUpperCase())) ||
      (searchTarget.toLocaleUpperCase().includes(searchKey)) ||
      (searchTarget.toUpperCase().includes(searchKey)) ||
      (searchTarget.includes(searchKey))
  }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM