Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PrimeNG Turbotable expand by default

Tags:

I have a PrimeNg turbotable with row expansion feature.

How can I expand the rows by default.

Here is my Code :

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' }         ];         }     } } 

I tried using expandedRowKeys attribute, but it is not working for me.

What am I missing here?

Thanks

like image 909
Mak Avatar asked Feb 22 '18 11:02

Mak


1 Answers

Update: For Version >7.x

Setting value to 1 won't work on version 7+ use boolean(true/false)

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

Working StackBlitz

For Version <7.x

I tried using expandedRowKeys attribute

Yes you're right. So add [expandedRowKeys]="expandedRows"> to p-table element :

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

and then, you just need to fill expandedRows object with vin values of the rows you want to expand (because dataKey is vin).

Because you want all rows to be expanded, you can fill it like that :

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

in order to have something like expandedRows = {"dsad231ff": 1, "gwregre345": 1, ...}

See working Plunker

like image 91
Antikhippe Avatar answered Sep 26 '22 01:09

Antikhippe