带* ngFor的ng-template在角度8中发布 [英] ng-template with *ngFor issue in angular 8

查看:88
本文介绍了带* ngFor的ng-template在角度8中发布的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用动态生成的列创建一个表. 我将 PrimeNg 库用于网格.

I want to create a table with dynamically generated columns. I used PrimeNg library for the grid.

我提出了很多问题,但是没有人回答我,请帮助我.

I opened many questions,but no one replied me,please help me to do this.

我与* ngFor一起使用来生成表列标题

I used with *ngFor to generate table column headers

有2个应答.一个用于行数据,一个用于列名称.

There are 2 arays. one for the row data and one for columns names.

这是我的行数据数组.其中包含一行

Here is my row data array.which contains one row

uersSurveyAnswers: any = [
    {
      userEmail: 'amara@gmail.com',
      qustns: [
        {
          qNo: 1,
          ansrs: ['1']
        },
        {
          qNo: 2,
          ansrs: ['1', '0', '1', '1']
        },
        {
          qNo: 5,
          ansrs: ['2']
        },
        {
          qNo: 6,
          ansrs: ['0', '1', '1', '0']
        }
      ]
    }];

数据列应以明智的方式加载

Columns to data should be load below wise

column Q1.1 - > uersSurveyAnswers -> qustns[0].ansrs[0]
column Q2.1 - > uersSurveyAnswers -> qustns[1].ansrs[0]
column Q2.2 - > uersSurveyAnswers -> qustns[1].ansrs[1]
column Q2.3 - > uersSurveyAnswers -> qustns[1].ansrs[2]
column Q2.4 - > uersSurveyAnswers -> qustns[1].ansrs[3]
column Q5.1 - > uersSurveyAnswers -> qustns[2].ansrs[0]
column Q6.1 - > uersSurveyAnswers -> qustns[3].ansrs[0]
column Q6.2 - > uersSurveyAnswers -> qustns[3].ansrs[1]
column Q6.3 - > uersSurveyAnswers -> qustns[3].ansrs[2]
column Q6.4 - > uersSurveyAnswers -> qustns[3].ansrs[3]

这是我的html代码

<p-table [columns]="columns" [value]="uersSurveyAnswers">
                            <ng-template pTemplate="header"  let-columns>
                                <tr>
                                    <th>
                                        Respondent Name
                                    </th>
                                    <th *ngFor="let col of columns">
                                        {{col.header}}
                                    </th>
                                </tr>
                            </ng-template>
                            <ng-template pTemplate="body" let-surveyAnswer let-columns="columns">
                                <tr>
                                    <td>{{surveyAnswer.userEmail}}</td>
                                     <td>{{surveyAnswer.qustns[0].ansrs[0]}}</td>
                                    <td *ngFor="let col of columns">
                                             {{col.field}}
                                        </td>
                                </tr>
                            </ng-template>
                        </p-table>

这是我的列数组

columns = [
{
field: 'qustns[0].ansrs[0]',
header: 'Q1.1'
},
{
field: 'qustns[1].ansrs[0]',
header: 'Q2.1'
},
{
field: 'qustns[1].ansrs[1]',
header: 'Q2.2'
},
{
field: 'qustns[1].ansrs[2]',
header: 'Q2.3'
}
,
{
field: 'qustns[1].ansrs[3]',
header: 'Q2.4'
}
,
{
field: 'qustns[2].ansrs[0]',
header: 'Q5.1'
}
,
{
field: 'qustns[3].ansrs[0]',
header: 'Q6.1'
}
,
{
field: 'qustns[3].ansrs[1]',
header: 'Q6.2'
}
,
{
field: 'qustns[3].ansrs[2]',
header: 'Q6.3'
}
,
{
field: 'qustns[3].ansrs[3]',
header: 'Q6.4'
}]

此数组是动态生成的.

现在我的问题是, 在我的第二个 ng-template 标签中包含 let-surveyAnswer ,其中包含行数据.

Now my problem is, inside my second 'ng-template' tag contains let-surveyAnswer which contains the row data.

如果我使用创建列

<td>{{surveyAnswer.qustns[0].ansrs[0]}}</td>

然后显示行数据

但我不明白如何在html中执行以下操作

but I can't understand how to do this in html like below

<td *ngFor="let col of columns">
  {{col.field}}
</td>

col.field 包含列数组数据,例如

col.field contains columns array data like

qustns[0].ansrs[0]

我想获取如下数据

<td *ngFor="let col of columns">
      {{surveyAnswer.col.field}}
    </td>

这是Stackblitz网址 https://stackblitz.com/edit/angular-ggvf31

Here is Stackblitz url https://stackblitz.com/edit/angular-ggvf31

请告诉我该怎么做.

推荐答案

问题出在您的ngOnInit()方法中.您正在尝试像这样设置字段值:

The problem is in your ngOnInit() method. You are trying to set the field values like this:

{
  field: 'qustns[0].ansrs[0]',
  header: 'Q1.1',
}

但这意味着实际上需要该值时,您要将field设置为字符串'qustns[0].ansrs[0]'.将它们更改为:

But that means you are setting field to the string 'qustns[0].ansrs[0]', when you actually want the value. Change them to be like:

{
  field: this.uersSurveyAnswers[0].qustns[0].ansrs[0],
  header: 'Q1.1',
}

这篇关于带* ngFor的ng-template在角度8中发布的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆