import React, {Component} from "react"; import {AgGridReact} from "ag-grid-react"; import SquareRenderer from "./SquareRenderer"; import CubeRenderer from "./CubeRenderer"; import ParamsRenderer from "./ParamsRenderer"; import CurrencyRenderer from "./CurrencyRenderer"; import ChildMessageRenderer from "./ChildMessageRenderer"; export default class DynamicComponentsExample extends Component { constructor(props) { super(props); this.state = { gridOptions: { context: { componentParent: this } }, rowData: DynamicComponentsExample.createRowData(), columnDefs: DynamicComponentsExample.createColumnDefs() }; } onGridReady = (params) => { this.gridApi = params.api; this.columnApi = params.columnApi; this.gridApi.sizeColumnsToFit(); }; methodFromParent(cell) { alert(`Parent Component Method from ${cell}!`); } refreshEvenRowsCurrencyData = () => { this.gridApi.forEachNode(rowNode => { if (rowNode.data.value % 2 === 0) { rowNode.setDataValue('currency', rowNode.data.value + Number(Math.random().toFixed(2))) } }); this.gridApi.refreshCells({ columns: ['currency'] }) }; static createColumnDefs() { return [ {headerName: "Row", field: "row", width: 100}, { headerName: "Square", field: "value", cellRendererFramework: SquareRenderer, editable: true, colId: "square", width: 100 }, { headerName: "Cube", field: "value", cellRendererFramework: CubeRenderer, colId: "cube", width: 100 }, { headerName: "Row Params", field: "row", cellRendererFramework: ParamsRenderer, colId: "params", width: 215 }, { headerName: "Currency", field: "currency", cellRendererFramework: CurrencyRenderer, colId: "currency", width: 135 }, { headerName: "Child/Parent", field: "value", cellRendererFramework: ChildMessageRenderer, colId: "params", width: 120 } ]; } static createRowData() { let rowData = []; for (let i = 0; i < 15; i++) { rowData.push({ row: "Row " + i, value: i, currency: 1 + Number(Math.random()).toFixed(2) }); } return rowData; } render() { return (

Dynamic React Component Example

Dynamic React Component Example

This example demonstrates Dynamic React Components with ag-Grid, Parent/Child Communication (cell component to parent grid component), as well as dynamic data updates of the Currency column.

Square, Cube, Row Params, Currency and Child/Parent: React Components within the Grid

Currency (Pipe): An React Component mimicking a Currency Pipe, dynamically updated with the button above.

Child/Parent: Demonstrates the Child Cell Component communicating with the Parent Grid Component.

Refresh Even Row Currency Data: Dynamically Updates Event Rows Currency Value. Only the Currency column will be re-rendered.

React Functionality

Utilise React Components within ag-Grid

React with ag-Grid React Renderers Updating Data
); } };