Skip to content

Commit dcf0fef

Browse files
committed
RecycleList: new keyField prop
1 parent 8e40bc2 commit dcf0fef

File tree

1 file changed

+24
-10
lines changed

1 file changed

+24
-10
lines changed

src/components/RecycleList.vue

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ export default {
4949
type: Number,
5050
default: null,
5151
},
52+
keyField: {
53+
type: String,
54+
default: null,
55+
},
5256
},
5357
5458
data () {
@@ -104,7 +108,7 @@ export default {
104108
},
105109
106110
methods: {
107-
addView (index, item) {
111+
addView (pool, index, item, key, type) {
108112
const view = {
109113
item,
110114
top: 0,
@@ -113,18 +117,20 @@ export default {
113117
id: uid++,
114118
index,
115119
used: true,
120+
key,
121+
type,
116122
}
117123
Object.defineProperty(view, 'nr', {
118124
configurable: false,
119125
value: nonReactive,
120126
})
121-
this.pool.push(view)
127+
pool.push(view)
122128
return view
123129
},
124130
125131
unuseView (view, fake = false) {
126132
const unusedViews = this.$_unusedViews
127-
const type = view.item[this.typeField]
133+
const type = view.nr.type
128134
let unusedPool = unusedViews.get(type)
129135
if (!unusedPool) {
130136
unusedPool = []
@@ -134,7 +140,7 @@ export default {
134140
if (!fake) {
135141
view.nr.used = false
136142
view.top = -9999
137-
this.$_views.delete(view.item)
143+
this.$_views.delete(view.nr.key)
138144
}
139145
},
140146
@@ -183,6 +189,7 @@ export default {
183189
184190
const itemHeight = this.itemHeight
185191
const typeField = this.typeField
192+
const keyField = this.keyField
186193
const items = this.items
187194
const count = items.length
188195
const heights = this.heights
@@ -246,7 +253,7 @@ export default {
246253
247254
let view
248255
249-
const continuous = startIndex < this.$_endIndex && endIndex > this.$_startIndex
256+
const continuous = startIndex <= this.$_endIndex && endIndex >= this.$_startIndex
250257
let unusedIndex
251258
252259
if (this.$_continuous !== continuous) {
@@ -264,7 +271,9 @@ export default {
264271
view = pool[i]
265272
if (view.nr.used) {
266273
// Update view item index
267-
if (checkItem) view.nr.index = items.indexOf(view.item)
274+
if (checkItem) view.nr.index = items.findIndex(
275+
item => keyField ? item[keyField] == view.item[keyField] : item === view.item
276+
)
268277
269278
// Check if index is still in visible range
270279
if (
@@ -286,7 +295,8 @@ export default {
286295
let v
287296
for (let i = startIndex; i < endIndex; i++) {
288297
item = items[i]
289-
view = views.get(item)
298+
const key = keyField ? item[keyField] : item
299+
view = views.get(key)
290300
291301
if (!itemHeight && !heights[i].height) {
292302
if (view) this.unuseView(view)
@@ -305,8 +315,10 @@ export default {
305315
view.item = item
306316
view.nr.used = true
307317
view.nr.index = i
318+
view.nr.key = key
319+
view.nr.type = type
308320
} else {
309-
view = this.addView(i, item, type)
321+
view = this.addView(pool, i, item, key, type)
310322
}
311323
} else {
312324
unusedPool = unusedViews.get(type)
@@ -319,14 +331,16 @@ export default {
319331
view.item = item
320332
view.nr.used = true
321333
view.nr.index = i
334+
view.nr.key = key
335+
view.nr.type = type
322336
unusedIndex.set(type, v + 1)
323337
} else {
324-
view = this.addView(i, item, type)
338+
view = this.addView(pool, i, item, key, type)
325339
this.unuseView(view, true)
326340
}
327341
v++
328342
}
329-
views.set(item, view)
343+
views.set(key, view)
330344
} else {
331345
view.nr.used = true
332346
}

0 commit comments

Comments
 (0)