Skip to content

Commit 3134c42

Browse files
committed
keyField and better doc
1 parent 567565a commit 3134c42

File tree

2 files changed

+37
-3
lines changed

2 files changed

+37
-3
lines changed

README.md

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
Smooth scroll with any amount of data ([demo](https://akryum.github.io/vue-virtual-scroller/)).
77

8+
# Installation
9+
810
```
911
npm install --save vue-virtual-scroller
1012
```
@@ -25,10 +27,30 @@ import VirtualScroller from 'vue-virtual-scroller'
2527
Vue.use(VirtualScroller)
2628
```
2729

30+
# Usage
31+
32+
The virtual scroller has three required props:
33+
34+
- `items` is the list of items you want to display in the scroller. There can be several types of item.
35+
- `renderers` is a map of component definitions objects or names for each item type.
36+
- `itemHeight` is the display height of the items in pixels used to calculate the scroll height and position.
37+
38+
There are additional props you can use:
39+
40+
- `typeField` to customize which field is used on the items to get their type and use the corresponding definition in the `renderers` map. The default is `'type'`.
41+
- `keyField` to customize which field is used on the items to set their `key` special attribute (see [the documation](https://vuejs.org/v2/api/#key)). The default is `'id'`.
42+
43+
The `renderers` map is an object containing a component definition for each possible value of the item type. **The component definition must have an `item` prop, that will get the item object to render in the scroller.**
44+
45+
Also, you need to set the size of the virtual-scroller element and the items elements (for example, with CSS).
46+
47+
# Example
48+
2849
```html
2950
<template>
3051
<div class="demo">
3152
<virtual-scroller
53+
class="scroller"
3254
:items="items"
3355
:renderers="renderers"
3456
item-height="22"
@@ -61,9 +83,17 @@ export default {
6183
}),
6284
}
6385
</script>
64-
```
6586

66-
Finally, set the size of the virtual-scroller element (for example, with CSS).
87+
<style>
88+
.scroller {
89+
height: 300px;
90+
}
91+
92+
.scroller .item {
93+
height: 22px;
94+
}
95+
</style>
96+
```
6797

6898
---
6999

src/components/VirtualScroller.vue

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<div class="virtual-scroller" @scroll="updateVisibleItems">
33
<div class="item-container" :style="itemContainerStyle">
44
<div class="items">
5-
<component class="item" v-for="item in visibleItems" :key="item.index" :is="renderers[item[typeField]]" :item="item"></component>
5+
<component class="item" v-for="item in visibleItems" :key="item[keyField]" :is="renderers[item[typeField]]" :item="item"></component>
66
</div>
77
</div>
88

@@ -29,6 +29,10 @@ export default {
2929
type: String,
3030
default: 'type',
3131
},
32+
keyField: {
33+
type: String,
34+
default: 'id',
35+
},
3236
},
3337
data: () => ({
3438
visibleItems: [],

0 commit comments

Comments
 (0)