Skip to content

Commit 9847ca7

Browse files
committed
Add reset methof
1 parent 4fb72fd commit 9847ca7

File tree

1 file changed

+25
-13
lines changed

1 file changed

+25
-13
lines changed

src/index.js

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,7 @@ const VirtualList = Vue.component(NAME, {
4040
this.isHorizontal = this.direction === 'horizontal'
4141
this.directionKey = this.isHorizontal ? 'scrollLeft' : 'scrollTop'
4242

43-
this.virtual = new Virtual({
44-
size: this.size, // also could be a estimate value
45-
slotHeaderSize: 0,
46-
slotFooterSize: 0,
47-
keeps: this.keeps,
48-
buffer: Math.round(this.keeps / 3), // recommend for a third of keeps
49-
uniqueIds: this.getUniqueIdFromDataSources()
50-
}, this.onRangeChanged)
51-
52-
// sync initial range
53-
this.range = this.virtual.getRange()
43+
this.installVirtual()
5444

5545
// listen item size changing
5646
this.$on(EVENT_TYPE.ITEM, this.onItemResized)
@@ -72,8 +62,6 @@ const VirtualList = Vue.component(NAME, {
7262
} else if (this.offset) {
7363
this.scrollToOffset(this.offset)
7464
}
75-
76-
window.virtual = this.virtual
7765
},
7866

7967
methods: {
@@ -116,8 +104,32 @@ const VirtualList = Vue.component(NAME, {
116104
}
117105
},
118106

107+
// reset all state back to initial
108+
reset () {
109+
this.virtual.destroy()
110+
this.scrollToOffset(0)
111+
this.installVirtual()
112+
},
113+
119114
// ----------- public method end -----------
120115

116+
installVirtual () {
117+
this.virtual = new Virtual({
118+
size: this.size, // also could be a estimate value
119+
slotHeaderSize: 0,
120+
slotFooterSize: 0,
121+
keeps: this.keeps,
122+
buffer: Math.round(this.keeps / 3), // recommend for a third of keeps
123+
uniqueIds: this.getUniqueIdFromDataSources()
124+
}, this.onRangeChanged)
125+
126+
// sync initial range
127+
this.range = this.virtual.getRange()
128+
129+
// just for debug
130+
// window.virtual = this.virtual
131+
},
132+
121133
getUniqueIdFromDataSources () {
122134
return this.dataSources.map((dataSource) => dataSource[this.dataKey])
123135
},

0 commit comments

Comments
 (0)