May 21, 2021 WeChat Mini Program Development Document
The small program length list component
Using this component relies on the small program base library version 2.2.2, as well as on the npm build of the developer tool. Details can be found in the official npm documentation.
At present, small programs will have a lot of application scenarios will use infinite long list of interaction, when a page displays a lot of information, will cause small program pages of Caton and white screen. There are several reasons for this:
Therefore, there is a long list assembly to solve these problems.
The core idea is that only the data displayed on the screen is rendering that the basic implementation is to listen to the Scroll event, and recalculate the data that needs to be rendered, and the data that needs to be rendered will leave an empty DIV placeholder element.
Suppose list data has 100 items, knows the position of the scrolling, how do you know which items must be displayed on the page?Because Item has not been rendered, you cannot get every Item's location through DOM operations such as getComputeStyle, so you can't know which Item needs to be rendered.In order to solve this problem, each ITEM is required to be fixed.Item's wide definition See the parameter Itemsize of the CreateRecycleContext () of the API below.
During the scroll, the data is renovated, and the DIV placeholder height before and after the current data is required, and it means in the same rendering cycle.The page rendering is triggered by SetData. The list data and DIV seating height are setDATA in 2 components. In order to put these 2 setData in the same rendering cycle, use a Hack method, so define the RECYCLE-VIEW BATCHThe property is fixed to batch = "{{BatchsetRecyCLEDATA}}".
In the scrolling process, in order to avoid frequent white screens, the contents of the two screens of the current screen will be rendered.
Long list components consist of 2 custom components Recycle-View, Recycle-Item, and a set of APIs, the corresponding code structure is as follows
├── miniprogram-recycle-view/
└── recycle-view 组件
└── recycle-item 组件
└── index.js
The package structure is described in detail below:
Directory / file | describe |
---|---|
Recycle-View component | Long list assembly |
Recycle-ITEM component | Long list Each ITEM component |
index.js | API for operating long list data |
Installation component
npm install --save miniprogram-recycle-view
2. Add the configuration of the Recycle-View and Recycle-Item custom components in the JSON profile of the page.
{
"usingComponents": {
"recycle-view": "miniprogram-recycle-view/recycle-view",
"recycle-item": "miniprogram-recycle-view/recycle-item"
}
}
3. Reference RecyCle-View in WXML file
<recycle-view batch="{{batchSetRecycleData}}" id="recycleId">
<view slot="before">长列表前面的内容</view>
<recycle-item wx:for="{{recycleList}}" wx:key="id">
<view>
<image style='width:80px;height:80px;float:left;' src="{{item.image_url}}"></image>
{{item.idx+1}}. {{item.title}}
</view>
</recycle-item>
<view slot="after">长列表后面的内容</view>
</recycle-view>
The properties of recycle-view are described below:
The field name | Type | Required | Describe |
---|---|---|---|
Id | String | Is | The id must be a unique string for the page |
batch | Boolean | Is |
It must
{{batchSetRecycleData}}
|
height | Number | Whether | Set the height of recycle-view, which defaults to the height of the page |
width | Number | Whether | Set the width of the recycle-view, which defaults to the width of the page |
enable-back-to-top | Boolean | Whether | The default is false, a field with the same name as scroll-view |
scroll-top | Number | Whether | The default is false, a field with the same name as scroll-view |
scroll-y | Number | Whether | The default is true, a field with the same name as scroll-view |
scroll-to-index | Number | Whether | Set items that scroll to a long list |
placeholder-image | String | Whether | The default placeholder background picture, displayed when rendering is not timely, is not recommended for large graphs as occupy. T he Base64 format for SVG is recommended, and the tool can be used to convert the SVG code to Base64 format. It supports setting rpx in SVG. |
scroll-with-animation | Boolean | Whether | The default is false, a field with the same name as scroll-view |
lower-threshold | Number | Whether | The default is false, a field with the same name as scroll-view |
upper-threshold | Number | Whether | The default is false, a field with the same name as scroll-view |
bindscroll | Event | Whether | Field with the same name as scroll-view |
bindscrolltolower | Event | Whether | Field with the same name as scroll-view |
bindscrolltoupper | Event | Whether | Field with the same name as scroll-view |
Recycle-view contains 3 slots, as follows:
Name | Describe |
---|---|
before | The previous non-recycled area of the default slot |
The default slot | A long list shows the area, and recycle-item must be defined in the default slot |
after | The non-recycled area after the default slot |
The contents of the long list are actually in a scroll-view scroll area, and when the contents of the long list are more than just a separate list, for example, there is a copyright declaration at the bottom of our page, so we can put the contents of this section in the two slots before and after.
The introduction to recycle-item is as follows:
It is important to note that the wx:for list loop must be defined in recycle-item, not by setting the variables of the wx:for binding through setData, but by creating the RecycleContext object through the creativeRecycleContext method to manage the data, which is defined in the index.js file. It is recommended that you set wx:key at the same time to improve the rendering performance of the list.
4. Page JS manages recycle-view data
const createRecycleContext = require('miniprogram-recycle-view')
Page({
onReady: function() {
var ctx = createRecycleContext({
id: 'recycleId',
dataKey: 'recycleList',
page: this,
itemSize: { // 这个参数也可以直接传下面定义的this.itemSizeFunc函数
width: 162,
height: 182
}
})
ctx.append(newList)
// ctx.update(beginIndex, list)
// ctx.destroy()
},
itemSizeFunc: function (item, idx) {
return {
width: 162,
height: 182
}
}
})
The page must be defined by the Component constructor, and after the introduction of the miniprogram-recycle-view package, the contentRecyContext object is created under the wx object to manage the data defined by the recycle-view, and the createRecycleContext receives parameters of 1 Object. Each key of the Object parameter is described below:
The name of the argument | Type | Describe |
---|---|---|
Id | String | The value of the id property that corresponds to recycle-view |
dataKey | String | The name of the binding variable that corresponds to the wx:for property setting for recycle-item |
page | Page/Component | An instance of a page or component on which recycle-view is located, or within a component, can pass this directly |
itemSize | Object/Function |
This parameter is used to generate the width and height of the recycle-item, as mentioned earlier, to know which items need to be rendered today, you must know the width and height of the item to calculate
Object must contain two properties, the function receives the item, index, and returns an Object that contains the word "width, height" If itemSize is a function, inside
this
points to RecycleContext
If the style uses rpx, you can convert it to px by transformRpx. For the Object type, there is another use, as detailed in the itemSize section below. |
useInPage | Boolean |
Whether the entire page is only realcle-view. P
age's definition must contain at least an empty onPageScroll function, mainly for long lists at the page level, and requires the effect of onPullDownRefresh.
The cut must set
root
parameter to the current page object
|
root | Page | The current page object can be obtained through getCurrentPages, when the useInPage must be provided for true |
The RecycleContext object provides methods such as:
Method | Parameters | Description |
---|---|---|
append | list, callback | By appending list data to the current long list data, callback is a rendered callback function |
splice | begin, count, list, callback | Insert/delete long list data, parameters with Array's splice function, callback is a rendered callback function |
update | begin, list, callback | Update the data of the long list, starting with the index parameter begin, update to the parameter list, parameter callback with the splice. |
destroy | No | Destroy the RecycleContext object and call this method when the recycle-view is destroyed |
forceUpdate | callback, reinitSlot | Re-render recycle-view. C allback is a rendered callback function that is called when the height of the before andafter two slots changes, and the reinitSlot is set to true. This method is also called when the width and height of the item changes. |
getBoundingClientRect | index | Gets the position of a data item in a long list and returns the Object of 'left, top, width, height'. |
getScrollTop | No | Gets the current scrolling position of the long list. |
transformRpx | rpx | Convert rpx to px and return the converted px integer. T he wide and high unit returned by itemSize is px, where this function can be called to convert rpx to px, and the argument is Number, such as ctx.transformRpx (140), returning 70. Note that transformRpx is rounded, so transformRpx (20) and transformRpx (90) are not necessarily equal to transformRpx (110) |
getViewportItems | inViewportPx | Gets a data item in the window to determine if an item appears in the window. F or exposure data reporting, the linkage effect of dishes and categories is achieved. The parameter inViewportPx indicates how many pixels from the screen appear inside the screen and can be negative. |
Where itemSize is used
ItemSize can be an Object that contains s width, height, and all data has only one wide-height message. If there are multiple, you can provide a function that the long list component calls to generate the width information for each data, as follows:
function(item, index) {
return {
width: 195,
height: item.azFirst ? 130 : 120
}
}
<recycle-view batch="{{batchSetRecycleData}}" batch-key="batchSetRecycleData"></recycle-view>
<recycle-view batch="{{batchSetRecycleData1}}" batch-key="batchSetRecycleData1"></recycle-view>