react-virtualized的WindowScroller、List、AutoSizer使用

主要代码如下,红色字体为react-virtualized使用代码。
注意scrollElement 的定义,滚动区域宽高都可自定义,根据需要使用组件
import React, { Component } from 'react';
import { List, Flex, Button, Modal } from 'antd-mobile';
import { connect } from 'dva';
import { List as VList, CellMeasurerCache, CellMeasurer,WindowScroller,AutoSizer } from 'react-virtualized';
import GoodsDetail from '../GoodsDetail';
import 'react-virtualized/styles.css'; // only needs to be imported once
import styles from './index.less';

const alert = Modal.alert;
const ListItem = List.Item;
const FlexItem = Flex.Item;
class OwnerPlanoutDetail extends Component {
    constructor(props) {
        super(props);
        this.state = {
            visible:false,
            width: 0
        };
    }

    measureCache = new CellMeasurerCache({
        fixedWidth: true,
        minHeight: 220
    });
    // 使用WindowScroller必须,根据滚动条所在元素去设置
    _setRef = (ref) => {
        this.setState({scrollElement: ref});
    }
    render() {
        const self = this;
        const { goods=[] } = this.props;
 
        // 商品不足20条时不适用虚拟列表
        const dom = goods.map((item,index)=>{
            return <GoodsDetail
                key={index}
                data={item}
            />
        });
        return (
            <div ref={this._setRef} key="1">
                <List className={styles.titleArea}>
                    标题
                </List>
                <div className={styles.gutter}></div>
                
               
<List style={{marginBottom:'1.5rem'}}>
    <h1>商品信息</h1>
    {goods.length > 20 ? <WindowScroller
        scrollElement = {this.state.scrollElement}
    >
        {({ height, isScrolling, onChildScroll, scrollTop, registerChild }) => {
            return (
                <AutoSizer disableHeight>
                    {({width}) => (
                        <div ref={registerChild}>
                            <VList
                                height={height}
                                width={width}
                                isScrolling={isScrolling}
                                onScroll={onChildScroll}
                                scrollTop={scrollTop}
                                rowCount={goods.length}
                                rowRenderer={
                                    ({
                                         key,         // Unique key within array of rows
                                         index,       // Index of row within collection
                                         parent,
                                         style // Style object to be applied to row (to position it)
                                     })=>{
                                        return (
                                            <CellMeasurer cache={this.measureCache} columnIndex={0} key={key} parent={parent} rowIndex={index}>
                                                <GoodsDetail
                                                    key={key}
                                                    style={style}
                                                    data={goods[index]}
                                                />
                                            </CellMeasurer>
                                        )
                                    }
                                }
                                deferredMeasurementCache={this.measureCache}
                                rowHeight={this.measureCache.rowHeight}
                            />
                        </div>
                    )}
                </AutoSizer>

            )
        }
        }
    </WindowScroller> : dom}
</List>
            </div>
        )
    }
}

 

 

  • 5
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
React-virtualized 是一个用于 React 应用程序的轻量级、高效的虚拟滚动库,可以用来渲染大量数据的表格。它提供了两个组件用于实现表格虚拟滚动:`Grid` 和 `List`。 其中,`Grid` 组件用于渲染固定行数和列数的表格,而 `List` 组件则用于渲染单列列表数据。在使用这两个组件之前,需要先安装 react-virtualized 库: ``` npm install --save react-virtualized ``` 接着,可以通过以下步骤来使用 `Grid` 组件实现表格虚拟滚动: 1. 引入 `Grid` 组件 ```javascript import { Grid } from 'react-virtualized'; ``` 2. 在组件中使用 `Grid` 组件 ```javascript <Grid cellRenderer={cellRenderer} columnCount={columnCount} columnWidth={columnWidth} height={height} rowCount={rowCount} rowHeight={rowHeight} width={width} /> ``` 其中,`cellRenderer` 是一个回调函数,用于渲染单元格;`columnCount` 和 `rowCount` 分别表示表格列数和行数;`columnWidth` 和 `rowHeight` 表示单元格的宽度和高度;`width` 和 `height` 表示表格的宽度和高度。 3. 实现 `cellRenderer` 回调函数 ```javascript function cellRenderer({ columnIndex, key, rowIndex, style }) { const rowData = rows[rowIndex]; const cellData = rowData[columnIndex]; return ( <div key={key} style={style}> {cellData} </div> ); } ``` 其中,`columnIndex` 和 `rowIndex` 分别表示单元格所在的列和行,`key` 是 React 组件的唯一标识,`style` 包含单元格的样式信息。 通过以上步骤,就可以实现基于 react-virtualized 的表格虚拟滚动了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值