Skip to content

Commit b7b689c

Browse files
committed
- fix rendering when items number change
1 parent ea35a7c commit b7b689c

File tree

3 files changed

+38
-38
lines changed

3 files changed

+38
-38
lines changed

package-lock.json

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,6 +1,6 @@
11
{
22
"name": "vue-windowing",
3-
"version": "0.14.4",
3+
"version": "0.14.5",
44
"description": "Set of components used for virtualizing DOM",
55
"author": {
66
"name": "Maciej Kaczorowski",

src/components/VirtualScroll.vue

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
class="virtual-scroll__spacer">
1616
<AutoHeightMeasurer
1717
v-for="(item, index) in visibleItems"
18-
:key="`${item.id || item}`"
18+
:key="`${item.id || item} - ${index + startNode}`"
1919
:index="index + startNode"
2020
:height="cachedHeight[index + startNode] || estimatedHeight"
2121
@height="onMeasuredHeight">
@@ -79,7 +79,6 @@ export default {
7979
this.childPositions = {
8080
0: 0,
8181
};
82-
8382
this.cachedHeight = {};
8483
}
8584
},
@@ -123,9 +122,12 @@ export default {
123122
},
124123
},
125124
mounted() {
126-
requestAnimationFrame(() => {
127-
this.height = this.$el.offsetHeight;
125+
this.height = this.$el.offsetHeight;
128126
127+
this.initVisibleNodes();
128+
},
129+
methods: {
130+
initVisibleNodes() {
129131
const start = 0;
130132
const end = Math.ceil((this.height * 2) / this.estimatedHeight);
131133
@@ -135,29 +137,41 @@ export default {
135137
136138
this.firstVisibleNode = start;
137139
this.lastVisibleNode = end;
138-
});
139-
},
140-
methods: {
140+
},
141+
updateVisibleNodes() {
142+
const startNode = this.getFirstVisibleNode();
143+
const endNode = this.getLastVisibleNode(startNode);
144+
145+
const offsetStart = Math.max(0, startNode - this.renderAhead);
146+
147+
for (let i = offsetStart; i < endNode; i += 1) {
148+
const prevIndex = i - 1;
149+
150+
const prevPosition = typeof this.childPositions[prevIndex] === 'undefined'
151+
? prevIndex * this.estimatedHeight
152+
: this.childPositions[prevIndex];
153+
154+
const position = prevPosition + (this.cachedHeight[prevIndex] || this.estimatedHeight);
155+
156+
this.childPositions[i] = position;
157+
}
158+
159+
this.firstVisibleNode = startNode;
160+
this.lastVisibleNode = endNode;
161+
},
141162
onIntersect(isIntersecting) {
142163
if (isIntersecting
143164
&& this.firstVisibleNode !== 0
144165
&& this.scrollTop > 0
145166
&& this.scrollTop !== this.$refs.root.scrollTop) {
146167
window.requestAnimationFrame(() => {
168+
console.log('intersecting');
147169
this.$refs.root.scrollTo(0, this.scrollTop);
148170
});
149171
}
150172
},
151173
onResize(entry) {
152174
this.height = entry.contentRect.height;
153-
154-
if (this.height > 0 && this.rowCount > 0) {
155-
const startNode = this.getFirstVisibleNode();
156-
const endNode = this.getLastVisibleNode(startNode);
157-
158-
this.firstVisibleNode = startNode;
159-
this.lastVisibleNode = endNode;
160-
}
161175
},
162176
onMeasuredHeight({
163177
index,
@@ -175,34 +189,20 @@ export default {
175189
? prevIndex * this.estimatedHeight
176190
: this.childPositions[prevIndex];
177191
178-
const position = prevPosition + (this.cachedHeight[prevIndex] || this.estimatedHeight);
179-
180-
this.childPositions[i] = position;
192+
this.childPositions[i] = prevPosition + (
193+
this.cachedHeight[prevIndex] || this.estimatedHeight
194+
);
181195
}
182196
},
183197
onScroll() {
184198
window.requestAnimationFrame(() => {
185199
this.scrollTop = this.$refs.root.scrollTop;
186200
187-
const startNode = this.getFirstVisibleNode();
188-
const endNode = this.getLastVisibleNode(startNode);
189-
190-
const offsetStart = Math.max(0, startNode - this.renderAhead);
191-
192-
for (let i = offsetStart; i < endNode; i += 1) {
193-
const prevIndex = i - 1;
194-
195-
const prevPosition = typeof this.childPositions[prevIndex] === 'undefined'
196-
? prevIndex * this.estimatedHeight
197-
: this.childPositions[prevIndex];
198-
199-
const position = prevPosition + (this.cachedHeight[prevIndex] || this.estimatedHeight);
200-
201-
this.childPositions[i] = position;
201+
if (this.firstVisibleNode > this.rowCount) {
202+
this.initVisibleNodes();
203+
} else {
204+
this.updateVisibleNodes();
202205
}
203-
204-
this.firstVisibleNode = startNode;
205-
this.lastVisibleNode = endNode;
206206
});
207207
},
208208
getFirstVisibleNode() {

0 commit comments

Comments
 (0)