gpt4 book ai didi

reactjs - react /AG-GRID : Dynamically setting columnDefs after retrieving data

转载 作者:行者123 更新时间:2023-12-05 07:23:06 27 4
gpt4 key购买 nike

componentDidMount() 函数中,我正在使用 AXIOS 检索数据,一旦接收到数据,我将尝试更改列 Header Names 我的 AG-GRID 在检索数据后,但 Header Names 不受影响。

请参阅以下代码中的 this.gridOptions.api.setColumnDefs(columnDefs) 行。

var columnDefs = [
{ headerName: "column0", field: "column0", width: 300 },
{ headerName: "column1", field: "column1", width: 100 },
{ headerName: "column2", field: "column2", width: 100 },
{ headerName: "column3", field: "column3", width: 100 },
{ headerName: "column4", field: "column4", width: 100 },
{ headerName: "column5", field: "column5", width: 100 },
];

var PARMS = '';

class Home extends React.Component {
state = {
columnDefs: columnDefs,
header: {},
isLoading: false,
error: null
}

componentDidMount() {
this.setState({ isLoading: true });

axios.get(API + PARMS)
.then(fubar => {
const rowData = fubar.data.results;
this.setState({ rowData });
const headerRow = fubar.data.header;
columnDefs[0].headerName = headerRow.column0;
columnDefs[1].headerName = headerRow.column1;
columnDefs[2].headerName = headerRow.column2;
columnDefs[3].headerName = headerRow.column3;
columnDefs[4].headerName = headerRow.column4;
columnDefs[5].headerName = headerRow.column5;

this.gridOptions.api.setColumnDefs(columnDefs);
})
.catch(error => this.setState({
error,
isLoading: false
}));
}

RENDER() 是:

render() {
const { isLoading, rowData, columnDefs } = this.state;

return (
<div className="ag-theme-balham" style={{ height: '525px', width: '920px' }} >
<h2>{heading}</h2>

<AgGridReact
columnDefs={columnDefs}
rowData={rowData}>
</AgGridReact>
</div>

);
}

我认为上面的代码在做什么(或试图做什么):

  • 定义列定义
  • 网格由列定义呈现
  • 数据来源
  • 重新定义列定义
  • 网格正在(或应该)重新渲染

但这并没有发生。在我的完美世界中,我更愿意:

  • 检索数据
  • 定义列
  • 渲染网格

但有人告诉我“那样不行”。

最佳答案

我的解决方案是对数组进行两个定义,一个设置为 STATE 对象,另一个设置为独立变量。当数据刷新时,独立变量被更新,然后用于替换 STATE 对象。

有没有更好的办法?

var columnDefsNew = [
{ headerName: "", field: "column0", width: 300, },
{ headerName: "", field: "column1", width: 100 },
{ headerName: "", field: "column2", width: 100 },
{ headerName: "", field: "column3", width: 100 },
{ headerName: "", field: "column4", width: 100 },
{ headerName: "", field: "column5", width: 100, }];

class Home extends Component {
constructor(props) {
super(props);

this.state = {
columnDefs: [
{ headerName: "", field: "column0", width: 300 },
{ headerName: "", field: "column1", width: 100 },
{ headerName: "", field: "column2", width: 100 },
{ headerName: "", field: "column3", width: 100 },
{ headerName: "", field: "column4", width: 100 },
{ headerName: "", field: "column5", width: 100 }],
rowData: null,
isLoading: false,
error: null
};
}

componentDidMount() {
this.setState({ isLoading: true });

axios.get(API + PARMS)
.then(fubar => {
const headerRow = fubar.data.header;
const rowData = fubar.data.results;

this.setState({ rowData });

columnDefsNew[0].headerName = headerRow.column0;
columnDefsNew[1].headerName = headerRow.column1;
columnDefsNew[2].headerName = headerRow.column2;
columnDefsNew[3].headerName = headerRow.column3;
columnDefsNew[4].headerName = headerRow.column4;
columnDefsNew[5].headerName = headerRow.column5;

this.setState({ columnDefs: columnDefsNew });
})
.catch(error => this.setState({
error,
isLoading: false
}));
}

render() {
const { isLoading, rowData, columnDefs } = this.state;

return (
<div className="ag-theme-balham" style={{ height: '525px', width: '900px' }} >
<h2>{heading}</h2>

<AgGridReact
columnDefs={columnDefs}
rowData={rowData}>
</AgGridReact>
</div>

);
}
}

export default Home;

关于reactjs - react /AG-GRID : Dynamically setting columnDefs after retrieving data,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56191372/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com