ag-grid with react not working

2.8k views Asked by At

I have a problem getting ag-Grid to work with my react app. Whatever I try, I only get some text, I don't see the grid. I don't know what I'm missing. Here is my code:

/*
 * ----------------------------
 * index.js
 * ----------------------------
 */

import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import registerServiceWorker from './registerServiceWorker';
import { BrowserRouter } from 'react-router-dom'

ReactDOM.render(
    <BrowserRouter>
        <App />
    </BrowserRouter>
    , document.getElementById('root'));
registerServiceWorker();

/*
 * ----------------------------
 * App.js
 * ----------------------------
 */

import React, { Component } from 'react';
import Header from './Header';
import Main from './Main';
import './Styles/App.css';

class App extends Component {
  render() {
    return (
      <div className="App">
        <Header />
        <Main />
      </div>
    );
  }
}

export default App;

/*
 * ----------------------------
 * Header.js
 * ----------------------------
 */

import React, { Component } from 'react';
import { Link } from 'react-router-dom'
import './Styles/Header.css';

class Header extends Component {
  render() {
    return (
        <div className="App-header">
          <h2>MY APPLICATION</h2>
          <Link to='/'>Home</Link>
          <Link to='/cities'>Cities</Link>
          <Link to='/countries'>Countries</Link>
        </div>
    );
  }
}

export default Header;

/*
 * ----------------------------
 * Main.js
 * ----------------------------
 */

import React, { Component } from 'react';
import { Switch, Route } from 'react-router-dom';
import Home from './Pages/Home';
import Stations from './Pages/Cities';
import Countries from './Pages/Countries';

class Main extends Component {
  render() {
    return (
        <div className="App-main">
          <Switch>
            <Route exact path='/' component={Home}/>
            <Route path='/cities' component={Cities}/>
            <Route path='/countries' component={Countries}/>
          </Switch>
        </div>
    );
  }
}

export default Main;

/*
 * ----------------------------
 * Countries.js
 * ----------------------------
 */

import React, { Component } from 'react';
import { AgGridReact } from 'ag-grid-react';

class Countries extends Component {
  constructor(props) {
    super(props);
    this.state = {
      columnDefs: this.createColumnDefs(),
      rowData: this.createRowData()
    };
  }

  createColumnDefs() {
    return [
      {headerName: "#", width: "30px", checkboxSelection: true, suppressSorting: true, suppressMenu: true, pinned: true },
      {headerName: "Name", field: "name"},
      {headerName: "Abbreviation", field: "abbr"},
      {headerName: "id", field: "id"}
    ];
  }

  createRowData() {
    return [
      {name: "Sweden", id: 1, abbr: "SE"},
      {make: "Denmark", id: 2, abbr: "DK"},
      {make: "Norway", id: 3, abbr: "NO"}
    ];
  }

  render() {
    let containerStyle = {
      height: '800px',
      width: '500px',
      margin: 'auto'
    };

    //console.log(this.state.columnDefs);

    return (
      <div>
        <div style={containerStyle} className="ag-fresh">
          <AgGridReact
            // properties
            columnDefs={this.state.columnDefs}
            rowData={this.state.rowData}
            groupHeaders="false"
            rowSelection="multiple"
            enableColResize="true"
            enableSorting="true"
            enableFilter="true"
            rowHeight="22"
            debug="true"
          />
        </div>
      </div>
    )
  }
};

export default Countries;

I didn't include some CSS files and Home.js and Cities.js since they render nothing at the moment. This is the result I get:

Resulting page

As you can see, I don't see the grid, just some strange text. I have been browsing for a long time looking at the ag-grid homepage and trying their example code, but I get this all the time.

Any ideas?

2

There are 2 answers

0
Jarod Moser On

This line is your problem:

{headerName: "#", width: "30px", checkboxSelection: true, suppressSorting: true, suppressMenu: true, pinned: true },

Change it to this:

  {headerName: "#", width: 30, checkboxSelection: true, suppressSorting: true, suppressMenu: true, pinned: true },

Found it in the errors in the console, even though it is just a warning, it still messes up the grid to being unrenderable

0
Lex Soft On

The problem is in the following part :

<div style={containerStyle} className="ag-fresh">

You wanted to style with "ag-fresh", but you didn't import the related CSS file. I have tried your code in my laptop, but supplied the style with the one I had. It worked. So you must supply the related CSS file for the style via import statement like so :

import 'ag-grid-community/dist/styles/ag-grid.css';
import 'ag-grid-community/dist/styles/ag-theme-balham.css';

The above additional import is just an example, you have to import your actual CSS. files.