在Kendo for Angular网格中以编程方式生成模板

时间:2018-08-17 15:10:42

标签: kendo-ui kendo-grid kendo-ui-angular2

在父组件中给出此columns数组:

columns = [
      { field: 'field1', title: 'Title 1', width: '100px' },
      { field: 'field2', title: 'Title 2', width: '200px' },
      { field: 'field3', title: 'Title 3' }
  ];

我可以在my-table组件中动态为Angular网格构建Kendo:

@Component({
  selector: 'my-table',
  template: `
          <kendo-grid #grid="kendoGrid" [data]="data">
              <kendo-grid-column
                  *ngFor="let column of columns"
                     field="{{column.field}}"
                     title="{{column.title}}"
                     width="{{column.width}}"
              </kendo-grid-column>
          </kendo-grid>
`
})
export class MyTableComponent {
     @Input() data: any[] = [];
     @Input() columns: any[] = [];
}

我需要以编程方式向表中添加包含按钮的列,该按钮应在父组件中执行功能。

这是应由MyTableComponent呈现的标记的示例:

 <kendo-grid-column>
      <ng-template kendoGridCellTemplate let-dataItem let-rowIndex="rowIndex">
           <button kendoButton (click)="edit(dataItem,rowIndex)" [icon]="'edit'"></button>
       </ng-template>
 </kendo-grid-column>

MyTableComponent应该从其父元素接收columns数组中的信息,如下所示:

columns: [ { isButton: true, buttonLabel: 'Edit', callbackFunc: parentFunc } ];

可以在表组件中以编程方式生成模板吗?

1 个答案:

答案 0 :(得分:2)

这种情况似乎是可能的。您应该在该列内添加一个单元格模板,并使用ngIf仅将其呈现给按钮列:

<ng-template *ngIf="column.isButton" kendoGridCellTemplate let-dataItem let-rowIndex="rowIndex">
    <button kendoButton (click)="column.callbackFunc(dataItem,rowIndex)" [icon]="column.icon">{{ column.buttonLabel }}</button>
</ng-template>

https://stackblitz.com/edit/angular-bzuy99?file=app/app.component.ts