PrimeNG Turbotable 默认展开

新手上路,请多包涵

我有一个具有行扩展功能的 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 属性,但它对我不起作用。

我在这里错过了什么?

谢谢

原文由 Mak 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 742
2 个回答

更新:版本 >7.x

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

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

工作 堆栈闪电战

对于版本 <7.x

我尝试使用 expandedRowKeys 属性

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

 <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 ) .

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

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

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

查看工作 Plunker

原文由 Antikhippe 发布,翻译遵循 CC BY-SA 4.0 许可协议

在接受的答案中,映射到涡轮表 expandedRowKeysexpandedRows 是一种方式,即它可以仅在加载时设置行的状态。如果有人想在加载表格后折叠或展开它,您可以通过从 html 传递表格引用来直接编辑“ expandedRowKeys ”变量。

使用参考变量 #dt 定义您的表

<p-table #dt [columns]="cols" [value]="cars" dataKey="vin">

像这样编辑您的表体以获得点击功能触发器

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

这种方法使您可以更自由地根据表外部的事件触发器更改表的状态,并一次展开或折叠多行。

原文由 himanshu goyal 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题