indexof不是打字稿中的函数,而是基于数字过滤



当我尝试在下拉列表中过滤月份值时,它显示indexof()不是函数..代码是..

@Pipe({
    name: "monthFilter",
})
export class MonthStatusPipe implements PipeTransform {
      transform(array: any[], query: number): any {  
        console.log("query", query);
        if (query) {
            return _.filter(array, row => row.LicenseMonth.indexOf(query) > -1);
        }
        return array;
    }
}

有没有其他方法可以根据值过滤下拉列表..

我的 json 数据是:

{
    "BusinessId": 1549,
    "OrganizationNumber": "992060867",
    "CompanyName": "Litra Containerservice AS ",
    "Address": "Industrigata 62",
    "Zipcode": "2619 Lillehammer",
    "Mobile": "98238925",
    "Telephone": "66789485",
    "ConnectedTo": "Admin",
    "ConnectedToId": "123",
    "HSEManager": "Marina Magerøy",
    "BusinessContact": "Lars Andre Skogstad",
    "ContactMobile": "48499613",
    "BusinessContactTelephone": "45283769",
    "NoOfEmployees": "15",
    "Status": "Active",
    "ModifiedOn": "2016-10-01T23:55:01.033",
    "LicenseMonth": 10,
    "Category": "Godstransport på vei"
  },
这是因为

LicenseMonth实际上是一个数字。

这应该有效:

@Pipe({name: 'monthFilter'})
export class MonthStatusPipe implements PipeTransform {
      transform(arr: any[], num?: number): any {
        return typeof num !== 'undefined' 
           ? arr.filter(e => e.LicenseMonth === num)
           : arr;
    }
}

我还想建议一种更通用的方法,而不是对特定字段使用特定的管道。

您可以改为编写更通用的解决方案。例如:

<p>{{ rows | filterBy: ['LicenseMonth']: 42 }}</p>

可以编写自己的版本,也可以使用我用一堆有用的管道编写的库。

ngx-pipes : https://github.com/danrevah/ngx-pipes

过滤器按管道: https://github.com/danrevah/ngx-pipes#filterby

最新更新