问题 PrimeNG Turbotable默认扩展


我有一个 PrimeNg可涡轮增压 具有行扩展功能。

如何默认展开行。

这是我的代码: 

HTML

<p-table [columns]="cols" [value]="cars" dataKey="vin">
<ng-template pTemplate="header" let-columns>
    <tr>
        <th style="width: 2.25em"></th>
        <th *ngFor="let col of columns">
            {{col.header}}
        </th>
    </tr>
</ng-template>
<ng-template pTemplate="body" let-rowData let-expanded="expanded" let-columns="columns">
    <tr>
        <td>
            <a href="#" [pRowToggler]="rowData">
                <i [ngClass]="expanded ? 'fa fa-fw fa-chevron-circle-down' : 'fa fa-fw fa-chevron-circle-right'"></i>
            </a>
        </td>
        <td *ngFor="let col of columns">
            {{rowData[col.field]}}
        </td>
    </tr>
</ng-template>
<ng-template pTemplate="rowexpansion" let-rowData let-columns="columns">
    <tr>
        <td [attr.colspan]="columns.length + 1">
            <div class="ui-g ui-fluid" style="font-size:16px;padding:20px">
                <div class="ui-g-12 ui-md-3" style="text-align:center">
                    <img [attr.alt]="rowData.brand" src="assets/showcase/images/demo/car/{{rowData.brand}}.png">
                </div>
                <div class="ui-g-12 ui-md-9">
                    <div class="ui-g">
                        <div class="ui-g-12">
                            <b>Vin:</b> {{rowData.vin}}
                        </div>
                        <div class="ui-g-12">
                            <b>Vin:</b> {{rowData.color}}
                        </div>
                        <div class="ui-g-12">
                            <b>Brand:</b> {{rowData.brand}}
                        </div>
                        <div class="ui-g-12">
                            <b>Color:</b> {{rowData.color}}
                        </div>
                    </div>
                </div>
            </div>
        </td>
    </tr>
  </ng-template>
</p-table>

TS 

export class TableRowExpansionDemo implements OnInit {

    cars: Car[];

    cols: any[];

    constructor(private carService: CarService) { }

    ngOnInit() {
        this.carService.getCarsSmall().then(cars => this.cars = cars);

        this.cols = [
            { field: 'vin', header: 'Vin' },
            { field: 'year', header: 'Year' },
            { field: 'brand', header: 'Brand' },
            { field: 'color', header: 'Color' }
        ];
        }
    }
}

我试过用 expandedRowKeys 属性,但它不适合我。

我在这里想念的是什么?

谢谢


3252
2018-02-22 11:10


起源



答案:


我尝试使用expandedRowKeys属性

你是对的。所以加 [expandedRowKeys]="expandedRows"> 至 p-table 元素:

<p-table [columns]="cols" [value]="cars" dataKey="vin" [expandedRowKeys]="expandedRows">

然后,你只需填写 expandedRows 对象 vin 要扩展的行的值(因为 dataKey 是 vin)。

因为您希望扩展所有行,所以可以像这样填充:

    const thisRef = this;
    this.cars.forEach(function(car) {
      thisRef.expandedRows[car.vin] = 1;
    });

为了有类似的东西 expandedRows = {"dsad231ff": 1, "gwregre345": 1, ...}

见工作 Plunker


10
2018-02-27 21:31



谢谢@Antikhippe。如果在expandedRows = {“dsad231ff”:1,“gwregre345”:1,...}中有什么用? - Mak
我想这意味着相应的行将在1处展开,如果没有则展开。 - Antikhippe
@Antikhippe为什么我们直接分配this.expandedRows [car.vin] = 1;不管用。 - Vignesh
你是什​​么意思“直接”? - Antikhippe
这是因为 这个 在使用时,在forEach循环中未定义 strict mode (stackoverflow.com/questions/19445599/...) - Antikhippe


答案:


我尝试使用expandedRowKeys属性

你是对的。所以加 [expandedRowKeys]="expandedRows"> 至 p-table 元素:

<p-table [columns]="cols" [value]="cars" dataKey="vin" [expandedRowKeys]="expandedRows">

然后,你只需填写 expandedRows 对象 vin 要扩展的行的值(因为 dataKey 是 vin)。

因为您希望扩展所有行,所以可以像这样填充:

    const thisRef = this;
    this.cars.forEach(function(car) {
      thisRef.expandedRows[car.vin] = 1;
    });

为了有类似的东西 expandedRows = {"dsad231ff": 1, "gwregre345": 1, ...}

见工作 Plunker


10
2018-02-27 21:31



谢谢@Antikhippe。如果在expandedRows = {“dsad231ff”:1,“gwregre345”:1,...}中有什么用? - Mak
我想这意味着相应的行将在1处展开,如果没有则展开。 - Antikhippe
@Antikhippe为什么我们直接分配this.expandedRows [car.vin] = 1;不管用。 - Vignesh
你是什​​么意思“直接”? - Antikhippe
这是因为 这个 在使用时,在forEach循环中未定义 strict mode (stackoverflow.com/questions/19445599/...) - Antikhippe