列表页数据量过大页面渲染节点过多会触发Dom limit exceeded .
解决方案如下。
欢迎讨论交流
// index.js Page({ data: { currentPage: 0, pageFrame:[] }, pageSize: 10, currentPage:0,
var list = []; for ( var i = 0; i< 10000 ; i++) { list.push({ name: 'ssss----' + i, desc: 'aaaaa----' + i, content: 'adasdadasdadadasdadadadadasdadad ----' + i });
this .pagedList = this .pageList(list); this .setData({ listLength: list.length, list: [ this .pagedList[0]] })
onListScroll(e) { if ( this .inPageUpdate) { return ; } var { scrollTop } = e.detail; if ( this .currentPage > 0) { var pageFrame = this .data.pageFrame[ this .currentPage - 1]; var screenHeight = wx.getSystemInfoSync().screenHeight; if ((scrollTop + screenHeight) - (pageFrame.lastBottom + pageFrame.height) < -200) { this .inPageUpdate = true ; this .currentPage -= 1; this .setData({ currentPage: this .currentPage, }, () => { this .inPageUpdate = false ; }) } } var currentPageFrame = this .data.pageFrame[ this .currentPage]; if (currentPageFrame) { if (scrollTop - (currentPageFrame.lastBottom + currentPageFrame.height) > 200) { this .inPageUpdate = true ; this .currentPage += 1; this .setData({ currentPage: this .currentPage, }, () => { this .inPageUpdate = false ; }) } }
reachPageBottom() { if ( this .inPageUpdate) { return ; } this .inPageUpdate = true ; if ( this .currentPage < this .pagedList.length - 1) { var self = this ; var currentPage = this .currentPage; wx.createSelectorQuery().select( '#listpage-' + this .currentPage).boundingClientRect( function (rect) { if (currentPage > 0) { rect.lastBottom = self.data.pageFrame[currentPage - 1].height + self.data.pageFrame[currentPage - 1].lastBottom } else { rect.lastBottom = 0; } self.setData({ [`pageFrame[${currentPage}]`]: rect }) }).exec(); this .currentPage = this .currentPage + 1; var nextPage = this .pagedList[ this .currentPage]; var key = `list[${ this .currentPage}]` var data = {}; data[key] = nextPage; data.currentPage = this .currentPage; console.log(data); this .setData(data, () => { this .inPageUpdate = false ; }); } else { this .setData({ pageEnd: true , }, () => { this .inPageUpdate = false ; }) } },
console.log(e.currentTarget.dataset);
pageList(list) { var splitArray = (arr, len) => { var a_len = arr.length; var result = []; for ( var i = 0; i < a_len; i += len) { result.push(arr.slice(i, i + len)); } return result; } var pagedList = splitArray(list, this .pageSize); return pagedList; } }) |
/*index.wxss*/ page { height : 100% ; } .list { height : 100% ; width : 100% ; } .listitem { line-height : 50 rpx; padding : 20 rpx; font-size : 32 rpx; color : #ff0000 ; border-bottom : 1 rpx solid #eeeeee ; } .list-end{ text-align : center ; font-size : 18 rpx; color : #b2b2b2 ; margin : 15 rpx 0 7.5 rpx; display : flex; flex- direction : row; justify- content : center ; align-items: center ; } .list-end .line { width : 20% ; height : 1px ; background-color : #dddddd ; margin : 0 30 rpx; } |
<!-- index.wxml -->
< template is = "listitem" wx:for = "{{subList}}" data = "{{item:item, page,index}}" /> </ block > < view wx:else style = 'height:{{pageFrame[page].height}}px;' ></ view > </ view > < view class = "list-end" wx:if="{{listLength > 0}}"> < view class = 'line' ></ view > < text wx:if = "{{pageEnd}}" >以上共{{listLength || 0}}个数据</ text > < button wx:else loading = "true" disabled = "true" bindtap = "empty" class = "button-noborder" style = 'font-size: 26rpx; background-color:transparent' >正在加载更多...</ button > < view class = 'line' ></ view > </ view > </ scroll-view > < template name = "listitem" > < view class = 'listitem' bindtap = 'listItemTap' data-item = '{{item}}' > < view >{{item.name}}</ view > < view >{{item.desc}}</ view > < view >{{item.content}}</ view > <!-- 循环100个节点 --> < text >{{page}}--</ text > < text wx:for = "{{100}}" wx:for-index = 'i' >{{index}}</ text > </ view > </ template > |
demo见代码片段
https: //developers.weixin.qq.com/s/ItRHXsmg7L5r |
使用小程序开发工具导入片段可直接运行
可以,先mark
怎么下拉回看加载过的数据,有延迟显示?
回看数据延迟显示问题怎么解决的啊?