简体   繁体   English

如何访问 Angular 中对象数组中的数组?

[英]How to access array inside of an array of objects in Angular?

I have an array of objects containing employees, that looks like this:我有一个包含员工的对象数组,如下所示:

[    
   {
      id: "1",
      name: "name",
      email: "email",
      languages: ['english', 'german', 'spanish']
    },
    {
      id: "2",
      name: "name",
      email: "email",
      languages: ['english', 'spanish']
    },
]

This array is the datasource for my grid.这个数组是我网格的数据源。 In the same grid's 'languages' column, I am trying to list these languages as chips elements.在同一个网格的“语言”列中,我试图将这些语言列为芯片元素。 To create these elements I tried to use ngFor, as it follows (I am using syncfusion):为了创建这些元素,我尝试使用 ngFor,如下所示(我使用的是 syncfusion):

<e-column field="languages" headerText="Languanges" type="array">
  <ng-template #template let-data>
    <div *ngFor="let employee of employeeList">
      <ejs-chiplist id="chip" *ngFor="let lang of employee.languages">
        <e-chips>
          <e-chip text="{{lang}}" cssClass="e-primary"></e-chip>
        </e-chips>
      </ejs-chiplist>
    </div>
  </ng-template>
</e-column>

This kinda works, however, because of the 2 ngFors, it displays the languages multiple times.然而,这有点工作,因为有 2 个 ngFors,它会多次显示语言。 What can I do about this?我能做些什么呢? What would be a better way to solve this?有什么更好的方法来解决这个问题?

You can remove the one ngFor for all the employees as you already have access to the data in the template from the grid's data source and can access the employees languages like below您可以为所有员工删除一个 ngFor,因为您已经可以从网格的数据源访问模板中的数据,并且可以访问员工语言,如下所示

<e-column field="languages" headerText="Languanges" type="array">
  <ng-template #template let-data>

      <ejs-chiplist id="chip" *ngFor="let lang of data.languages">
        <e-chips>
          <e-chip text="{{lang}}" cssClass="e-primary"></e-chip>
        </e-chips>
      </ejs-chiplist>
  </ng-template>
</e-column>

you can also have a look at the Syncfusion documentation on column templates: https://help.syncfusion.com/angular/grid/columns#column-template您还可以查看有关列模板的 Syncfusion 文档: https://help.syncfusion.com/angular/grid/columns#column-template

Greetings from the Syncfusion support,来自 Syncfusion 支持的问候,

You can bind the chip list component in the Grid column using the column template feature of the Grid.您可以使用 Grid 的列模板功能在 Grid 列中绑定芯片列表组件。 In the below code example, we have used ngFor in an ng-template directive and it is a looping basis to bind text values in the chip list.在下面的代码示例中,我们在 ng-template 指令中使用了 ngFor,它是在芯片列表中绑定文本值的循环基础。 Please refer to the below code example and sample for more information.请参阅下面的代码示例和示例以获取更多信息。

 [app.component.html] <ejs-grid #grid [dataSource]='data' allowPaging='true' [pageSettings]='pageSettings'> <e-columns> <e-column field='id' headerText='Order ID' width='120' textAlign='Right' isPrimaryKey='true'></e-column> <e-column field='name' headerText='Name' width='120' textAlign='Right'></e-column> <e-column field='email' headerText='Email' width='150'></e-column> <e-column headerText= 'Languages' width='120'> <ng-template #template ngFor let-column [ngForOf]="data"> <ejs-chiplist id="chip-default"> <e-chips> <ng-template ngFor let-lang [ngForOf]="column.languages"> <e-chip [text]="lang" cssClass="e-primary"></e-chip> </ng-template> </e-chips> </ejs-chiplist> </ng-template> </e-column> </e-columns> </ejs-grid>

Sample link here示例链接在这里

Please get back to us, if you need further assistance.如果您需要进一步的帮助,请回复我们。

Regards,问候,

Balaji Sekar巴拉吉·塞卡

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM