[英]Create Dynamic Column in react.js
I'm trying to create a dynamic column in react.js any body have an idea? 我正在尝试在react.js中创建一个动态列,任何机构都有想法吗? I've already done with the static column but I want to make them dynamic have a look on my code and please give me suggestion.
我已经完成了static列,但想使它们成为动态的,请看一下我的代码,并请给我建议。
import React from 'react';
import ReactDataGrid from 'react-data-grid';
class Example extends React.Component {
constructor(props, context) {
super(props, context);
this.createRows();
this._columns = [
{ key: 'id', name: 'ID' },
{ key: 'title', name: 'Title' },
{ key: 'count', name: 'Count' } ];
this.state = null;
}
createRows = () => {
let rows = [];
for (let i = 1; i < 5; i++) {
rows.push({
id: i,
title: 'Title ' + i,
count: i * 1000
});
}
this._rows = rows;
};
rowGetter = (i) => {
return this._rows[i];
};
render() {
return (
<ReactDataGrid
columns={this._columns}
rowGetter={this.rowGetter}
rowsCount={this._rows.length}
minHeight={200} />);
}
}
export default Example;
You can try this function to create dynamic column key and name 您可以尝试使用此功能来创建动态列键和名称
createColumns = async (data) => {
let columns = [], keys = [];
if (data)
keys = Object.keys(data);
for (let i = 0; i < keys.length; i++) {
columns.push({key: Object.keys(data)[i], name: Object.keys(data)[i]});
}
await this.setState({columns});
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.