We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

How to get Grid layout with dynamic columns and data

Here is my Data Service
import { HttpClient, HttpParams, HttpHeaders } from "@angular/common/http";
import { Injectable } from "@angular/core";
import { map } from "rxjs/operators";
import { Request } from "@angular/http";

@Injectable()
export class DataService {

  constructor(private http: HttpClient) {

  }

  public compData: Object[];
  public compColumns: CompData[];
  loadCompColumns() {
    return this.http.get("/api/CompDetails/GetColumns")
      .pipe(
      map((data: any[]) => {
        //console.log(data);
        this.compColumns = data;
        return true;
      })
      );
  }
  loadCompData() {
    return this.http.get("/api/CompDetails/GetData")
      .pipe(
        map((data: any[]) => {
          this.compData = data;
          return true;
        })
      );
  }
}
export interface CompData {
  field: string,
  headerText: string,
  minWidth: number,
  maxWidth: number,
  width: number,
  allowEditing: Boolean,
  allowFiltering: Boolean,
  allowReordering: Boolean,
  allowResizing: Boolean,
  allowSorting: Boolean,
  isFrozen: Boolean,
  visible: Boolean,
  format: string,
  columns: CompData
}

Here is my home.component.html
<ejs-grid #gridComp height='500px' [editSettings]="editSettings" [toolbar]="toolbar" showColumnChooser='true' allowResizing= 'false' allowTextWrap='true' [gridLines]='lines' (created)="gridCreated($event)" >
  
</ejs-grid>

Here is my home.component.ts file 
import { Component, OnInit, ViewChild, ViewEncapsulation, Input, Injectable, Inject, ElementRef } from '@angular/core';
import { DataManager, Query, UrlAdaptor, WebApiAdaptor, ODataAdaptor } from '@syncfusion/ej2-data';
import { GridComponent, EditService, ToolbarService, PageService, ColumnChooserService, EditSettingsModel, ToolbarItems, GridLine, Column, valueAccessor, ValueAccessor } from '@syncfusion/ej2-ng-grids';
import { EventArgs } from '@syncfusion/ej2-navigations';
import {DataService, AttorneyCompData} from '../shared/dataService'
@Component({
  selector: 'app-home',
  styleUrls: ['./home.component.css'],
  templateUrl: './home.component.html',
  encapsulation: ViewEncapsulation.None,
  providers: [ToolbarService, EditService, PageService, ColumnChooserService]

})


export class HomeComponent implements OnInit {
  public title: string = 'Attorney Comp';
  public lines: GridLine = <GridLine>'Both';
  public editSettings: EditSettingsModel;
  public toolbar: ToolbarItems[];
constructor(private data: DataService) {

  }
public columns: Object[];
  @ViewChild('gridAttorneyComp')
  public gridAttorneyComp: GridComponent;
ngOnInit() {
this.data.loadCompData().subscribe(successForData => {
          if (successForData) {

            this.gridComp.dataSource = this.data.compData;
            this.gridComp.allowPaging = false;
            this.gridComp.pageSettings = { pageSize: this.data.compData.length };

            this.data.loadCompColumns().subscribe(success => {
              if (success) {
                
                this.columns = this.data.compColumns;
                this.gridComp.columns = this.columns;

              }
            });
            
          }
        });
}
public gridCreated(test: EventArgs) {

    
  }
}

This keeps on generating error - 


if I hardcopy the columns that are returned from the web service in this.columns and assign it to the grid columns it works perfectly. But as soon as I go back to the web service to pull that data it errors out. I cannot use DataManager since I am doing some manipulation on columns on the grid and have to update one column at a time.

any help is greatly appreciated.


3 Replies

MF Mohammed Farook J Syncfusion Team November 30, 2018 07:41 AM UTC

Hi Ameet, 
 
Thanks for contacting Syncfusion support. 
 
 
From schema definition the Grid can be rendered based on column or dataSource. So you must be define columns or dataSource for grid render. For your requirement we have created a sample “Grid with dynamic column render”. Please find the code example and sample for your reference. 
 
 
[app.component.html] 
 
    <ejs-grid [dataSource]='data' allowPaging='true' height=365 [pageSettings]='initialPage' [columns] = 'columns' > 
        
    </ejs-grid> 
 
 
[app.component.ts] 
 
. . . 
 
export class PageComponent implements OnInit { 
  public data: Object[]; 
  public initialPage: Object; 
  public columns: Object[]; 
 
  ngOnInit(): void { 
    this.data = data; 
    this.initialPage = { pageCount: 4 }; 
    this.columns = [ 
      { field: 'OrderID', headerText: 'Order ID', width: 120, textAlign: 'Right' }, 
      { field: 'CustomerName', headerText: 'Customer Name', width: 150 }, 
      { field: 'OrderDate', headerText: 'Order Date', width: 130, format: 'yMd', textAlign: 'Right' }, 
      { field: 'Freight', width: 120, format: 'C2', textAlign: 'Right' }, 
 
      { field: 'ShipCountry', headerText: 'Ship Country', width: 150 } 
    ] 
  } 
} 
 
 
Here we can define the dataSource and define the column in dynamic. 
 
 
Please get back to us, if you need any further assistance. 
 
Regards, 
J Mohammed Farook 
 
 



CS Charanjit Singh December 10, 2020 06:04 AM UTC

Dear Team,

I am able to create dynamic grid column but after grid is not scrolling please help what i can do for this one.

Thanks
Arshdeep


SK Sujith Kumar Rajkumar Syncfusion Team December 11, 2020 11:41 AM UTC

Hi Arshdeep, 
 
Greetings from Syncfusion support. 
 
We checked your reported problem but unfortunately were unable to reproduce it from our end as the scrolling was working fine with dynamically generated columns. You can check the below sample for reference, 
 
 
Please share us the following information to validate further on this, 
 
  • Video demonstration of the problem to understand it clearly.
  • Are any console errors thrown?
  • Syncfusion package version used.
  • Grid code file.
  • If possible share us a simple sample to replicate the problem or try reproducing it in the above provided sample.
 
Let us know if you have any concerns. 
 
Regards, 
Sujith R 


Loader.
Up arrow icon