组织产品类别 Angular
组织产品类别 Angular
我试图将这段代码应用到一个Angular项目中,纯JS可以正常工作,但在TypeScript的for循环中出现了问题。目前在我的Angular视图中,我只是用*ngFor打印产品列表,但我需要按类别进行分离。我希望用数组方法如map()
或filter()
来替换for循环,但我无法弄清楚如何做到这一点。如何在Angular的*ngFor中实现这个目标?
var products = [ {id: 1, name: 'Apple', category: 'Fruit'}, {id: 2, name: 'Orange', category: 'Fruit'}, {id: 3, name: 'Blueberry', category: 'Jam'}, {id: 4, name: 'Sugar', category: 'Sweet'}, {id: 5, name: 'Candy', category: 'Sweet'}, {id: 6, name: 'Cheese', category: 'Cheese'} ]; //获取类别 let categories = []; for (let i = 0; i < products.length; i++) { if(!categories.includes(products[i].category)){ categories.push(products[i].category); } } console.log(categories); //按类别获取产品 for(item of categories){ console.log(item); let filtered = products.filter(x => x.category == item); console.log(filtered); } /* 期望输出 Fruit {id: 1, name: 'Apple', category: 'Fruit'}, {id: 2, name: 'Orange', category: 'Fruit'}, Jam {id: 3, name: 'Blueberry', category: 'Jam'}, Sweet {id: 4, name: 'Sugar', category: 'Sweet'}, {id: 5, name: 'Candy', category: 'Sweet'}, Cheese {id: 6, name: 'Cheese', category: 'Cheese'} */
Angular视图:
{{product.name}} {{product.category}}
问题:如何根据产品的类别来组织产品的分类?
原因:需要根据产品的类别来显示不同的分类。
解决方法:在组件中通过以下代码来获取唯一的类别集合:
this.categories = [...new Set(this.products.map(item => item.category))];
然后在视图中根据类别来有条件地显示它们:
<div *ngIf="product.category === category">
可以参考以下示例代码:
import { Component } from "/core"; ({ selector: "app-root", templateUrl: "./app.component.html", styleUrls: ["./app.component.css"] }) export class AppComponent { products = [ {id: 1, name: 'Apple', category: 'Fruit'}, {id: 2, name: 'Orange', category: 'Fruit'}, {id: 3, name: 'Blueberry', category: 'Jam'}, {id: 4, name: 'Sugar', category: 'Sweet'}, {id: 5, name: 'Candy', category: 'Sweet'}, {id: 6, name: 'Cheese', category: 'Cheese'} ]; categories; constructor() { this.categories = [...new Set(this.products.map(item => item.category))]; console.log(this.categories); } }
以下是视图代码:
<div> <div *ngFor="let category of categories; index as i;"> <div class="header">{{category}}</div> <div *ngFor="let product of products; index as i;"> <div *ngIf="product.category === category"> {{product.name}} </div> </div> </div> </div>
CSS样式:
.header { font-weight: bold; margin-bottom: 10px; margin-top: 20px; text-decoration: underline; }
可以参考以下示例链接:
https://codesandbox.io/s/bold-kalam-du9n7?fontsize=14&hidenavigation=1&theme=dark
问题的原因是在Angular应用的CategoryProductsComponent文件中,需要将产品按类别分类展示。解决方法是通过遍历产品数组,根据产品的类别将其分组,并将分组后的结果存储在categorizedProducts对象中。然后,在CategoryProdcutsComponent.html文件中,使用*ngFor指令将每个类别的产品展示在页面上。
首先,在CategoryProductsComponent.ts文件中,定义了一个products数组和一个categorizedProducts对象,用于存储按类别分组后的产品。在ngOnInit生命周期钩子函数中,通过遍历产品数组,将每个产品按类别分组,并存储在categorizedProducts对象中。最后,使用Object.keys方法获取categorizedProducts对象的所有键,并通过map方法将键对应的值转换为一个新的数组。最后,将分组后的结果打印到控制台。
然后,在CategoryProdcutsComponent.html文件中,使用*ngFor指令遍历categorizedProducts数组,将每个类别的产品展示在页面上。在循环中,首先显示类别的标题,然后使用*ngFor指令遍历当前类别下的产品数组,展示每个产品的信息。
通过以上方法,可以实现将产品按类别分类展示的功能。
问题的出现原因:
问题是由于数据的结构不适合在ngFor中显示而产生的。原始数据的结构是一个包含多个产品的数组,每个产品对象都有一个category属性,但在ngFor中,我们需要将产品按照类别进行分组,并且将每个类别的产品显示在一个分组中。
解决方法:
有两种解决方法。第一种方法是重新组织数据,使其更适合在ngFor中使用。我们可以通过遍历原始产品数组,创建一个新的对象,其中key是类别,value是该类别对应的产品数组。然后,我们将这个对象转换成一个数组,其中每个元素是一个类别对应的产品数组。这样,我们就可以在ngFor中使用这个数组来显示数据。
第二种方法是直接对原始产品数组进行排序。我们可以在ngOnInit方法中使用一个自定义的compare函数来比较产品的类别,并将产品数组进行排序。然后,我们可以在ngFor中遍历排序后的数组,并使用getPreviousCategory方法来检查前一个产品的类别,如果与当前产品的类别不同,则显示一个新的类别标题。
下面是整理后的文章:
首先,我们来看第一种解决方法。我们可以通过遍历原始产品数组,创建一个新的对象,其中key是类别,value是该类别对应的产品数组。然后,我们将这个对象转换成一个数组,其中每个元素是一个类别对应的产品数组。这样,我们就可以在ngFor中使用这个数组来显示数据。以下是示例代码:
import { Component, OnInit } from "/core"; @Component({ selector: "my-app", templateUrl: "./app.component.html", styleUrls: ["./app.component.css"] }) export class AppComponent implements OnInit { name = "Angular"; products = [ { id: 1, name: "Apple", category: "Fruit" }, { id: 2, name: "Orange", category: "Fruit" }, { id: 3, name: "Blueberry", category: "Jam" }, { id: 4, name: "Sugar", category: "Sweet" }, { id: 5, name: "Candy", category: "Sweet" }, { id: 6, name: "Cheese", category: "Cheese" } ]; arrangedProducts = {}; ngOnInit() { this.products.forEach(p => { if (!this.arrangedProducts || !this.arrangedProducts[p.category]) { this.arrangedProducts[p.category] = [p]; } else { this.arrangedProducts[p.category].push(p); } }); this.arrangedProducts = Object.keys(this.arrangedProducts).map( key => this.arrangedProducts[key] ); console.log(this.arrangedProducts); } }
然后,在HTML模板中使用ngFor来显示数据:
{{arrayOfProducts[0].category}}
{{product.name}}
第二种解决方法是直接对原始产品数组进行排序。我们可以在ngOnInit方法中使用一个自定义的compare函数来比较产品的类别,并将产品数组进行排序。然后,我们可以在ngFor中遍历排序后的数组,并使用getPreviousCategory方法来检查前一个产品的类别,如果与当前产品的类别不同,则显示一个新的类别标题。以下是示例代码:
import { Component, OnInit } from "/core"; @Component({ selector: "my-app", templateUrl: "./app.component.html", styleUrls: ["./app.component.css"] }) export class AppComponent implements OnInit { name = "Angular"; products = [ { id: 1, name: "Apple", category: "Fruit" }, { id: 6, name: "Cheese", category: "Cheese" }, { id: 2, name: "Orange", category: "Fruit" }, { id: 4, name: "Sugar", category: "Sweet" }, { id: 3, name: "Blueberry", category: "Jam" }, { id: 5, name: "Candy", category: "Sweet" }, ]; arrangedProducts = []; ngOnInit() { this.arrangedProducts = this.products.sort(this.compare); console.log(this.arrangedProducts) } compare(a, b) { if (a.category < b.category) { return -1; } if (a.category > b.category) { return 1; } return 0; } getPreviousCategory(i){ if(i-1 < 0) return; return this.arrangedProducts[i-1].category; } }
然后,在HTML模板中使用ngFor来显示数据:
{{product.category}}
{{product.name}}
这两种方法都可以解决在Angular中对产品按类别进行分组显示的问题,具体使用哪种方法取决于你的需求和偏好。希望这些解决方法对你有帮助!