Angular 2 : How to make my own custom KendoGrid in angular 2

被刻印的时光 ゝ 提交于 2019-12-11 06:08:52

问题


I have 4 Components. And I am using KendoGrid for displaying the data in all four components. But now, I dont want to use to setup KendoGrid in all four components. For this, I made a child component in which i am setting up the KendoGrid and passing the data from parent component. My child component is given below:

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

And one of my ParentComponent looks Like:

ParentComponent.html : In This, I am passing the array of objects in gridView.

<my-kendo-grid [dataVal]="gridView"></my-kendo-grid>

Now project's output is :

The headers are coming properly, but in the place of values, i am getting an object.

https://i.stack.imgur.com/L1RZt.png

Please let me know what wrong I am doing here.


回答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>


来源:https://stackoverflow.com/questions/43027248/angular-2-how-to-make-my-own-custom-kendogrid-in-angular-2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!