Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ag-grid react table not displaying?

New to react and ag-grid. I'm trying to implement a trivial example of ag-grid in my react project. When I run the project, the grid doesn't display. No console warnings, just the grid doesn't display.

import React from 'react';
import {AgGridReact} from 'ag-grid-react';
import '../../node_modules/ag-grid/dist/styles/ag-grid.css';
import '../../node_modules/ag-grid/dist/styles/theme-material.css';

var headers =  [{headerName: 'Name', field: 'name', width: 150, filter: 'text'},
                {headerName: 'Last', field: 'last', width: 150, filter: 'text'}]

var data = [{name: 'bob', last:'dude'},
            {name: 'will', last:'willdude'}]


export default class Accounts extends React.Component {
  constructor() {
    super();
    this.state = {
        showGrid: true,
        columnDefs: headers,
        rowData: data,
    };
  }
  onGridReady(params) {
      this.api = params.api;
      this.columnApi = params.columnApi;
  }

  render() {
    var gridTemplate;
    gridTemplate = (
        <div  className="ag-material">
            <AgGridReact
                columnDefs={this.state.columnDefs}
                rowData={this.state.rowData}
                onGridReady={this.onGridReady.bind(this)}
                showGrid={this.state.showGrid}
            />
        </div>
    );
    return (
      <div style={{width: '800px'}}>
        <p>I see this</p>
        {gridTemplate}
      </div>
    )
  }
}

This is what I see when I run this

like image 922
Brom Quinn Avatar asked May 13 '26 00:05

Brom Quinn


2 Answers

Okay got it, we need height on the parent div.

e.g.

    <div  className="ag-material" style={{height: '500px'}}>
        <AgGridReact
            columnDefs={this.state.columnDefs}
            rowData={this.state.rowData}
            onGridReady={this.onGridReady.bind(this)}
            showGrid={this.state.showGrid}
        />
    </div>
like image 55
Ryo Avatar answered May 14 '26 13:05

Ryo


its always better to give height and width otherwise ag-grid table will render by adjusting it to fit min content.

<div className="ag-material" style={{height: 500, width: 500}}>
        <AgGridReact
            columnDefs={this.state.columnDefs}
            rowData={this.state.rowData}
            onGridReady={this.onGridReady.bind(this)}
            showGrid={this.state.showGrid}
        />
</div>
like image 28
Ankita Srivastava Avatar answered May 14 '26 15:05

Ankita Srivastava



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!