我正在使用来自firebase的数据创建https://github.com/MIt9/angular-4-data-table。我的问题是,我有'name'参数的route naviagation。我点击链接,我想只显示这个等于我传递的'name'参数。我点击链接,向我展示db in objects of objects中的所有产品。如何过滤?我需要先进行过滤,然后再将其传递给我的数据表。
component.ts文件:
productCategory: Product[];
filteredProducts: any[];
subscription: Subscription;
tableResource: DataTableResource<Product>;
name;
items: Product[] = [];
itemCount: number;
constructor(
private productService: ProductsService,
private route: ActivatedRoute
) {
this.route.paramMap.subscribe(params => {
let name = params.get("name");
this.name = name;
this.subscription = this.productService.getAll().subscribe(products => {
this.filteredProducts = this.productCategory = products;
// here i think i want to filter input data and pass this into variable
let category = products.filter(products => products.category);
console.log(category);
this.InitializeTable(category);
});
});
}
private InitializeTable(products: Product[]) {
this.tableResource = new DataTableResource(products);
this.tableResource.query({ offset: 0 }).then(items => (this.items = items));
this.tableResource.count().then(count => (this.itemCount = count));
}
reloadItems(params) {
if (!this.tableResource) return;
this.tableResource.query(params).then(items => (this.items = items));
}
答案 0 :(得分:0)
Javascript支持原生filter
方法。 (如果您只想要第一个,还需要find
;从您的问题中不清楚这是否足够。)
因此,例如,如果要查找数组中具有与某个目标“name”匹配的name
属性的第一项,您可以执行以下操作:
let myItem = myArray.find(item => item.name === "name");
或者,如果您想要过滤掉您的数组,以便只拥有与目标“名称”匹配的项目,则可以使用类似的filter
方法:
let myItems = myArray.filter(item => item.name === "name"); // myItems is an array
您可以参考MDN了解有关这些功能的更多信息: