123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143 |
- <template lang="pug">
- input.searcher(type='search' :placeholder='placeholder' :style='{ width, height }' v-model='search')
- </template>
- <script>
- import Fuse from 'fuse.js'
- export default {
- props: {
- items: {
- type: Array,
- default: [],
- required: true
- },
- placeholder: {
- type: String,
- default: 'Buscar...'
- },
- width: {
- type: String,
- default: '100%'
- },
- height: {
- type: String,
- default: '35px'
- },
- shouldSort: {
- type: Boolean,
- default: true
- },
- threshold: {
- type: Number,
- default: 0.4,
- },
- location: {
- type: Number,
- default: 0
- },
- distance: {
- type: Number,
- default: 100
- },
- maxPatternLength: {
- type: Number,
- default: 32
- },
- minMatchCharLength: {
- type: Number,
- default: 1
- },
- keys: {
- type: Array,
- default: [],
- required: true
- },
- mode: {
- type: String,
- default: 'fuzzy'
- }
- },
- watch: {
- items(values) {
- if (this.mode !== 'fuzzy') {
- return
- }
- this.fuse.setCollection(values)
- },
- search(value) {
- this.performSearch(value.trim())
- },
- results(values) {
- this.$emit('onSearch', values)
- }
- },
- methods: {
- initFuse() {
- this.fuse = new Fuse(this.items, {
- shouldSort: this.shouldSort,
- threshold: this.threshold,
- location: this.location,
- distance: this.distance,
- maxPatternLength: this.maxPatternLength,
- minMatchCharLength: this.minMatchCharLength,
- keys: this.keys
- })
- },
- performSearch(value) {
- if(!value) {
- this.results = []
- return
- }
- if (this.mode === 'fuzzy') {
- this.results = this.fuse.search(value)
- } else {
- this.results = []
- for (let item of this.items) {
- for (let field in item) {
- if (typeof item[field] !== 'string') {
- continue
- }
- if (this.keys.length !== 0 && this.keys.indexOf(field) === -1) {
- continue
- }
- if (item[field].toLowerCase().indexOf(value.toLowerCase()) !== -1) {
- this.results.push(item)
- break
- }
- }
- }
- }
- }
- },
- data() {
- return {
- fuse: null,
- search: '',
- results: []
- }
- },
- mounted() {
- if (this.mode !== 'fuzzy') {
- return
- }
-
- this.initFuse()
- }
- }
- </script>
- <style lang="sass">
- .searcher
- text-align: center
- border-radius: 0 !important
- font:
- size: 11pt
- style: normal
- weight: bold
- </style>
|