Skip to content

Commit 3176a01

Browse files
committed
Variable mode support automatic get height. (#27)
1 parent 77d7112 commit 3176a01

File tree

3 files changed

+30
-4
lines changed

3 files changed

+30
-4
lines changed

README.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,12 +129,23 @@ new Vue({
129129
| totop | Function | * | Called when virtual-list is scrolled to top, no param. |
130130
| tobottom | Function | * | Called when virtual-list is scrolled to bottom, no param. |
131131
| onscroll | Function | * | Called when virtual-list is scrolling, param: `(e, { offset, offsetAll, start, end })`. |
132-
| variable | Function | * | For using virtual-list with variable height mode, this prop is a variable height getter function which is called with param: `(index)` when each item is ready to be calculated. |
132+
| variable | Function or Boolean | * | For using virtual-list with variable height mode. If assign `Function`, this prop is a variable height getter function which is called with param: `(index)` when each item is ready to be calculated. If assign `Boolean`, virtual-list will get each item variable height by it's inline style height automatic. |
133133

134134
### About variable height
135135

136136
In variable height mode, prop `size` is still required. All the index variable height and scroll offset will be cached by virtual-list after the binary-search calculate, if you want to change anyone `<Item/>` height from data, you should call virtual-list's `updateVariable(index)` method to clear the offset cached, refer to [variable example](https://github.com/tangbc/vue-virtual-scroll-list/blob/master/examples/variable/variable.vue#L1) source for detail.
137137

138+
If you are using `variable` assign by `Boolean`, **do not** set inline style height inside `<item/>` component, you must set inline style height on `<item/>` component outside directly, such as:
139+
```vue
140+
<template>
141+
<div>
142+
<virtualList :size="40" :remain="8" :variable="true">
143+
<Item v-for="item of items" :key="item.id" :style="{ height: item.height + 'px' }" />
144+
</virtualList>
145+
</div>
146+
</template>
147+
```
148+
138149

139150
## Contributions
140151

index.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
wtag: { type: String, default: 'div' },
4343
wclass: { type: String, default: '' },
4444
start: { type: Number, default: 0 },
45-
variable: Function,
45+
variable: [Function, Boolean],
4646
bench: Number,
4747
debounce: Number,
4848
totop: Function,
@@ -188,7 +188,22 @@
188188
// return a variable size (height) from given index.
189189
getVarSize: function (index, nocache) {
190190
var cache = this.delta.varCache[index]
191-
return (!nocache && cache && cache.size) || this.variable(index) || 0
191+
if (!nocache && cache) {
192+
return cache.size
193+
}
194+
195+
if (typeof this.variable === 'function') {
196+
return this.variable(index) || 0
197+
} else if (typeof this.variable === 'boolean') {
198+
var slot = this.$slots.default[index]
199+
var calcStyle = slot && slot.data && slot.data.style
200+
if (calcStyle && calcStyle.height) {
201+
var mc = calcStyle.height.match(/^(.*)px$/)
202+
return (mc && +mc[1]) || 0
203+
}
204+
}
205+
206+
return 0
192207
},
193208

194209
// return the variable paddingTop base current zone.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "vue-virtual-scroll-list",
3-
"version": "1.1.8",
3+
"version": "1.1.9",
44
"description": "A vue (2.x) component that support big data and infinite loading by using virtual scroll list.",
55
"main": "index.js",
66
"files": [

0 commit comments

Comments
 (0)