PrimeNG Turbotable 默认展开
PrimeNG Turbotable expand by default
我有一个 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;
});
对于版本 <7.x
I tried using expandedRowKeys attribute
是的,你是对的。所以将 [expandedRowKeys]="expandedRows">
添加到 p-table
元素:
<p-table [columns]="cols" [value]="cars" dataKey="vin" [expandedRowKeys]="expandedRows">
然后,您只需要用要扩展的行的 vin
值填充 expandedRows
对象(因为 dataKey
是 vin
)。
因为你想展开所有的行,所以你可以这样填充:
const thisRef = this;
this.cars.forEach(function(car) {
thisRef.expandedRows[car.vin] = 1;
});
为了拥有类似的东西
expandedRows = {"dsad231ff": 1, "gwregre345": 1, ...}
查看工作 Plunker
在接受的答案中,expandedRows 映射到 turbo table expandedRowKeys 是一种方式,即它可以设置仅在加载时行的状态。
如果有人想在加载 table 后折叠或展开它,您可以通过传递 table 引用来直接编辑 'expandedRowKeys' 变量31=].
用参考变量定义你的table#dt
<p-table #dt [columns]="cols" [value]="cars" dataKey="vin">
像这样编辑您的 table 正文以获得点击功能触发器
<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">
<a >
<span (click)="onItemClick(rowData, dt)">
{{rowData[col.field]}}
</span>
</a>
</td>
</tr>
</ng-template>
在你的 TS 文件中添加函数
onItemClick(rowData:any, dt:any) {
if(dt.expandedRowKeys[rowData._id]){
dt.expandedRowKeys[rowData._id] = 0;
}
else {
dt.expandedRowKeys[rowData._id] = 1;
}
}
这种方法让您可以更自由地根据 table 外部的事件触发器更改 table 的状态,并一次展开或折叠多行。
在找到许多解决方案后,我尝试并找到了最简单的解决方法
<p-table [columns]="cols" [value]="cars" dataKey="vin" [expandedRowKeys]="expandedRows">
将数据键值替换为从服务或 http 调用获取的对象列表中的唯一 ID 键。
<p-table [columns]="cols" [value]="repaymentsList" dataKey="id" expandableRows="true" rowExpandMode="single" [responsive]="true">
我有一个 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;
});
对于版本 <7.x
I tried using expandedRowKeys attribute
是的,你是对的。所以将 [expandedRowKeys]="expandedRows">
添加到 p-table
元素:
<p-table [columns]="cols" [value]="cars" dataKey="vin" [expandedRowKeys]="expandedRows">
然后,您只需要用要扩展的行的 vin
值填充 expandedRows
对象(因为 dataKey
是 vin
)。
因为你想展开所有的行,所以你可以这样填充:
const thisRef = this;
this.cars.forEach(function(car) {
thisRef.expandedRows[car.vin] = 1;
});
为了拥有类似的东西
expandedRows = {"dsad231ff": 1, "gwregre345": 1, ...}
查看工作 Plunker
在接受的答案中,expandedRows 映射到 turbo table expandedRowKeys 是一种方式,即它可以设置仅在加载时行的状态。 如果有人想在加载 table 后折叠或展开它,您可以通过传递 table 引用来直接编辑 'expandedRowKeys' 变量31=].
用参考变量定义你的table#dt
<p-table #dt [columns]="cols" [value]="cars" dataKey="vin">
像这样编辑您的 table 正文以获得点击功能触发器
<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">
<a >
<span (click)="onItemClick(rowData, dt)">
{{rowData[col.field]}}
</span>
</a>
</td>
</tr>
</ng-template>
在你的 TS 文件中添加函数
onItemClick(rowData:any, dt:any) {
if(dt.expandedRowKeys[rowData._id]){
dt.expandedRowKeys[rowData._id] = 0;
}
else {
dt.expandedRowKeys[rowData._id] = 1;
}
}
这种方法让您可以更自由地根据 table 外部的事件触发器更改 table 的状态,并一次展开或折叠多行。
在找到许多解决方案后,我尝试并找到了最简单的解决方法
<p-table [columns]="cols" [value]="cars" dataKey="vin" [expandedRowKeys]="expandedRows">
将数据键值替换为从服务或 http 调用获取的对象列表中的唯一 ID 键。
<p-table [columns]="cols" [value]="repaymentsList" dataKey="id" expandableRows="true" rowExpandMode="single" [responsive]="true">