Skip to content

Commit 69b5317

Browse files
committed
Scoped slots
1 parent 16aae3c commit 69b5317

File tree

7 files changed

+68
-13
lines changed

7 files changed

+68
-13
lines changed

README.md

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,10 @@ Vue.component('virtual-scroller', VirtualScroller)
105105

106106
# Usage
107107

108-
The virtual scroller has three required props:
108+
The virtual scroller has three main props:
109109

110110
- `items` is the list of items you want to display in the scroller. There can be several types of item.
111-
- `renderers` is a map of component definitions objects or names for each item type.
111+
- `renderers` is a map of component definitions objects or names for each item type. If you don't define `renderers`, the scroller will use *scoped slots* ([see below](#scoped_slots)).
112112
- `itemHeight` is the display height of the items in pixels used to calculate the scroll height and position.
113113

114114
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.**
@@ -149,6 +149,32 @@ If you set `contentTag` to `'table'`, the actual result in the DOM will look lik
149149

150150
> The browsers have a height limitation on DOM elements, it means that currently the virtual scroller can't display more than ~500k items depending on the browser.
151151
152+
## Scoped slots
153+
154+
Alternatively, you can use [scoped slots](https://vuejs.org/v2/guide/components.html#Scoped-Slots) instead of `renderers`. This is active when you don't define the `renderers` prop on the virtual scroller.
155+
156+
The scope will contain the row's item in the `item` attribute, so you can write `scope="props"` and then use `props.item`.
157+
158+
Here is an example:
159+
160+
```html
161+
<virtual-scroller class="scroller" :items="items" item-height="42" content-tag="table">
162+
<template scope="props">
163+
<tr v-if="props.item.type === 'letter'" class="letter">
164+
<td>
165+
{{props.item.value}} Scoped
166+
</td>
167+
</tr>
168+
169+
<tr v-if="props.item.type === 'person'" class="person">
170+
<td>
171+
{{props.item.value.name}}
172+
</td>
173+
</tr>
174+
</template>
175+
</virtual-scroller>
176+
```
177+
152178
# Example
153179

154180
```html

dist/vue-virtual-scroller.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs-src/src/App.vue

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,29 @@
1212
</span>
1313
<span>
1414
<button @mousedown="showScroller = !showScroller">Toggle scroller</button>
15+
<label><input type="checkbox" v-model="scopedSlots" /> Scoped slots</label>
1516
</span>
1617
</div>
1718
<div class="content" v-if="showScroller">
1819
<div class="wrapper">
19-
<virtual-scroller class="scroller" :items="items" :renderers="renderers" item-height="42" type-field="type" key-field="index" main-tag="section" content-tag="table"></virtual-scroller>
20+
<!-- Scoped slots -->
21+
<virtual-scroller v-if="scopedSlots" class="scroller" :items="items" item-height="42" main-tag="section" content-tag="table">
22+
<template scope="props">
23+
<!-- <letter v-if="props.item.type === 'letter'" :item="props.item"></letter>-->
24+
<tr v-if="props.item.type === 'letter'" class="letter">
25+
<td class="index">
26+
{{props.item.index}}
27+
</td>
28+
<td>
29+
{{props.item.value}} Scoped
30+
</td>
31+
</tr>
32+
<item v-if="props.item.type === 'person'" :item="props.item"></item>
33+
</template>
34+
</virtual-scroller>
35+
36+
<!-- Renderers -->
37+
<virtual-scroller v-else class="scroller" :items="items" :renderers="renderers" item-height="42" type-field="type" key-field="index" main-tag="section" content-tag="table"></virtual-scroller>
2038
</div>
2139
</div>
2240
</div>
@@ -34,13 +52,19 @@ const renderers = {
3452
}
3553
3654
export default {
55+
components: {
56+
Letter,
57+
Item,
58+
},
59+
3760
data: () => ({
3861
items: [],
3962
renderers,
4063
count: 10000,
4164
generateTime: null,
4265
updateTime: null,
4366
showScroller: true,
67+
scopedSlots: false,
4468
}),
4569
4670
watch: {

docs/build.js

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/build.js.map

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,7 +1,7 @@
11
{
22
"name": "vue-virtual-scroller",
33
"description": "Smooth scrolling for any amount of data",
4-
"version": "0.2.1",
4+
"version": "0.3.0",
55
"author": {
66
"name": "Guillaume Chau",
77
"email": "[email protected]"

src/components/VirtualScroller.vue

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,12 @@
22
<component :is="mainTag" class="virtual-scroller" @scroll="updateVisibleItems">
33
<component :is="containerTag" class="item-container" :style="itemContainerStyle">
44
<component :is="contentTag" class="items" :style="itemsStyle">
5-
<component class="item" v-for="item in visibleItems" :key="item[keyField]" :is="renderers[item[typeField]]" :item="item"></component>
5+
<template v-if="renderers">
6+
<component class="item" v-for="item in visibleItems" :key="item[keyField]" :is="renderers[item[typeField]]" :item="item"></component>
7+
</template>
8+
<template v-else>
9+
<slot class="item" v-for="item in visibleItems" :item="item"></slot>
10+
</template>
611
</component>
712
</component>
813
<resize-observer @notify="updateVisibleItems" />
@@ -25,7 +30,7 @@ export default {
2530
required: true,
2631
},
2732
renderers: {
28-
required: true,
33+
default: null,
2934
},
3035
itemHeight: {
3136
type: [Number, String],

0 commit comments

Comments
 (0)