Skip to content

Commit 9be92f5

Browse files
committed
Fix static height mode index error
1 parent e2384fc commit 9be92f5

File tree

7 files changed

+47
-26
lines changed

7 files changed

+47
-26
lines changed

dist/vue-virtual-scroller.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs-src/src/App.vue

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
<span>
55
<input v-model="countInput" type="number" min="0" max="500000" /> items
66
</span>
7+
<label>
8+
<input v-model="enableLetters" type="checkbox" /> variable height
9+
</label>
710
<span>
811
<input v-model="buffer" type="number" min="1" max="500000" /> buffer
912
</span>
@@ -21,7 +24,7 @@
2124
<div class="content" v-if="showScroller">
2225
<div class="wrapper">
2326
<!-- Scoped slots -->
24-
<virtual-scroller v-if="scopedSlots" class="scroller" :items="items" main-tag="section" content-tag="table" :buffer="buffer" :pool-size="poolSize">
27+
<virtual-scroller v-if="scopedSlots" class="scroller" :item-height="itemHeight" :items="items" main-tag="section" content-tag="table" :buffer="buffer" :pool-size="poolSize">
2528
<template scope="props">
2629
<!-- <letter v-if="props.item.type === 'letter'" :item="props.item"></letter>-->
2730
<tr v-if="props.item.type === 'letter'" class="letter" :key="props.itemKey">
@@ -37,7 +40,7 @@
3740
</virtual-scroller>
3841

3942
<!-- Renderers -->
40-
<virtual-scroller v-else class="scroller" :items="items" :renderers="renderers" type-field="type" key-field="index" main-tag="section" content-tag="table"></virtual-scroller>
43+
<virtual-scroller v-else class="scroller" :item-height="itemHeight" :items="items" :renderers="renderers" type-field="type" key-field="index" main-tag="section" content-tag="table"></virtual-scroller>
4144
</div>
4245
</div>
4346
</div>
@@ -70,12 +73,16 @@ export default {
7073
scopedSlots: false,
7174
buffer: 0,
7275
poolSize: 1,
76+
enableLetters: true,
7377
}),
7478
7579
watch: {
7680
count () {
7781
this.generateItems()
7882
},
83+
enableLetters () {
84+
this.generateItems()
85+
},
7986
},
8087
8188
computed: {
@@ -92,13 +99,17 @@ export default {
9299
this.count = val
93100
},
94101
},
102+
103+
itemHeight () {
104+
return this.enableLetters ? null : 42
105+
},
95106
},
96107
97108
methods: {
98109
generateItems () {
99110
console.log('Generating ' + this.count + ' items...')
100111
let time = Date.now()
101-
const items = Object.freeze(getData(this.count))
112+
const items = Object.freeze(getData(this.count, this.enableLetters))
102113
this._time = Date.now()
103114
this.generateTime = this._time - time
104115
console.log('Generated ' + items.length + ' in ' + this.generateTime + 'ms')
@@ -151,7 +162,7 @@ body {
151162
margin-bottom: 12px;
152163
}
153164
154-
.counter > span:not(:last-child) {
165+
.counter > *:not(:last-child) {
155166
margin-right: 24px;
156167
}
157168

docs-src/src/data.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import faker from 'faker'
22

3-
export function getData (count) {
3+
export function getData (count, letters) {
44
const raw = {}
55

66
const alphabet = 'abcdefghijklmnopqrstuvwxyz'.split('')
@@ -22,12 +22,14 @@ export function getData (count) {
2222

2323
for (const l of alphabet) {
2424
raw[l] = raw[l].sort((a, b) => a.name < b.name ? -1 : 1)
25-
data.push({
26-
index: index++,
27-
type: 'letter',
28-
value: l,
29-
height: 200,
30-
})
25+
if (letters) {
26+
data.push({
27+
index: index++,
28+
type: 'letter',
29+
value: l,
30+
height: 200,
31+
})
32+
}
3133
for (var item of raw[l]) {
3234
data.push({
3335
index: index++,

docs/build.js

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/build.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "vue-virtual-scroller",
33
"description": "Smooth scrolling for any amount of data",
4-
"version": "0.7.1",
4+
"version": "0.7.3",
55
"author": {
66
"name": "Guillaume Chau",
77
"email": "[email protected]"

src/components/VirtualScroller.vue

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -163,13 +163,15 @@ export default {
163163
updateVisibleItems () {
164164
const l = this.items.length
165165
const scroll = this.getScroll()
166+
const items = this.items
167+
const itemHeight = this.itemHeight
166168
let containerHeight, offsetTop
167169
if (scroll) {
168170
let startIndex = -1
169171
let endIndex = -1
170172
171173
// Variable height mode
172-
if (this.itemHeight === null) {
174+
if (itemHeight === null) {
173175
const heights = this.heights
174176
let h
175177
let a = 0
@@ -197,16 +199,16 @@ export default {
197199
// Searching for endIndex
198200
for (endIndex = i; endIndex < l && heights[endIndex] < scroll.bottom; endIndex++);
199201
if (endIndex === -1) {
200-
endIndex = this.items.length - 1
202+
endIndex = items.length - 1
201203
} else {
202204
endIndex++
203205
}
204206
} else {
205207
// Fixed height mode
206-
startIndex = Math.floor((Math.floor(scroll.top / this.itemHeight) - this.buffer) / this.poolSize) * this.poolSize
207-
endIndex = Math.floor((Math.ceil(scroll.bottom / this.itemHeight) + this.buffer) / this.poolSize) * this.poolSize
208-
containerHeight = l * this.itemHeight
209-
offsetTop = startIndex * this.itemHeight
208+
const buffer = this.buffer
209+
const poolSize = this.poolSize
210+
startIndex = ~~((~~(scroll.top / itemHeight) - buffer) / poolSize) * poolSize
211+
endIndex = ~~((Math.ceil(scroll.bottom / itemHeight) + buffer) / poolSize) * poolSize
210212
}
211213
212214
if (startIndex < 0) {
@@ -216,11 +218,17 @@ export default {
216218
endIndex = l
217219
}
218220
221+
if (itemHeight !== null) {
222+
// Fixed height mode
223+
offsetTop = startIndex * itemHeight
224+
containerHeight = l * itemHeight
225+
}
226+
219227
if (startIndex !== this._startIndex || endIndex !== this._endIndex) {
220228
this.keysEnabled = !(startIndex > this._endIndex || endIndex < this._startIndex)
221229
this._startIndex = startIndex
222230
this._endIndex = endIndex
223-
this.visibleItems = this.items.slice(startIndex, endIndex)
231+
this.visibleItems = items.slice(startIndex, endIndex)
224232
this.itemContainerStyle = {
225233
height: containerHeight + 'px',
226234
}

0 commit comments

Comments
 (0)