过滤($过滤器)从排除不必要的属性的对象列表匹配值 [英] Filtering ($filter) matching values from list of objects excluding unnecessary properties

查看:158
本文介绍了过滤($过滤器)从排除不必要的属性的对象列表匹配值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过定义修改智能表全局搜索的行为我的自定义过滤器 ST-设置过滤器=myCustomFilter。这

  .filter('myCustomFilter',['$过滤',函数($过滤器){
    返回功能(输入,predicate){
        返回$过滤器('过滤器')(输入,predicate,FALSE);
    }
}])

智能表输入对象的列表和它们的性质列的名称。 我想只过滤表列的给定子集

如果我CONSOLE.LOG predicate 键入的我看到类似对象{$后:富} 。这意味着该滤波器正在寻找列表中的对象的针对一个匹配的任何的属性(即,列)。

有没有一种方法能根据我的需求正确地定义predicate(例如,排除不必要的属性?


解决方案

只是把这种排斥逻辑过滤器predicate。您可以直接传递predicate通过其函数名的HTML。

一个predicate功能是需要一些参数(通常是一个对象),并返回一个true或false值的函数。这predicate通常会被一个数组中的每个元素单独计算。在 $滤波器的情况下中,predicate决定是否包含或排除该元素。这意味着你只需要编写一个函数来正确地包括合适的元素,同时排除那些错误的。

从棱角分明的 $过滤文档 中,predicate有这样的签名:


  

功能(价值指数,数组):一个predicate功能可用于写入任意过滤器。函数被调用为阵列的每个元素,与元件,其索引,和整个阵列本身作为参数。<​​/ P>

您不必使用所有的投入,往往你只需要第一个参数(元素本身)。您的过滤predicate可能是这样的:

  $ scope.customFilter =功能(项指标,数组){
  VAR排除= item.testResult =='失败';
  //。包括()逻辑搜索过滤器
  包括VAR = item.name.toLowerCase()的indexOf($ scope.filterData.name.toLowerCase())&GT; -1;  返回包含与放大器;&安培; !除外;
};

然后就是通过predicate功能本身的过滤器在HTML

 &LT; D​​IV NG重复=数据OBJ |过滤器:customFilter&GT;
     做的东西与滤波的数据
&LT; / DIV&GT;

我联系上plnkr 演示,演示的基本概念如何你的predicate可以与表单的其他部分进行交互,包括一些元素,同时也排除其他。


关于智能表。我不知道什么你的智能表期待,但如果只是想要一个 $过滤器对象,那么你就应该能够利用 $过滤器predicate 参数。只是通过你的predicate功能(如上面所示)到您的自定义过滤, FILTERNAME:ARG1 。我得知这个语法从托德座右铭的页面(第3节:过滤器重复使用参数)。

过滤器定义

  //同样作为过滤器,而是一个叫好一点
.filter('appLevelFilter',['$过滤',函数($过滤器){
    返回功能(输入,predicate){
        返回$过滤器('过滤器')(输入,predicate,FALSE);
    }
}])

HTML

 &LT; D​​IV NG重复=数据OBJ | appLevelFilter:customFilter&GT;
     做的东西与滤波的数据
&LT; / DIV&GT;

I'm trying to modify the behavior of Smart Table global search by defining my custom filter st-set-filter="myCustomFilter". Here it is

.filter('myCustomFilter', ['$filter', function($filter){
    return function(input, predicate){
        return $filter('filter')(input, predicate, false);
    }
}])

Smart Table input is a list of objects and their properties are the names of columns. I want to filter only a given subset of table columns.

If I console.log predicate after typing foo I see something like Object {$: "foo"}. It means that the filter is looking for a match against any property (i.e. column) of the objects in the list.

Is there a way to properly define predicate according to my demand (e.g. excluding unnecessary properties?

解决方案

Just put that exclusion logic in your filter predicate. You can pass that predicate directly to the HTML via its function name.

A predicate function is a function that takes some parameters (typically an object) and returns a true or false value. This predicate will usually be evaluated individually on each element of an array. In the case of $filter, the predicate determines whether to include or exclude that element. This means you just need to write that function to properly include the right elements while excluding the wrong ones.

From the documentation for angular's $filter, the predicate has this signature:

function(value, index, array): A predicate function can be used to write arbitrary filters. The function is called for each element of the array, with the element, its index, and the entire array itself as arguments.

You don't have to use all the inputs, and often you'll only need the first argument (the element itself). Your filtering predicate might look like this:

$scope.customFilter = function(item, index, array) {
  var excluded = item.testResult == 'fail';
  //.contains() logic for a search filter 
  var included = item.name.toLowerCase().indexOf($scope.filterData.name.toLowerCase()) > -1;

  return included && !excluded;
};

And then just pass the predicate function itself to your filter in the HTML:

<div ng-repeat="obj in data | filter:customFilter">
     Do stuff with the filtered data
</div>

I've linked a demo on plnkr that demonstrates the basic idea of how your predicate can interact with other parts of the form to include some elements while also excluding others.


Regarding the smart table. I'm not sure about what your smart table is expecting, but if is just wants a $filter object, then you should be able to leverage the predicate argument of the $filter. Just pass your predicate function (such as the one shown above) into your custom filter with filterName:arg1. I learned about this syntax from Todd Motto's page (section 3: filters for repeats with arguments).

Filter definition

//same as your filter, but named a bit better
.filter('appLevelFilter', ['$filter', function($filter){
    return function(input, predicate){
        return $filter('filter')(input, predicate, false);
    }
}])

HTML

<div ng-repeat="obj in data | appLevelFilter:customFilter">
     Do stuff with the filtered data
</div>

这篇关于过滤($过滤器)从排除不必要的属性的对象列表匹配值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆