【发布时间】:2018-03-15 04:11:24
【问题描述】:
我刚刚使用 InfiniteLoader、Table、Column 和 AutoSizer 完成了一个工作表,我意识到该表不会随浏览器窗口水平缩放。尽管图书馆的大部分内容都有很好的记录,但我花了很长时间才完成这项工作。我想要实现的是一个类似 HTML 的表格,它根据浏览器窗口的宽度调整大小,随着内容的换行垂直增加行高,并使用具有不同行高的无限负载。反应虚拟化是否可以实现所有这些点?似乎最好先问一下,因为组合这个库的所有移动部分可能会很棘手。谢谢!
这是我的组件:
import React from 'react';
import fetch from 'isomorphic-fetch';
import { InfiniteLoader, Table, Column, AutoSizer } from 'react-virtualized';
import { connect } from 'react-redux';
import { CSSTransitionGroup } from 'react-transition-group';
import { fetchProspects } from '../actions/prospects';
import Footer from './footer';
class ProspectsTable extends React.Component {
constructor(props, context) {
super(props, context);
this.renderProspects = this.renderProspects.bind(this);
this.isRowLoaded = this.isRowLoaded.bind(this);
this.loadMoreRows = this.loadMoreRows.bind(this);
this.rowRenderer = this.rowRenderer.bind(this);
this.state = {
remoteRowCount: 200,
list: [
{
fullName: '1 Vaughn',
email: 'brianv@gmail.com',
phone: '608 774 6464',
programOfInterest: 'Computer Science',
status: 'Active',
dateCreated: '10/31/2017',
},
{
fullName: '2 Vaughn',
email: 'brianv@gmail.com',
phone: '608 774 6464',
programOfInterest: 'Computer Science',
status: 'Active',
dateCreated: '10/31/2017',
},
{
fullName: '3 Vaughn',
email: 'brianv@gmail.com',
phone: '608 774 6464',
programOfInterest: 'Computer Science',
status: 'Active',
dateCreated: '10/31/2017',
},
],
};
}
isRowLoaded({ index }) {
return !!this.state.list[index];
}
loadMoreRows({ startIndex, stopIndex }) {
return fetch(`http://localhost:5001/api/prospects?startIndex=${startIndex}&stopIndex=${stopIndex}`)
.then((response) => {
console.log('hi', response);
console.log('hi', this.props);
});
}
rowRenderer({ key, index, style }) {
return (
<div
key={key}
style={style}
>
{this.state.list[index]}
</div>
);
}
render() {
return (
<InfiniteLoader
isRowLoaded={this.isRowLoaded}
loadMoreRows={this.loadMoreRows}
rowCount={this.state.remoteRowCount}
>
{({ onRowsRendered, registerChild }) => (
<div>
<AutoSizer>
{({ width }) => (
<Table
ref={registerChild}
onRowsRendered={onRowsRendered}
width={width}
height={300}
headerHeight={20}
rowHeight={30}
rowCount={this.state.list.length}
rowGetter={({ index }) => this.state.list[index]}
>
<Column
label="Full Name"
dataKey="fullName"
width={width / 6}
/>
<Column
width={width / 6}
label="Email"
dataKey="email"
/>
<Column
width={width / 6}
label="Phone"
dataKey="phone"
/>
<Column
width={width / 6}
label="Program of Interest"
dataKey="programOfInterest"
/>
<Column
width={width / 6}
label="Status"
dataKey="status"
/>
<Column
width={width / 6}
label="Date Created"
dataKey="dateCreated"
/>
</Table>
)}
</AutoSizer>
</div>
)}
</InfiniteLoader>
);
}
}
}
const mapStateToProps = state => ({
prospects: state.prospects,
});
export default connect(mapStateToProps)(ProspectsTable);
这是一个与我的代码非常相似的 plnkr,我经常引用它:https://plnkr.co/edit/lwiMkw?p=preview
【问题讨论】:
标签: javascript reactjs react-virtualized