0

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.

I used with *ngFor to generate table column headers

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: '[email protected]',
      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]

Here is my html code

<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>

Here is my columns array

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'
}]

this array is generated by dynamically.

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

If I create column with

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

Then it's showing the row data

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

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

col.field contains columns array data like

qustns[0].ansrs[0]

I want to get the data like below

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

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

Please tell me how to do this.

1 Answer 1

0

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',
}

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',
}
Sign up to request clarification or add additional context in comments.

3 Comments

Collierre, thanks alot for your reply, I updated my stackblits app. Can you please check it. But it's not showing data. I can't do this,if it's working,then it's only loading first row data.Foor this question I only added one index array,but that array contains many indexes.so how I do this method for all the rows
Have a look in the console on stackblitz. It says Error: this.uersSurveyAnswers[0].ustns is undefined. You need to change ustns to qustns in the second last object of this.columns.
If you want to make it work with many rows, are all the user answers the same? I.e. does question 1 always have one answer, question 2 always has 4 answers etc.?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.