Ver código fonte

[IMP] searcher features

Gogs 7 anos atrás
pai
commit
6894ed46bd

+ 39 - 1
src/components/common/Searcher.vue

@@ -52,10 +52,18 @@
                 type: Array,
                 default: [],
                 required: true
+            },
+            mode: {
+                type: String,
+                default: 'fuzzy'
             }
         },
         watch: {
             items(values) {
+                if (this.mode !== 'fuzzy') {
+                    return
+                }
+
                 this.fuse.setCollection(values)
             },
             search(value) {
@@ -78,7 +86,33 @@
                 })
             },
             performSearch(value) {
-                this.results = this.fuse.search(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() {
@@ -89,6 +123,10 @@
             }
         },
         mounted() {
+            if (this.mode !== 'fuzzy') {
+                return
+            }
+            
             this.initFuse()
         }
     }

+ 1 - 1
src/components/steps/Product.vue

@@ -1,7 +1,7 @@
 <template lang="pug">
     .pos-step
         .products-selector
-            searcher(:items='products' :keys="['name', 'displayName', 'ean13']" @onSearch='filterProducts')
+            searcher(:items='products' :keys="['name', 'displayName', 'ean13']" mode='normal' @onSearch='filterProducts')
             card-grid(:items='visibleProducts' :loading='loadingProducts' @onAdd='showProductForm' @onSelect='selectProduct')
             product-modal(:show='showingProductForm' @onAccept='submitProduct' @onCancel='hideProductForm')
             variant-modal