mirror of
https://github.com/tenrok/vue-select.git
synced 2026-06-19 09:50:33 +03:00
docs(filtering): Add Filtering Docs (#1017)
docs(infinite-scroll): improve example (#1017)
This commit is contained in:
@@ -1,9 +1,10 @@
|
|||||||
<template>
|
<template>
|
||||||
<div>
|
<div>
|
||||||
<v-select
|
<v-select
|
||||||
|
placeholder="choose a country"
|
||||||
v-model="selected"
|
v-model="selected"
|
||||||
:options="['Canada', 'United States']"
|
|
||||||
:components="{Deselect}"
|
:components="{Deselect}"
|
||||||
|
:options="['Canada', 'United States']"
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
|
|||||||
@@ -0,0 +1,31 @@
|
|||||||
|
<template>
|
||||||
|
<v-select :filter="fuseSearch" :options="books" :getOptionLabel="option => option.title">
|
||||||
|
<template #option="{author, title}">
|
||||||
|
{{ title }} <br>
|
||||||
|
<cite>{{ author.firstName }} {{ author.lastName }}</cite>
|
||||||
|
</template>
|
||||||
|
</v-select>
|
||||||
|
</template>
|
||||||
|
|
||||||
|
<script>
|
||||||
|
import Fuse from 'fuse.js';
|
||||||
|
import books from '../data/books';
|
||||||
|
|
||||||
|
export default {
|
||||||
|
computed: {
|
||||||
|
books: () => books,
|
||||||
|
},
|
||||||
|
methods: {
|
||||||
|
fuseSearch (options, search) {
|
||||||
|
const fuse = new Fuse(options, {
|
||||||
|
keys: ['title', 'author.firstName', 'author.lastName'],
|
||||||
|
shouldSort: true,
|
||||||
|
});
|
||||||
|
return search.length ? fuse.search(search) : fuse.list;
|
||||||
|
},
|
||||||
|
},
|
||||||
|
};
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<style scoped>
|
||||||
|
</style>
|
||||||
@@ -5,7 +5,6 @@
|
|||||||
@open="onOpen"
|
@open="onOpen"
|
||||||
@close="onClose"
|
@close="onClose"
|
||||||
@search="query => search = query"
|
@search="query => search = query"
|
||||||
ref="select"
|
|
||||||
>
|
>
|
||||||
<template #list-footer v-if="hasNextPage">
|
<template #list-footer v-if="hasNextPage">
|
||||||
<li ref="load" class="loader">
|
<li ref="load" class="loader">
|
||||||
@@ -17,16 +16,16 @@
|
|||||||
|
|
||||||
<script>
|
<script>
|
||||||
import countries from '../data/countries';
|
import countries from '../data/countries';
|
||||||
import vSelect from '../../../src/components/Select'
|
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
name: "InfiniteScroll",
|
name: "InfiniteScroll",
|
||||||
components: {vSelect},
|
data () {
|
||||||
data: () => ({
|
return {
|
||||||
observer: null,
|
observer: new IntersectionObserver(this.infiniteScroll),
|
||||||
limit: 10,
|
limit: 10,
|
||||||
search: ''
|
search: ''
|
||||||
}),
|
}
|
||||||
|
},
|
||||||
computed: {
|
computed: {
|
||||||
filtered () {
|
filtered () {
|
||||||
return countries.filter(country => country.includes(this.search));
|
return countries.filter(country => country.includes(this.search));
|
||||||
@@ -35,12 +34,9 @@ export default {
|
|||||||
return this.filtered.slice(0, this.limit);
|
return this.filtered.slice(0, this.limit);
|
||||||
},
|
},
|
||||||
hasNextPage () {
|
hasNextPage () {
|
||||||
return this.paginated.length + 10 < this.filtered.length;
|
return this.paginated.length < this.filtered.length;
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
mounted () {
|
|
||||||
this.observer = new IntersectionObserver(this.infiniteScroll);
|
|
||||||
},
|
|
||||||
methods: {
|
methods: {
|
||||||
async onOpen () {
|
async onOpen () {
|
||||||
if (this.hasNextPage) {
|
if (this.hasNextPage) {
|
||||||
|
|||||||
@@ -226,11 +226,6 @@ export default {
|
|||||||
loading(false);
|
loading(false);
|
||||||
});
|
});
|
||||||
}, 250),
|
}, 250),
|
||||||
fuseSearch (options, search) {
|
|
||||||
return new Fuse(options, {
|
|
||||||
keys: ['title', 'author.firstName', 'author.lastName'],
|
|
||||||
}).search(search);
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
</script>
|
</script>
|
||||||
|
|||||||
@@ -131,7 +131,8 @@ module.exports = {
|
|||||||
collapsable: false,
|
collapsable: false,
|
||||||
children: [
|
children: [
|
||||||
['guide/keydown', 'Keydown Events'],
|
['guide/keydown', 'Keydown Events'],
|
||||||
['guide/positioning', 'Dropdown Position']
|
['guide/positioning', 'Dropdown Position'],
|
||||||
|
['guide/filtering', 'Option Filtering'],
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -0,0 +1,19 @@
|
|||||||
|
Vue Select provides two props accepting `functions` that can be used to implement custom filtering
|
||||||
|
algorithms.
|
||||||
|
|
||||||
|
- `filter` <Badge text="v2.5.0+" />
|
||||||
|
- `filterBy` <Badge text="v2.5.0+" />
|
||||||
|
|
||||||
|
By default, the component will perform a very basic check to see if an options label includes
|
||||||
|
the current search text. If you're using scoped slots, you might have information within the
|
||||||
|
option templates that should be searchable. Or maybe you just want a better search algorithm that
|
||||||
|
can do fuzzy search matching.
|
||||||
|
|
||||||
|
## Filtering with Fuse.js
|
||||||
|
|
||||||
|
You can use the `filter` and `filterBy` props to hook right into something like
|
||||||
|
[Fuse.js](https://fusejs.io/) that can handle searching multiple object keys with fuzzy matchings.
|
||||||
|
|
||||||
|
<FuseFilter />
|
||||||
|
|
||||||
|
<<< @/.vuepress/components/FuseFilter.vue
|
||||||
@@ -4,8 +4,8 @@ by hooking into the `open`, `close`, and `search` events, along with the `filter
|
|||||||
|
|
||||||
Let's break down the example below, starting with the `data`.
|
Let's break down the example below, starting with the `data`.
|
||||||
|
|
||||||
- `observer` - when the component is mounted, a new `IntersectionObserver` will be set here
|
- `observer` - a new `IntersectionObserver` with `infiniteScroll` set as the callback
|
||||||
- `limit` - the number of options to display 'per page'
|
- `limit` - the number of options to display
|
||||||
- `search` - since we've disabled Vue Selects filtering, we'll need to filter options ourselves
|
- `search` - since we've disabled Vue Selects filtering, we'll need to filter options ourselves
|
||||||
|
|
||||||
When Vue Select opens, the `open` event is emitted and `onOpen` will be called. We wait for
|
When Vue Select opens, the `open` event is emitted and `onOpen` will be called. We wait for
|
||||||
|
|||||||
Reference in New Issue
Block a user