更新 defaultColumnDefs 不会重新呈现网格
我试图添加一个在网格中切换排序的按钮,当我单击该按钮时,控制台会记录可排序已切换但网格从未呈现的 defaultColumnDefs。
import React, { useState } from 'react';
import { AgGridReact } from 'ag-grid-react';
import 'ag-grid-community/dist/styles/ag-grid.css';
import 'ag-grid-community/dist/styles/ag-theme-alpine.css';
function App() {
const [sort, setSort] = useState(false);
const [rowData] = useState([
{ make: 'Toyota', model: 'Celica', price: 35000 },
{ make: 'Ford', model: 'Mondeo', price: 32000 },
{ make: 'Porsche', model: 'Boxter', price: 72000 },
]);
const [columnDefs] = useState([
{ field: 'make' },
{ field: 'model' },
{ field: 'price' },
]);
const [defaultColDef, setDefaultColDef] = useState({
sortable: sort,
});
const sortHandler = () => {
setDefaultColDef({ ...defaultColDef, sortable: !sort });
setSort(prev => !prev);
};
console.log(defaultColDef);
return (
<div>
<div className="ag-theme-alpine" style={{ height: 400, width: 600 }}>
<AgGridReact
columnDefs={columnDefs}
defaultColDef={defaultColDef}
rowData={rowData}
></AgGridReact>
</div>
<button onClick={sortHandler}> {sort ? 'No sort' : 'Sort'} </button>
</div>
);
}
export default App;
可能出了什么问题?
I'm trying to add a button that toggles sorting in the grid, when I click on the button, and console log the defaultColumnDefs that sortable got toggled but the grid never rendered.
import React, { useState } from 'react';
import { AgGridReact } from 'ag-grid-react';
import 'ag-grid-community/dist/styles/ag-grid.css';
import 'ag-grid-community/dist/styles/ag-theme-alpine.css';
function App() {
const [sort, setSort] = useState(false);
const [rowData] = useState([
{ make: 'Toyota', model: 'Celica', price: 35000 },
{ make: 'Ford', model: 'Mondeo', price: 32000 },
{ make: 'Porsche', model: 'Boxter', price: 72000 },
]);
const [columnDefs] = useState([
{ field: 'make' },
{ field: 'model' },
{ field: 'price' },
]);
const [defaultColDef, setDefaultColDef] = useState({
sortable: sort,
});
const sortHandler = () => {
setDefaultColDef({ ...defaultColDef, sortable: !sort });
setSort(prev => !prev);
};
console.log(defaultColDef);
return (
<div>
<div className="ag-theme-alpine" style={{ height: 400, width: 600 }}>
<AgGridReact
columnDefs={columnDefs}
defaultColDef={defaultColDef}
rowData={rowData}
></AgGridReact>
</div>
<button onClick={sortHandler}> {sort ? 'No sort' : 'Sort'} </button>
</div>
);
}
export default App;
What might be wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
sort
属性是有状态的,这意味着您可以更改它们而无需更新列定义。为此,您可以调用columnApi
方法applyColumnState()
请参阅有关 列状态
这是一个 动态更改排序的示例 网格的
The
sort
property is stateful meaning that you can change them without having to update the column definition. To do this, you call thecolumnApi
methodapplyColumnState()
See documentation on Column State
Here is an example that dynamically changes the sorting of the grid