Skip to content

Commit 62bc6ac

Browse files
committed
handle changes from zero to some items
1 parent 611afeb commit 62bc6ac

File tree

1 file changed

+53
-33
lines changed

1 file changed

+53
-33
lines changed

src/VirtualList.html

Lines changed: 53 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,8 @@
3434
},
3535

3636
oncreate() {
37-
const { items, _props, itemHeight } = this.get();
38-
const { viewport, container } = this.refs;
39-
const viewportHeight = viewport.offsetHeight;
37+
const { items, _props } = this.get();
38+
const { container } = this.refs;
4039

4140
const keys = Object.keys(this.options.data).filter(key => key !== 'items' && key !== 'component' && key !== 'height' && key !== 'itemHeight');
4241
if (keys.length) {
@@ -47,45 +46,31 @@
4746
this.set({ _props });
4847
}
4948

49+
this.heightMap = [];
5050
this.rows = container.getElementsByClassName('row');
5151

52-
if (itemHeight) {
53-
this.heightMap = items.map(item => itemHeight);
54-
this.set({
55-
end: Math.min(items.length, Math.ceil(viewportHeight / itemHeight)),
56-
bottom: items.length * itemHeight
57-
});
58-
} else {
59-
this.heightMap = [];
60-
61-
let height = 0;
62-
let i = 0;
63-
64-
while (height < viewportHeight && i < items.length) {
65-
this.set({ end: i + 1 });
66-
67-
const rowHeight = this.heightMap[i] = this.rows[i].offsetHeight;
68-
height += rowHeight;
69-
70-
i += 1;
71-
}
72-
73-
const end = i;
74-
const avg = Math.round(height / i);
75-
76-
for (; i < items.length; i += 1) this.heightMap[i] = avg;
77-
78-
this.set({
79-
bottom: (items.length - end) * avg
80-
});
52+
if (items.length > 0) {
53+
this.initialise();
8154
}
8255

8356
this.on('state', ({ changed, previous, current }) => {
8457
if (changed.items || changed.height || changed.itemHeight) {
85-
if (current.itemHeight && (changed.itemHeight || current.items.length !== this.heightMap.length)) {
58+
if (current.itemHeight && (changed.itemHeight || current.items.length > this.heightMap.length)) {
8659
this.heightMap = current.items.map(() => current.itemHeight);
8760
}
8861

62+
else if (current.items.length > this.heightMap.length) {
63+
if (this.heightMap.length === 0) {
64+
this.initialise();
65+
} else {
66+
let height = 0;
67+
let i = 0;
68+
for (; i < this.heightMap.length; i += 1) height += this.heightMap[i];
69+
const avg = height / this.heightMap.length;
70+
for (; i < current.items.length; i += 1) this.heightMap[i] = avg;
71+
}
72+
}
73+
8974
this.refresh();
9075
}
9176

@@ -100,6 +85,41 @@
10085
},
10186

10287
methods: {
88+
initialise() {
89+
const { items, itemHeight } = this.get();
90+
const { viewport } = this.refs;
91+
const viewportHeight = viewport.offsetHeight;
92+
93+
if (itemHeight) {
94+
this.heightMap = items.map(item => itemHeight);
95+
this.set({
96+
end: Math.min(items.length, Math.ceil(viewportHeight / itemHeight)),
97+
bottom: items.length * itemHeight
98+
});
99+
} else {
100+
let height = 0;
101+
let i = 0;
102+
103+
while (height < viewportHeight && i < items.length) {
104+
this.set({ end: i + 1 });
105+
106+
const rowHeight = this.heightMap[i] = this.rows[i].offsetHeight;
107+
height += rowHeight;
108+
109+
i += 1;
110+
}
111+
112+
const end = i;
113+
const avg = Math.round(height / i);
114+
115+
for (; i < items.length; i += 1) this.heightMap[i] = avg;
116+
117+
this.set({
118+
bottom: (items.length - end) * avg
119+
});
120+
}
121+
},
122+
103123
refresh() {
104124
const { items, start, end, itemHeight } = this.get();
105125
const { offsetHeight, scrollTop } = this.refs.viewport;

0 commit comments

Comments
 (0)