【问题标题】:Primeng table: add rows to table in ngOnInitPrimeng 表:在 ngOnInit 中向表中添加行
【发布时间】:2020-03-10 14:55:14
【问题描述】:

我正在使用以下代码,取自 Primeng:

Table

它只显示表格的标题。

我想在 ts 代码中添加几行带有静态值的行。

能否提供示例代码?

谢谢你, 兹维卡

【问题讨论】:

    标签: primeng


    【解决方案1】:

    以下是向表中添加静态值的方法。

    export class TableBasicDemo implements OnInit {
    
        cars: Car[];
    
        constructor(private carService: CarService) { }
    
        ngOnInit() {
            this.carService.getCarsSmall().then(cars => this.cars = cars);
        }
    }
    <h3 class="first">Basic</h3>
    <p-table [value]="cars">
        <ng-template pTemplate="header">
            <tr>
                <th>Vin</th>
                <th>Year</th>
                <th>Brand</th>
                <th>Color</th>
            </tr>
        </ng-template>
        <ng-template pTemplate="body" let-car>
            <tr>
                <td>{{car.vin}}</td>
                <td>{{car.year}}</td>
                <td>{{car.brand}}</td>
                <td>{{car.color}}</td>
            </tr>
        </ng-template>
    </p-table>
    
    

    【讨论】:

      【解决方案2】:

      这就是我的意思:

          cars: Car[];
      
          this.cars = [
                { vin: 0, year:0, brand:0, color:0 },
                { vin: 0, year:0, brand:0, color:0 },
      ...
                { vin: 0, year:0, brand:0, color:0 },
      
            ];
      
      export interface Car{
        vin;
        brand;
        color;
      }

      【讨论】:

        猜你喜欢
        • 2012-11-01
        • 2017-04-07
        • 2016-03-01
        • 1970-01-01
        • 2010-10-22
        • 1970-01-01
        • 2016-08-27
        • 2013-02-11
        • 2021-04-23
        相关资源
        最近更新 更多