我有4个组件。我正在使用KendoGrid在所有四个组件中显示数据。但是现在,我不想在所有四个组件中使用设置KendoGrid。为此,我创建了一个子组件,我在其中设置了KendoGrid并从父组件传递数据。我的孩子组成如下:
ChildComponent.ts:
@Component({
selector:"my-kendo-grid",
template:`
<kendo-grid [data]="dataVal">
<template ngFor [ngForOf]="myArr" let-column >
<kendo-grid-column field="{{column}}" title="{{column}}" >
<template kendoCellTemplate let-dataItem>
<div>{{dataItem}}</div>
</template>
</kendo-grid-column>
</template>
</kendo-grid>
export class ChildComponent implements OnInit {
@Input() dataVal:any; //taking dataVal value from parent component
myArr=[];
ngOnInit({
this._usersService.getUsers().subscribe((userResponse: any)=> {
for (var key in userResponse[0]) {
this.myArr.push(key);
}
return this.myArr; // binding 'myArr' in Kendogrid template which is making the kendogrid header
});
}
})
}
我的一个ParentComponent看起来像:
ParentComponent.html: 在这里,我在gridView中传递对象数组。
<my-kendo-grid [dataVal]="gridView"></my-kendo-grid>
现在项目的输出是:
标题正确,但在值的位置,我得到一个对象。
https://i.stack.imgur.com/L1RZt.png
请让我知道我在这里做错了什么。
答案 0 :(得分:1)
You are getting [object Object] in cell value because you are not going to access value, you have to code like:
<div>{{dataItem [column]}}</div>
And also you are not filtering any column from main datasource, so you don't required column array. You can load grid with all column.
Bind from parent component:
<gridView [height]="500" [dataSource]="dataSource"></gridView>
(1) With All Column:
@Input() height: number = 400;
@Input() dataSource: any = null;
ngOnChanges(changes: any) {
if (changes.dataSource != null && changes.dataSource.currentValue != null) {
this.SetDataSource();
}
}
SetDataSource() {
if (this.dataSource != null) {
}
}
HTML:
<kendo-grid [data]="dataSource"
[scrollable]="'scrollable'"
[height]="height"
[selectable]="true"
[sortable]="{ mode: 'multiple' }">
</kendo-grid>
(1) With Configured Column Array (as per your implementation):
@Input() height: number = 400;
@Input() dataSource: any = null;
public columns: any = [];
ngOnChanges(changes: any) {
if (changes.dataSource != null && changes.dataSource.currentValue != null) {
this.SetDataSource();
}
}
SetDataSource() {
if (this.dataSource != null) {
this.SetColumns();
}
}
SetColumns(): any {
this.columns = [];
if (this.dataSource != null) {
let row = this.dataSource[0];
this.columns = this.GetColumns(row);
}
}
protected GetColumns(obj: any): any {
let properties: any = [];
if (obj != null && typeof obj == "object") {
for (var property in obj) {
if (obj.hasOwnProperty(property)) {
if (property != '$type') {
let item: any = {};
item.Name = property;
item.DisplayFormat = null;
item.CanSort = true;
item.CanFilter = true;
item.DataType = 'String';
properties.push(item);
}
}
}
}
if (properties != null)
properties.sort();
return properties;
}
public setStyles(): any {
let styles = {
'height': (this.height - 45) + 'px'
};
return styles;
}
HTML:
<kendo-grid [ngStyle]="setStyles()"
[data]="dataSource"
[scrollable]="'scrollable'"
[height]="height"
[selectable]="true"
[sortable]="{ mode: 'multiple' }">
<kendo-grid-column *ngFor="let col of columns;let i=index" field="{{col.Name}}" title="{{col.Name}}"
[sortable]="col.CanSort"
[width]="100">
<ng-template kendoGridCellTemplate let-dataItem let-rowIndex="rowIndex">
<div class="gridCell">
{{dataItem[col.Name]}}
</div>
</ng-template>
</kendo-grid-column>
</kendo-grid>