From 68872fea6a63ea23a5159184b61972014057f225 Mon Sep 17 00:00:00 2001 From: Elsio Sanchez <45974454+elsiosanchez@users.noreply.github.com> Date: Tue, 22 Jun 2021 18:18:11 -0400 Subject: [PATCH] Auto-Complete field support (#937) Co-authored-by: elsiosanchez --- .../ADempiere/Field/FieldAutocomplete.vue | 113 ++++++++++-------- 1 file changed, 60 insertions(+), 53 deletions(-) diff --git a/src/components/ADempiere/Field/FieldAutocomplete.vue b/src/components/ADempiere/Field/FieldAutocomplete.vue index 64e35e85..697370ad 100644 --- a/src/components/ADempiere/Field/FieldAutocomplete.vue +++ b/src/components/ADempiere/Field/FieldAutocomplete.vue @@ -18,20 +18,37 @@ @@ -52,6 +69,7 @@ export default { } return { + recordsBusinessPartners: [], controlDisplayed: this.displayedValue, isFocus: false, isLoading: false, @@ -191,80 +209,69 @@ export default { } }, localSearch(stringToMatch, callBack) { - if (this.isEmptyValue(stringToMatch)) { - // not show list - callBack([]) + const localListSearch = this.metadata.loadAll + const results = stringToMatch ? localListSearch.filter(this.createFilter(stringToMatch)) : localListSearch + // call callback function to return suggestions + if (this.isEmptyValue(results) && stringToMatch.length > 3) { + clearTimeout(this.timeOut) + this.timeOut = setTimeout(() => { + this.remoteSearch(stringToMatch) + .then(remoteResponse => { + callBack(remoteResponse) + }) + }, 3000) return } - - const recordsList = this.getterLookupList - let results = recordsList - if (stringToMatch || true) { - const parsedValue = stringToMatch.toLowerCase().trim() - results = recordsList.filter(rowBPartner => { - // columns: id, uuid, label - for (const columnBPartner in rowBPartner) { - const valueToCompare = String(rowBPartner[columnBPartner]).toLowerCase() - - if (valueToCompare.includes(parsedValue)) { - return true - } - } - return false - }) - - // Remote search - if (this.isEmptyValue(results)) { - clearTimeout(this.timeOut) - - this.timeOut = setTimeout(() => { - this.remoteSearch(stringToMatch) - .then(remoteResponse => { - callBack(remoteResponse) - }) - }, 2000) - return - } - } - - // call callback function to return suggestions callBack(results) }, + createFilter(stringToMatch) { + return (find) => { + return (find.name.toLowerCase().indexOf(stringToMatch.toLowerCase()) === 0) + } + }, remoteSearch(searchValue) { return new Promise(resolve => { const message = { - message: 'Sin resultados coincidentes con la busqueda', + message: this.$t('notifications.searchWithOutRecords'), type: 'info', showClose: true } - this.$store.dispatch('getLookupListFromServer', { - parentUuid: this.metadata.parentUuid, - containerUuid: this.metadata.containerUuid, - tableName: this.metadata.reference.tableName, - query: this.metadata.reference.query, - isAddBlankValue: true, - blankValue: this.blankOption.id, - valuesList: searchValue + this.$store.dispatch(this.metadata.searchServer, { + pageNumber: 1, + searchValue }) - .then(() => { - const recordsList = this.getterLookupAll + .then((response) => { + const recordsList = this.metadata.loadAll if (this.isEmptyValue(recordsList)) { this.$message(message) } - - resolve(recordsList) + return response }) .catch(error => { console.warn(error.message) this.$message(message) - resolve([]) - }) - .finally(() => { - this.isLoading = false + return [] }) }) + }, + handleSelect(item) { + this.$store.commit('updateValueOfField', { + containerUuid: this.metadata.containerUuid, + columnName: this.metadata.columnName, + value: item.id + }) + this.$store.commit('updateValueOfField', { + containerUuid: this.metadata.containerUuid, + columnName: this.metadata.displayColumnName, + value: item.name + }) + this.$store.commit('updateValueOfField', { + containerUuid: this.metadata.containerUuid, + columnName: this.metadata.columnName + '_UUID', + value: item.uuid + }) } } }