虚拟渲染 [英] Virtual rendering

查看:51
本文介绍了虚拟渲染的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从头开始构建datagrid. 我知道有很好的解决方案.

I am building datagrid from scratch. I know there are very good solution available.

我必须特别注意网格的性能.我最多可以有5000行(包括rowpans,colspans和内联编辑)

I have to pay special attention to the performances of the grid. I will have a maximum of 5000 rows (with rowspans, colspans and inline editing)

我有一个让我印象深刻的功能是在网格中实现的虚拟渲染,例如SlickGrid和DataTables.

I have a feature that really impresses me is the virtual rendering that is implemented in grid such a SlickGrid and DataTables.

我想知道是否有人可以给我关于如何使用Jquery/javascript实现虚拟渲染的见解.

I was wondering if someone could give me an insight on how to implement virtual rendering using Jquery / javascript.

谢谢

推荐答案

我有一个使用react的很好的示例,代码非常小,您可以使用Jquery来更新Dom.

I have a really good example using react, the code is quite tiny and you can do a version of it using Jquery to update the Dom.

const data = []
function createData(){
	for (let i=0;i<1000000;i++){
  	data.push({name: `Row ${i}`});
  }
}
createData();

//Item class to render each of the list rows
class Item extends React.Component{
    constructor(props){
        super(props);
    }
    render(){
        return (
        <div className="item" style={{top:this.props.top,height:this.props.itemheight}}>
                {this.props.label}
        </div>)
          
    }
}


//The list component that contains the rows and manage the virtual rendering 
// using the vertical scroll position
class Vlist extends React.Component{
    constructor(props){
        super(props);
        this.numVisibleItems=Math.trunc(300 / this.props.itemheight);
        this.state={
            start:0,
            end:this.numVisibleItems         
        }
        this.containerStyle={height:data.length * this.props.itemheight}
        
        this.scollPos=this.scollPos.bind(this)
    }

    scollPos(){
        let currentIndx=Math.trunc(this.refs.viewPort.scrollTop/this.props.itemheight)
        currentIndx=currentIndx-this.numVisibleItems>=data.length?currentIndx-this.numVisibleItems:currentIndx;
        if (currentIndx!==this.state.start){
            console.log("Redraw");
            this.setState(
                this.state={
                    start:currentIndx,
                    end:currentIndx+this.numVisibleItems>=data.length ? data.length-1:currentIndx+this.numVisibleItems
                }
            )
        }
       
    }
    
    renderRows(){
        let result=[];
        for (let i=this.state.start;i<=this.state.end;i++){
            let item=data[i];
            result.push(<Item key={i} label={item.name} top={i*this.props.itemheight} itemheight={this.props.itemheight} />);
        }
        return result;
    }
    
    render(){
        return (
        <div ref="viewPort"  className="viewPort" onScroll={this.scollPos} >
            <div className="itemContainer" style={this.containerStyle}>
                {this.renderRows()}    
            </div>
        </div>)
    }

}

ReactDOM.render(<Vlist itemheight={30} />, document.querySelector("#app"))

.viewPort {
  position: relative;
  width: 510px;
  height: 300px;
  border: solid 1px;
  overflow-y: scroll;
}

.itemContainer {
  position: absolute;
  width: 500px;
  background-color: azure;
}

.item {
  position: absolute;
  background-color: beige;
  border: solid 1px;
  width: 500px;
  text-align: center;
}

.done {
  color: rgba(0, 0, 0, 0.3);
  text-decoration: line-through;
}

<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
<div id="app"></div>

这篇关于虚拟渲染的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆