Angular:使用 *ngfor将数据链接到动态表

发布于 2025-02-04 13:05:06 字数 1459 浏览 4 评论 0原文

我正在创建一个动态表。下面我的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:
enter image description here

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鲸落 2025-02-11 13:05:06

首先,我认为您需要对桌子进行重组。由于主题表与特定学生相关联,因此它可能应该生活在包含该学生数据的td中:

<table>
  <tr *ngFor="let row of rowHeaders">
    <td *ngFor="let student of data.info.students"> <--- each td contains data for a student
      <span *ngIf="row !== 'Subjects'">{{ student.name }}</span>

      <table *ngIf="row === 'Subjects'"> <--- move this table inside the td
        <tr>
          <td *ngFor="let subject of getSubjectsForStudent(student)"> <-- the *ngFor you were missing
            {{ subject.name }}
          </td>
        </tr>
      </table>
    </td>
  </tr>
</table>

另请注意,我更改了您的*ngfor =“让数据项目。 info.students.name“,因此它可以通过data.info.students而不是data.info.students.name迭代,因为您需要学生实例数据(和data.info.students.name不是集合)。

为了显示学生名称,我用{{item [row]}}{{student.name}}替换。

然后,要显示该学生的主题,您将需要创建一种接受学生对象(或仅仅是其id)的方法,然后解析数据并返回学生的相关主题。

该方法是您要问的缺少*ngfor中所需的方法。

(或者,当然,您可以提前将数据转换为映射学生将其映射到学科的映射对象。)

因此,您要问的缺少*ngfor看起来像这样的东西:

<td *ngFor="let subject of getSubjectsForStudent(student)">

请注意,它在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:

<table>
  <tr *ngFor="let row of rowHeaders">
    <td *ngFor="let student of data.info.students"> <--- each td contains data for a student
      <span *ngIf="row !== 'Subjects'">{{ student.name }}</span>

      <table *ngIf="row === 'Subjects'"> <--- move this table inside the td
        <tr>
          <td *ngFor="let subject of getSubjectsForStudent(student)"> <-- the *ngFor you were missing
            {{ subject.name }}
          </td>
        </tr>
      </table>
    </td>
  </tr>
</table>

Also note that I changed your *ngFor="let item of data.info.students.name" so it iterates over data.info.students rather than data.info.students.name since you need the student instance data (and data.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:

<td *ngFor="let subject of getSubjectsForStudent(student)">

Note that it's on the td, not the tr 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文