角ng-repeat filter:¿如何指定filter字段?

时间:2022-08-23 23:49:32

Ok, looks easy, but not.

看起来很简单,但不是。

I want to perform a simply angular filter, but i need to specify which fields of my array use in filter.

我想要执行一个简单的角度过滤器,但是我需要指定在过滤器中使用数组的哪个字段。

A Basic filter is:

一个基本的过滤器:

 <li ng-repeat="person in app.people | filter: app.search">

A One-Field filter is:

一个字段过滤:

<li ng-repeat="person in app.people | filter: {Surname: app.search}">

How can i specify more than one field for filter? Something like:

如何为过滤器指定多个字段?喜欢的东西:

<li ng-repeat="person in app.people | filter: {Name,Surname: app.search}">

Here a Plunker with Basic Filter and One-Field filter: https://jsfiddle.net/odLz1v71/3/

这里有一个带基本过滤器和单场过滤器的柱塞器:https://jsfiddle.net/odLz1v71/3/

2 个解决方案

#1


2  

In this case, I would suggest you add a filter function in your controller like this:

在这种情况下,我建议您在控制器中添加一个过滤器函数,如下所示:

vm.searchFilter = function(item){
    // Add your own search logic here
    return item.Name.includes(vm.search) || item.Surname.includes(vm.search);
}

And then use it like this:

然后像这样使用它:

<li ng-repeat="person in app.people | filter: app.searchFilter">

#2


0  

I don't think you can do this with the basic filter directly, but you can provide a function for the filter filter to use that will do this check for you:

我不认为你可以直接用基本的过滤器做这个,但是你可以为过滤器提供一个函数用来做这个检查:

  vm.checkName = function (value) {
    return value.Name.indexOf(vm.search) > -1 || value.Surname.indexOf(vm.search) > -1;
  };

See: https://jsfiddle.net/odLz1v71/4/

参见:https://jsfiddle.net/odLz1v71/4/

#1


2  

In this case, I would suggest you add a filter function in your controller like this:

在这种情况下,我建议您在控制器中添加一个过滤器函数,如下所示:

vm.searchFilter = function(item){
    // Add your own search logic here
    return item.Name.includes(vm.search) || item.Surname.includes(vm.search);
}

And then use it like this:

然后像这样使用它:

<li ng-repeat="person in app.people | filter: app.searchFilter">

#2


0  

I don't think you can do this with the basic filter directly, but you can provide a function for the filter filter to use that will do this check for you:

我不认为你可以直接用基本的过滤器做这个,但是你可以为过滤器提供一个函数用来做这个检查:

  vm.checkName = function (value) {
    return value.Name.indexOf(vm.search) > -1 || value.Surname.indexOf(vm.search) > -1;
  };

See: https://jsfiddle.net/odLz1v71/4/

参见:https://jsfiddle.net/odLz1v71/4/