javascript - 虚拟渲染

标签 javascript jquery

我正在从头开始构建数据网格。 我知道有非常好的解决方案可用。

我要特别注意格子的表现。我将有最多 5000 行(使用行跨度、列跨度和内联编辑)

我有一个真正让我印象深刻的功能是在 SlickGrid 和 DataTables 等网格中实现的虚拟渲染。

我想知道是否有人可以告诉我如何使用 Jquery/javascript 实现虚拟渲染。

谢谢

最佳答案

我有一个使用 React 的非常好的示例,代码非常小,您可以使用 Jquery 来更新 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>

关于javascript - 虚拟渲染,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20387863/

相关文章:

Javascript getElementsByName.value 不起作用

javascript - Jquery 移动到其他 div(动画)

javascript - canvas 或 requestAnimationFrame 在移动设备上速度很慢

javascript - 处理两个单独的 JQuery Api 调用

javascript隐藏/取消隐藏,但希望箭头指向向下/向上隐藏/取消隐藏

javascript - 有没有比纯 CSS 2 更好的、符合标准的方法来为网站制作下拉导航?

php - 将参数从 $.post() 回调传递给外部变量

javascript - 如何使用 Jquery/Javascript 在 li 中添加和删除事件类以锚定标记

javascript - 使用js库内部缓存的数据

javascript - 如何删除除一个以外的所有切换?