Search results

Auto Generated Columns in React Grid component

02 Dec 2022 / 3 minutes to read

The columns are automatically generated when columns declaration is empty or undefined while initializing the grid. All the columns in the dataSource are bound as grid columns.

Source
Preview
App.jsx
App.tsx
Copied to clipboard
import { GridComponent } from '@syncfusion/ej2-react-grids';
import * as React from 'react';
const data = [
    { OrderID: 10248, CustomerID: 'VINET', EmployeeID: 5 },
    { OrderID: 10249, CustomerID: 'TOMSP', EmployeeID: 6 },
    { OrderID: 10250, CustomerID: 'HANAR', EmployeeID: 4 }
];
export default class App extends React.Component {
    render() {
        return <GridComponent dataSource={data}/>;
    }
}
Copied to clipboard
import { GridComponent } from '@syncfusion/ej2-react-grids';
import * as React from 'react';

const data: object[] = [
  { OrderID: 10248, CustomerID: 'VINET', EmployeeID: 5 },
  { OrderID: 10249, CustomerID: 'TOMSP', EmployeeID: 6 },
  { OrderID: 10250, CustomerID: 'HANAR', EmployeeID: 4 }];

export default class App extends React.Component<{}, {}>{
  public render() {
      return <GridComponent dataSource={data}/>
  }
}

Set Primary key column for auto generated columns when editing is enabled

Primary key can be defined in the declaration of column object of the grid. When we didn’t declare the columns, the grid will generate the columns automatically. For these auto generated columns, you can set isPrimaryKey column property as true by using the following ways,

If Primary key “column index” is known then refer the following code example

Source
Preview
App.jsx
App.tsx
Copied to clipboard
import { Edit, GridComponent, Inject } from '@syncfusion/ej2-react-grids';
import * as React from 'react';
const data = [
    { OrderID: 10248, CustomerID: 'VINET', EmployeeID: 5 },
    { OrderID: 10249, CustomerID: 'TOMSP', EmployeeID: 6 },
    { OrderID: 10250, CustomerID: 'HANAR', EmployeeID: 4 }
];
export default class App extends React.Component {
    constructor() {
        super(...arguments);
        this.editOptions = {
            allowAdding: true, allowDeleting: true, allowEditing: true
        };
    }
    dataBound() {
        if (this.grid) {
            const column = this.grid.columns[0];
            column.isPrimaryKey = true;
        }
    }
    render() {
        this.dataBound = this.dataBound.bind(this);
        return <GridComponent dataSource={data} dataBound={this.dataBound} editSettings={this.editOptions} ref={g => this.grid = g}>
              <Inject services={[Edit]}/>
             </GridComponent>;
    }
}
Copied to clipboard
import { ColumnModel, Edit, EditSettingsModel, Grid, GridComponent, Inject } from '@syncfusion/ej2-react-grids';
import * as React from 'react';

const data: object[] = [
  { OrderID: 10248, CustomerID: 'VINET', EmployeeID: 5 },
  { OrderID: 10249, CustomerID: 'TOMSP', EmployeeID: 6 },
  { OrderID: 10250, CustomerID: 'HANAR', EmployeeID: 4 }];

export default class App extends React.Component<{}, {}>{
  public grid: Grid | null;
  public editOptions: EditSettingsModel = {
    allowAdding: true, allowDeleting: true, allowEditing: true
  };
  public dataBound(){
    if (this.grid) {
      const column: ColumnModel = this.grid.columns[0] as ColumnModel;
      column.isPrimaryKey = true;
    }
  }
  public render() {
      this.dataBound = this.dataBound.bind(this);
      return <GridComponent dataSource={data} dataBound= { this.dataBound }
                editSettings={this.editOptions} ref={g => this.grid = g}>
              <Inject services={[Edit]} />
             </GridComponent>
  }
}

If Primary key column and its field is known then primary key for the respective column can be defined as follows.

Copied to clipboard
  const column: ColumnModel = this.grid.getColumnByField('OrderID');
  column.isPrimaryKey = true;

Set column options to auto generated columns

You can set column options such as format, width to the auto generated columns by using dataBound event of the grid.

In the below example, width is set for OrderID column, date type is set for OrderDate column and numeric type is set for Freight column.

Source
Preview
App.jsx
App.tsx
Copied to clipboard
import { GridComponent } from '@syncfusion/ej2-react-grids';
import * as React from 'react';
const data = [
    { OrderID: 10248, CustomerID: 'VINET', Freight: 32.3800, OrderDate: "1996-07-02T00:00:00.000Z" },
    { OrderID: 10249, CustomerID: 'TOMSP', Freight: 32.3800, OrderDate: "1996-07-19T00:00:00.000Z" },
    { OrderID: 10250, CustomerID: 'HANAR', Freight: 32.3800, OrderDate: "1996-07-22T00:00:00.000Z" }
];
export default class App extends React.Component {
    constructor() {
        super(...arguments);
        this.isInitial = false;
    }
    load() {
        this.isInitial = true;
    }
    dataBound() {
        if (this.grid && this.isInitial) {
            this.isInitial = false;
            const columns = this.grid.columns;
            columns[0].width = 120;
            for (const col of columns) {
                if (col.field === "OrderDate") {
                    col.type = "date";
                }
                if (col.type === "date") {
                    col.format = { type: "date", format: "dd/MM/yyyy" };
                }
                if (col.field === 'Freight') {
                    col.format = "P2";
                }
            }
            this.grid.refreshColumns();
        }
    }
    render() {
        this.dataBound = this.dataBound.bind(this);
        this.load = this.load.bind(this);
        return <GridComponent dataSource={data} dataBound={this.dataBound} load={this.load} ref={g => this.grid = g}/>;
    }
}
Copied to clipboard
import { ColumnModel, Grid, GridComponent } from '@syncfusion/ej2-react-grids';
import * as React from 'react';


const data: object = [
  { OrderID: 10248, CustomerID: 'VINET', Freight: 32.3800, OrderDate: "1996-07-02T00:00:00.000Z" },
  { OrderID: 10249, CustomerID: 'TOMSP', Freight: 32.3800, OrderDate: "1996-07-19T00:00:00.000Z" },
  { OrderID: 10250, CustomerID: 'HANAR', Freight: 32.3800, OrderDate: "1996-07-22T00:00:00.000Z" }];

export default class App extends React.Component<{}, {}>{
  isInitial = false;
  public grid: Grid | null;
  public load() {
    this.isInitial=true;
  }
  public dataBound() {
    if (this.grid && this.isInitial) {
      this.isInitial=false;
      const columns: ColumnModel[] = this.grid.columns as ColumnModel[];
      columns[0].width = 120;
      for (const col of columns) {
        if(col.field === "OrderDate"){
            col.type="date";
        }
        if (col.type === "date") {
            col.format = { type: "date", format: "dd/MM/yyyy" };
        }
        if (col.field === 'Freight') {
          col.format = "P2";
        }
      }
      this.grid.refreshColumns();
    }
  }
  public render() {
    this.dataBound = this.dataBound.bind(this);
    this.load = this.load.bind(this);
    return <GridComponent dataSource={data} dataBound= { this.dataBound} load= { this.load}
            ref={g => this.grid = g}/>
  }
}