最新版本的AG-Grid有所变化,原先的自定义CSS方法现在已不再适用。解决这个问题的方法是使用AG-Grid提供的主题样式表和SASS预处理器定制样式。
以下是一个例子,使用SCSS定制表格颜色:
首先,使用npm安装ag-grid和ag-grid-react和node-sass:
npm install ag-grid ag-grid-react node-sass
然后在React组件中使用样式:
import React from 'react';
import 'ag-grid-community/dist/styles/ag-grid.css';
import 'ag-grid-community/dist/styles/ag-theme-alpine.css';
import './MyGrid.scss';
import { AgGridReact } from 'ag-grid-react';
const columnDefs = [
{ headerName: "Make", field: "make" },
{ headerName: "Model", field: "model" },
{ headerName: "Price", field: "price" }
];
const rowData = [
{ make: "Toyota", model: "Camry", price: 25000 },
{ make: "Honda", model: "Accord", price: 30000 },
{ make: "Ford", model: "Mustang", price: 40000 }
];
function MyGrid() {
return (
)
}
export default MyGrid;
然后创建一个MyGrid.scss文件,定义表格颜色:
@import '~ag-grid-community/src/styles/ag-grid.scss';
@import '~ag-grid-community/src/styles/ag-theme-alpine/sass/ag-theme-alpine-mixin.scss';
.ag-theme-alpine {
@include ag-theme-alpine();
.ag-header-cell {
background-color: #004482;
}
.ag-row {
background-color: #e6f1f7;
}
}
最后,确保scss文件正确引入样式表和SASS预处理器,重新启动你的React应用程序即可看到定制后的表格样式。