- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我正在尝试模拟 AngularJS 中的 OrderBy。
给定这种数组。我需要按 car_category 过滤汽车。
[
{
"car_category": 3,
"name": "Fusion",
"year": "2010"
},
{
"car_category": 2,
"name": "Mustang",
"year": "2010"
},
{
"car_category": 3,
"name": "Fiesta",
"year": "2010"
},
]
这是我的代码到目前为止的样子
car.component.html
<div *ngIf="products">
<ul *ngFor="let product of products | filterBy: car_category">
<li>{{car.name}}</li>
</ul>
</div>
car.component.ts
import { Component, OnInit } from '@angular/core';
import { CarService } from '../car.service';
import { ICars } from '../ICars';
import { FilterByPipe } from '../filter-by.pipe';
@Component({
selector: 'app-home-page',
templateUrl: './car.component.html',
styleUrls: ['./car.component.css']
})
export class CarComponent implements OnInit {
cars: Array<ICars>;
errorMessage: string;
filteredCars: any;
car_category= 3;
constructor(private _carService: CarService) { }
ngOnInit() {
this._carService.getCars()
.subscribe(
cars => this.cars = cars,
error => this.errorMessage = error
);
}
}
filet-by.pipe.ts
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({
name: 'filterBy'
})
export class FilterByPipe implements PipeTransform {
transform(value, args) {
if (!args[0]) {
return value;
} else if (value) {
return value.filter(item => {
// tslint:disable-next-line:prefer-const
for (let key in item) {
if ((typeof item[key] === 'string' || item[key] instanceof String) &&
(item[key].indexOf(args[0]) !== -1)) {
return true;
}
}
});
}
}
}
我的管道需要如何重构?
更新这就是我的 pipe 现在的样子。注意汽车是数字,年份显示为字符串
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({
name: 'filterBy'
})
export class FilterByPipe implements PipeTransform {
transform(value, args) {
if (!args[0]) {
return value;
} else if (value) {
return value.filter(item => {
// tslint:disable-next-line:prefer-const
for (let key in item) {
if ((typeof item[key] === 'number' || item[key] instanceof Number) &&
(item[key].indexOf(args[0]) !== -1)) {
return true;
}
}
});
}
}
}
最佳答案
编写自定义通用管道可能很棘手。如果您查看 Angular 1
source code ,你会明白我的意思。
因此我建议使用库,例如 ng-pipes .老实说,我还没有测试过 Angular 2
版本,但它对于 Angular 1
来说非常方便。
关于Angular 4 FilterBy 管道,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43633367/
我正在使用 vue.js 构建一个包含一长串所选项目的表单。我正在使用此处记录的动态选择列表:http://012.vuejs.org/guide/forms.html#Dynamic_Select_
如何使用 filterBy 函数使用多个值来过滤列表? 我的场景是 - 我有一个控制台列表,我想根据 console_id 对其进行过滤。 不幸的是,我无法控制 JSON,因此每个控制台都有不同的 I
我正在使用 Vue.js(目前为 v.1.x,将很快更新),我想要一个包含一些数据的表行表。我正在使用 Vue.js 组件通过 filterBy 过滤表行,但我希望在完成任何搜索/过滤之前隐藏表行。
如何通过在多个搜索键中搜索进行过滤? 我正在尝试这样的事情,但是(当然)它不会起作用: 最佳答案 filterBy 自定义过滤器没有文档 AFAIK,但您可以使用方法来制作您自己的过滤器: var
我正在尝试模拟 AngularJS 中的 OrderBy。 给定这种数组。我需要按 car_category 过滤汽车。 [ { "car_category": 3, "name"
我正在创建一个通过 filterKey 过滤的 filterBy 函数: filterBy (data, filterKey) { data = data.filter(function (row
我有一项服务可以管理我应用中的所有错误和警报。代码看起来像这样 服务 import Ember from 'ember'; export default Ember.Service.extend({
我正在尝试对通过 JSON 填充的表进行一些过滤(使用 bootstrap-table by wenzhixin )。 部分 HTML: Filter
目前使用 wenzhixin 的 Bootstrap Table 库。这里这个 https://github.com/wenzhixin/bootstrap-table 。我遇到了 filterby
我正在使用 Primefaces 的组件 p:dataTable,但在对对象列表进行排序时遇到了麻烦。 我有一个电子邮件列表,在此列表中我有另一个列表及其电子邮件别名。这是我的代码: public c
在 vue.js 中如何一个 filterBy 多个属性? 考虑以下数据结构。 new Vue({ el: body, data: { items: [ {name: 'thi
我正在使用 vue 可拖动组件将对象从一个 div 拖放到另一个 div。我还使用 filterBy 过滤 v-model。问题是当我过滤(例如得到 1 个结果)并尝试拖放所有项目中的第一个项目而不是
我正在使用 ExtJS 4.1。我正在使用商店的 clearFilter() 从商店中删除过滤器。我正在使用 filterBy 方法对商店应用过滤器。我正在过滤名称不是罗纳尔多的所有记录。 清除过滤器
假设我有一个具有 2 个属性的组件:我如何能够基于 localID 属性和 Ember.compulated.filterBy 宏创建计算属性? localID: 2, data: [ { i
我想通过来自第一个 ng-repeat 的动态值过滤第二个 ng-repeat。我的代码是: {{all.Category}} 我尝试了上面的代码,但第二个 ng-repeat
我正在尝试开发一个具有延迟加载的数据表。通过使用 UserService(即简单的 DAO),可以从持久化上下文中正确加载未过滤和已过滤的记录。我 100% 确定该问题与正在使用的 DAO 无关,因为
我是一名优秀的程序员,十分优秀!