如何在 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))
  }