ag-grid-react does not render properly
Asked Answered
C

3

8

Following the sample from the docs: https://www.ag-grid.com/best-react-data-grid/index.php

After creating new react app (have tried several times on different machines)

create-react-app whatever

If I apply the stylesheets (ag-grid.css & theme-fresh.css) all that is rendered is a gray line across the page. Any other combination renders a blank page. Removing ag-grid.css renders the grid but its jumbled all over the place.

Has anyone used this lately successfully with React? Does anyone recommend something different? (requirements: paging, sorting, filtering, selectable row(s))

thanks :-)

import React, { Component } 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-fresh.css';

class App extends Component {
  constructor(props) {
    super(props);
    this.state = {
      columnDefs: [
        {headerName: 'First Name', field: 'name' },
        {headerName: 'Last Name', field: 'lname'  }
      ],
      rowData: [
        { name: 'Dave', lname: 'Smith' },
        { name: 'Tommy', lname: 'Smith' },
        { name: 'Lea', lname: 'Jones' }
      ]
    }
  }
  render() {
    return (
      <div className="ag-fresh">
        <AgGridReact
            columnDefs={this.state.columnDefs}
            rowData={this.state.rowData}
          rowSelection="multiple"
          enableColResize="true"
          enableSorting="true"
          enableFilter="true"
          groupHeaders="true"
          rowHeight="22"
          debug="true"
        />
      </div>
    );
  }
}

export default App;
Chaldron answered 21/9, 2016 at 5:39 Comment(0)
C
16

The outer grid required a height :-(

The documentation does not show this. Not sure why there is no min default height for the grid, but there is not.

Chaldron answered 24/9, 2016 at 18:37 Comment(3)
damn. I would have never thought of that.Bilek
ran into this today... height:100%; fails. Height:200px; works. Angular v14 Ag-grid-angular v 28 ....that should go on the bug list!Edwin
Follow on to previous comment: ag-grid.com/angular-data-grid/filter-quick does have an example that uses height:100% . I do not see where there is any containing div that has a explicit height, so I do not know why it works there.. I do know I was getting the grey line mentioned by OP w/ Height:100%Edwin
I
4

So essentially you need something like this, where the grid is wrapped with an element which has a height:

<div className="ag-fresh">
  <div className="grid_height_fix">
  <AgGridReact columnDefs={this.state.columnDefs} rowData={this.state.data.gridDate} >
  </AgGridReact>
  </div>
</div>
.grid_height_fix {height:800px;}
Istic answered 26/1, 2017 at 14:0 Comment(0)
R
1

The new and better way to do this :

  const GridExample = () => {
    // I use 100% because I set the height of the higher element manually, you can set something like 500px for the height here instead, or calculate it based on the screen size
    const containerStyle = useMemo(() => ({ width: '100%', height: '100%' }), []);
    const gridStyle = useMemo(() => ({ height: '100%', width: '100%' }), []);

    return (
      <div style={containerStyle}>
        <div style={gridStyle} className="ag-theme-alpine">
          <AgGridReact
            ...
          ></AgGridReact>
        </div>
      </div>
    );
  };

this way you have better controle over the dimensions.

Resound answered 2/2, 2023 at 16:33 Comment(2)
I did this exact thing and still had the issueDehumidifier
@Dehumidifier you need to check if the higher element has a height set, because in css if you use "%" values for height your parent element must have a height set, so it's either you use "%" up to the <body> element or set a specific height for the direct parent element (ex: 500px). or you could directly set the height on the container <div>Resound

© 2022 - 2024 — McMap. All rights reserved.