3 回答

TA貢獻1775條經驗 獲得超11個贊
為此,您可以調用一種方法來過濾(或排序,無論您喜歡什么)subjectWithTopics 數組以顯示它。
假設您從右側隱藏的表格開始,單擊按鈕后將顯示所選的 classId。
因此,我們從打印所有按鈕的 ngFor 開始:
<button *ngFor="let c of classes" (click)="selectClass(c)">
{{ c.classname }}
</button>
至少在這個例子中,右手表格應該顯示給定 classId 的所有選定元素,或者如果它沒有元素則隱藏。我會這樣做:
<table *ngIf="selectedSubjects && selectedSubjects.length > 0" >
<tr>
<th> Subject name </th>
<th> Subject name </th>
</tr>
<tr *ngFor="let subject of selectedSubjects">
<th> {{ subject.subjectName }} </th>
<th> {{ subject.topicName }} </th>
</tr>
</table>
在控制器(.ts 文件)中,您編寫“selectClass”函數,它將過濾 subjectWithTopics 數組,并將其放入我們新創建的數組“selectedSubjects”(您應該在控制器的類中定義):
selectClass(selectedClass){
this.selectedSubjects = subjectWithTopics.filter( topic => topic.classId === selectedClass.classid);
}
這應該夠了吧!

TA貢獻1866條經驗 獲得超5個贊
您可以將主題存儲在變量中并在 html 中呈現該變量。
您可以編寫一個函數來在單擊某個類時獲取 Subjects。例如
let subjects = [];
...
getSubjects(classId){
this.subjects = this.subjectWithTopics.filter(subject=>subject.classId===classId)
}
然后在html中的for循環中渲染這個函數。例如
<div ngFor="let subject of subjects">
</div>

TA貢獻1806條經驗 獲得超8個贊
組件.html
<!-- display unique subject array and on setTopics() will filter topics by subject name-->
<h3>Classes</h3>
<div *ngFor="let classname of classes" style="display:inline-block;">
<button style="margin: 0px 5px; color: blue;" (click)="setTopics(classname)">{{classname | uppercase}} </button>
</div>
<h3>Topics</h3>
<div *ngFor="let topic of topics">
<div (click)="setTopics(topic)">{{topic.topicName}} </div>
</div>
組件.ts
import { Component, VERSION, OnInit } from '@angular/core';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: [ './app.component.css' ]
})
export class AppComponent implements OnInit{
name = 'Angular ' + VERSION.major;
classesArray = [
{classid:1,subjectName:"hindi", topicName : "topic1 of hindi class id 10" },
{classid:1,subjectName:"hindi", topicName : "topic2 of hindi class id 10" },
{classid:1,subjectName:"English", topicName : "topic1 of English class id 10" },
{classid:1,subjectName:"English", topicName : "topic2 of English class id 10" },
{classid:1,subjectName:"English", topicName : "topic3 of English class id 10" },
{classid:2,subjectName:"hINDI", topicName : "topic1 of hINDI class id 20" },
{classid:2,subjectName:"HINDI", topicName : "topic2 of hINDI class id 20" },
{classid:2,subjectName:"HINDI", topicName : "topic3 of HINDI class id 20" },
{classid:2,subjectName:"English", topicName : "topic1 of English class id 20" },
{classid:2,subjectName:"English", topicName : "topic2 of English class id 20" },
{classid:2,subjectName:"English", topicName : "topic3 of English class id 20" },
];
classes = [];
topics = [];
ngOnInit() {
// find unique class names
this.classes = this.classesArray.map((obj) => obj.subjectName.toLowerCase());
this.classes = this.classes.filter((v, i) => this.classes.indexOf(v) === i);
// default selected subject to hindi
this.setTopics('hindi');
}
// filter topics by subjectname and generate new topics array
setTopics(subjectName) {
this.topics = this.classesArray.filter((classes)=> classes.subjectName.toLowerCase() === subjectName.toLowerCase())
}
}
這是工作演示https://stackblitz.com/edit/santosh-angular-array-filter
添加回答
舉報