Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
884 views
in Technique[技术] by (71.8m points)

angular - Primeng the table component isn't working with ngFor

i am trying to loop an array each object in this array is a table on html. and it display like this :

<p-table [value]="section" *ngFor="let section of sections">
  <ng-template pTemplate="header">
          <tr>
              <th>Quantity</th>
              <th>Length</th>
              <th>m^2</th>
              <th></th>
          </tr>
          <tr>
              <th colspan="4">
                  <div (click)="showDialog()" class="text-left">+ A - Flat Panel RAW MDF Red Gloss
                      - $95 / sqm
                  </div>
              </th>
              <th colspan="8">
                  <div class="md-inputfield">
                      <input type="text" class="form-control" pInputText>
                  </div>
              </th>
          </tr>
      </ng-template>
      <ng-template pTemplate="body" let-rowData *ngFor="let piece of rowData.Pieces">
          <tr>
              <td>
                  <p-spinner [(ngModel)]="rowData.Quantity"></p-spinner>
              </td>
              <td pEditableColumn>
                  <p-cellEditor>
                      <ng-template pTemplate="input">
                          <input type="text" [(ngModel)]="rowData.Length">
                      </ng-template>
                      <ng-template pTemplate="output">
                          {{rowData.Length}}
                      </ng-template>
                  </p-cellEditor>
              </td>
              <td>
                  {{CalculateTotalArea(rowData)}}
              </td>
              <td>
                  <button pButton type="button" icon="fa-close"></button>
              </td>
          </tr>
      </ng-template>
</p-table>

but this give me an error this.value.sort is not a function and here is array

this.pieces = [{ Quantity: 2, Length: 3, Width: 3, Thickness: 4 }]

this.sections = [ { Pieces: this.pieces, text: "abc" } ]

i am trying to push code into Plunker but i don't know how to upgrade version of primeng to "primeng": "^5.2.0-rc.1", then the Plunker is't working right now anyone help me update the primeng library and suggest me how to resolve this bug . here is the link Plunker : Plunker

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It is because, p-table expects an array through value however you are passing { Pieces: this.pieces, text: "abc" } which causes the error. Either you have to remove *ngFor="let section of sections" and pass your array like this <p-table [value]="sections"> or if you really want to have bunch of tables in your page, your sections array should be like following (multidimensional):

// for the sake of simplicity, I named your objects as 'item'
this.sections = [
    [item1, item2, item3],
    [item10, item11, item12],
    [item15, item50, item32]
]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...