0

我的项目中有一个有效的搜索管道。它应用在桌子上。表有很多列,如产品名称、类别、数量等。管道搜索所有字段并过滤结果。它区分大小写,我想让它不区分大小写。如果我输入Mobile它会显示结果,但如果我输入mobile它不会显示结果。

搜索.pipe.ts

import { Pipe, PipeTransform } from '@angular/core';
import {isObject} from "rxjs/util/isObject";
import {isEmpty} from "rxjs/operator/isEmpty";
import {empty} from "rxjs/Observer";
@Pipe({
    name: 'search'
})
export class SearchPipe implements PipeTransform
{
    transform(input, searchString)
    {
        if (input == null) return input;
        return input.filter(this.compareWithAllFields, searchString);
    }

    compareWithAllFields(value, index)
    {
        var fields = Object.keys(value);
        for (let i = 0; i < fields.length; i++)
        {
            if (value[fields[i]] != null)
            {
                if (isObject(value[fields[i]]))
                {
                    var childFields = Object.keys(value[fields[i]]);
                    if (childFields.length > 0)
                    {
                        for (let j = 0; j < childFields.length; j++)
                        {
                            if ((value[fields[i]][childFields[j]] + "").indexOf(this.toString()) !== -1)
                            {
                                return true;
                            }
                        }
                    }
                }
                if ((value[fields[i]] + "").indexOf(this.toString()) !== -1)
                {
                    return true;
                }
            }
        }
        return false;
    }
}

输入表格

<input placeholder="Enter Product Details to Search" class="form-control input-lg" type="text" #searchProduct
                               (keyup)="0">

用法

<tr *ngFor="let product of products | search:searchProduct.value;let serial = index">
4

1 回答 1

1

String.prototype.toLowerCase()为了胜利:

compareWithAllFields(value, index)
{
    var fields = Object.keys(value);
    for (let i = 0; i < fields.length; i++)
    {
        if (value[fields[i]] != null)
        {
            if (isObject(value[fields[i]]))
            {
                var childFields = Object.keys(value[fields[i]]);
                if (childFields.length > 0)
                {
                    for (let j = 0; j < childFields.length; j++)
                    {
                        if ((value[fields[i]][childFields[j]] + "").toLowerCase().indexOf(this.toString().toLowerCase()) !== -1)
                        {
                            return true;
                        }
                    }
                }
            }
            if ((value[fields[i]] + "").toLowerCase().indexOf(this.toString().toLowerCase()) !== -1)
            {
                return true;
            }
        }
    }
    return false;
}

参考: https ://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/toLowerCase

于 2016-11-04T15:49:36.560 回答