gpt4 book ai didi

html - PrimeNG Turbotable 默认展开

转载 作者:技术小花猫 更新时间:2023-10-29 12:14:38 34 4
gpt4 key购买 nike

我有一个 PrimeNg turbotable具有行扩展功能。

如何默认展开行。

这是我的代码:

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 属性,但它对我不起作用。

我在这里错过了什么?

谢谢

最佳答案

更新:版本 >7.x

将值设置为 1 不适用于版本 7+ 使用 boolean(true/false)

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

工作 StackBlitz

版本<7.x

I tried using expandedRowKeys attribute

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

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

然后,您只需将要扩展的行的 vin 值填充到 expandedRows 对象中(因为 dataKeyvin).

因为你想展开所有的行,所以你可以这样填充:

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

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

查看工作 Plunker

关于html - PrimeNG Turbotable 默认展开,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48925945/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com