其他分享
首页 > 其他分享> > Ag-Grid React入门

Ag-Grid React入门

作者:互联网

Ag-Grid和React兼容性:

reactAg-Grid
15.x18 - 21.2.0
16.3X22+

安装

npm install --save ag-grid-community ag-grid-react ag-grid-enterprise

使用

// import依赖
import React, { useState } from 'react'
import { AgGridColumn, AgGridReact } from 'ag-grid-react';
import 'ag-grid-enterprise'
import 'ag-grid-community/dist/styles/ag-grid.css';
import 'ag-grid-community/dist/styles/ag-theme-alpine.css';
// 定义数据rowData
const [rowData, setRowData] = useState([
	{ make: "Toyota", model: "Celica", price: 35000 },
	{ make: "Ford", model: "Mondeo", price: 32000 },
	{ make: "Porsche", model: "Boxter", price: 72000 }
])
// grid定义
<div className="ag-theme-alpine-dark" style={ { height: 400, width: 600 } }>
	<AgGridReact
		rowSelection="multiple"
		rowData={rowData}
	>
		<AgGridColumn field="make" sortable={true} filter={true} checkboxSelection={true}></AgGridColumn>
		<AgGridColumn field="model" filter={true}></AgGridColumn>
		<AgGridColumn field="price" sortable={true}></AgGridColumn>
	</AgGridReact>
</div>

基本使用

排序

<AgGridColumn field="price" sortable={true}></AgGridColumn>

过滤

<AgGridColumn field="model" filter={true}></AgGridColumn>

选择

// params 包含有type、api(gridApi)、columnApi
const [gridApi, setGridApi] = useState(null)
const [gridColumnApi, setGridColumnApi] = useState(null)

function onGridReady(params) {
	setGridApi(params.api);
	setGridColumnApi(params.columnApi);
}
<AgGridReact
	rowSelection="multiple"
	rowData={rowData}
	onGridReady={onGridReady}
>
	<AgGridColumn field="make" checkboxSelection={true}></AgGridColumn>
</AgGridReact>

分组

<AgGridReact
	rowSelection="multiple"
	rowData={rowData}
	onGridReady={onGridReady}
	groupSelectsChildren={true}
	autoGroupColumnDef={{
		headerName: "Price",
		field: "price",
		cellRenderer:'agGroupCellRenderer',
		cellRendererParams: {
			checkbox: true
		}
	}}
>
	<AgGridColumn field="make" filter={true} rowGroup={true}></AgGridColumn>
</AgGridReact>

标签:Ag,ag,onGridReady,React,rowData,grid,import,Grid,true
来源: https://blog.csdn.net/zhaowanwan/article/details/110084177