【发布时间】:2018-08-20 17:54:24
【问题描述】:
我对 React Virtualized MultiGrid 组件有一个非常基本的用法,我只是在其中渲染了一个从 1 到 100 的数字表。
由于某种原因,第一行不会被渲染。换句话说,表格总是从数字 2 开始。
这是我的代码。
const Container = {
width: "90%",
height: "100vh",
margin: "auto",
};
class App extends Component {
state = {
data: [],
sort: {
column: "",
descending: false,
},
};
componentDidMount() {
const numbers = [];
for (let i = 0; i < 100; i++) {
numbers.push(i + 1);
}
const final = numbers.map(n => {
return {
single: n,
double: n * 2,
triple: n * 3
};
});
this.setState({ data: final });
}
cellRenderer = (data, columns) => {
if (data.rowIndex === 0) {
return (
<span
style={data.style}
key={data.key}
>
{columns[data.columnIndex]}
</span>
);
}
const column = columns[data.columnIndex];
return (
<span
style={data.style}
key={data.key}
>
{this.state.data[data.rowIndex][column]}
</span>
);
}
render() {
const columns = ["single", "double", "triple"];
return (
<div style={Container}>
<AutoSizer>
{({ width, height }) => (
<MultiGrid
columnWidth={70}
width={width}
height={height}
cellRenderer={(data) => this.cellRenderer(data, columns)}
fixedRowCount={1}
rowHeight={70}
columnCount={3}
rowCount={this.state.data.length}
/>
)}
</AutoSizer>
</div>
);
}
}
这是我输出的截图。
【问题讨论】:
-
在
cellRenderer内,当您在rowIndex === 0时返回列名而不是数据本身 -
这实际上是有道理的,但是我不知道如何拥有标题和第一行。
标签: javascript reactjs react-virtualized