简体   繁体   中英

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

这是我运行时看到的

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

eg

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

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>

I was seeing a similar issue when the grid was not rendering. Adding the grid styles fixed the problem for me.

import '../../node_modules/ag-grid/dist/styles/ag-grid.css';
import '../../node_modules/ag-grid/dist/styles/theme-material.css';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM