ag-grid反应不起作用

时间:2017-09-12 10:15:31

标签: javascript reactjs ag-grid ag-grid-react

我在让ag-Grid与我的反应应用程序一起工作时遇到了问题。无论我尝试什么,我只得到一些文字,我没有看到网格。我不知道我错过了什么。这是我的代码:

/*
 * ----------------------------
 * 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;

我没有包含一些CSS文件以及Home.jsCities.js,因为它们目前没有任何内容。这是我得到的结果:

Resulting page

正如你所看到的,我没有看到网格,只是一些奇怪的文字。我已经浏览了很长时间查看ag-grid主页并尝试他们的示例代码,但我总是得到这个。

有什么想法吗?

2 个答案:

答案 0 :(得分:0)

这一行是你的问题:

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

将其更改为:

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

在控制台中的错误中找到它,即使它只是一个警告,它仍然会让网格变得不可理解

答案 1 :(得分:0)

问题出在以下部分:

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

您想使用“ ag-fresh”样式,但是您没有导入相关的CSS文件。 我已经在笔记本电脑中尝试过您的代码,但是提供了我所拥有的样式。有效。 因此,您必须通过import语句为样式提供相关的CSS文件,如下所示:

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

上述其他导入只是一个示例,您必须导入实际的CSS。文件。