Angular:使用 *ngfor将数据链接到动态表
我正在创建一个动态表。下面我的JSON结构的问题在于,学生对象不包含对象中的主题数组,而是通过链接[]定义学生[]和主题[],其中为学生一个学科定义了链接。
您有什么建议如何在上创建 *ngfor?
JSON结构看起来像这样:
export class StudentsAndSubjects {
info: SchoolInformation;
}
export class SchoolInformation {
students: Students[];
subjects: Subjects[];
links: Links[];
}
export class Students {
id: string; //example: student_1
name: string;
surname: string;
};
export class Subjects{
id: string; //example: subject_1
name: string;
};
export class Links{
studentId: string; //example: student_1
subjectId: string; //example: subject_1
};
表结构:
@Component({
selector: 'app-student-and-subjects',
template: `
<table>
<tr *ngFor="let row of rowHeaders">
<td *ngFor="let item of data.info.students.name">
<span *ngIf="row !== 'Subjects'">{{ item[row] }}</span>
</td>
<table *ngIf="row === 'Subjects'">
<tr **???**>
<td>{{ subject}}</td>
</tr>
</table>
</tr>
</table>
`;
})
export class StudentsAndSubjects {
@Input() data: StudentSubjectData;
readonly rowHeaders = ['Student','Subjects']
}
I am creating a dynamic table. The problem with my json structure below is that the Students object does not contain Subjects array within the object, but rather Students[] and Subjects[] are defined through Links[], where there is link defined for student a subject.
The output of the table should look like this:
Do you have any suggestions how to create *ngFor in ??? area that links each student to its subjects?
The json structure looks like this:
export class StudentsAndSubjects {
info: SchoolInformation;
}
export class SchoolInformation {
students: Students[];
subjects: Subjects[];
links: Links[];
}
export class Students {
id: string; //example: student_1
name: string;
surname: string;
};
export class Subjects{
id: string; //example: subject_1
name: string;
};
export class Links{
studentId: string; //example: student_1
subjectId: string; //example: subject_1
};
The table structure:
@Component({
selector: 'app-student-and-subjects',
template: `
<table>
<tr *ngFor="let row of rowHeaders">
<td *ngFor="let item of data.info.students.name">
<span *ngIf="row !== 'Subjects'">{{ item[row] }}</span>
</td>
<table *ngIf="row === 'Subjects'">
<tr **???**>
<td>{{ subject}}</td>
</tr>
</table>
</tr>
</table>
`;
})
export class StudentsAndSubjects {
@Input() data: StudentSubjectData;
readonly rowHeaders = ['Student','Subjects']
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,我认为您需要对桌子进行重组。由于主题表与特定学生相关联,因此它可能应该生活在包含该学生数据的
td
中:另请注意,我更改了您的
*ngfor =“让数据项目。 info.students.name“
,因此它可以通过data.info.students
而不是data.info.students.name
迭代,因为您需要学生实例数据(和data.info.students.name
不是集合)。为了显示学生名称,我用
{{item [row]}}
用{{student.name}}
替换。然后,要显示该学生的主题,您将需要创建一种接受学生对象(或仅仅是其
id
)的方法,然后解析数据并返回学生的相关主题。该方法是您要问的缺少
*ngfor
中所需的方法。(或者,当然,您可以提前将数据转换为映射学生将其映射到学科的映射对象。)
因此,您要问的缺少
*ngfor
看起来像这样的东西:请注意,它在
td
上,而不是标记中的tr
。那是因为每个主题都有自己的单元格,而不是自己的行 - 所有主题都在同一行中。这是一个stackblitz 展示这种方法。
First, I think you need to restructure your table a bit. Since the table of subjects is associated to a particular student, it should probably live inside the
td
that contains that student's data:Also note that I changed your
*ngFor="let item of data.info.students.name"
so it iterates overdata.info.students
rather thandata.info.students.name
since you need the student instance data (anddata.info.students.name
isn't a collection).And to display the student name, I replaced
{{ item[row] }}
with{{ student.name }}
.Then, to display the subjects for that student, you would need to create a method that accepts the student object (or just its
id
) and then parses the data and returns the student's associated subjects.That method is what you would need in the missing
*ngFor
that you're asking about.(Alternatively, of course, you could transform the data ahead of time into a mapping object that maps the students to their subjects.)
So the missing
*ngFor
that you're asking about would look something like this:Note that it's on the
td
, not thetr
like in your markup. That's because each subject has its own cell, not its own row—all subjects are in the same row.Here's a StackBlitz showing this approach.