1
0
mirror of https://github.com/PanJiaChen/vue-element-admin.git synced 2025-08-10 12:01:57 +08:00

Auto-Complete field support (#937)

Co-authored-by: elsiosanchez <elsiossanches@gmail.com>
This commit is contained in:
Elsio Sanchez 2021-06-22 18:18:11 -04:00 committed by GitHub
parent c027493ca9
commit 68872fea6a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -18,20 +18,37 @@
<template> <template>
<el-autocomplete <el-autocomplete
v-model="displayedValue" v-model="displayedValue"
:placeholder="placeholder" :placeholder="metadata.placeholder"
:fetch-suggestions="localSearch" :fetch-suggestions="localSearch"
:trigger-on-focus="true"
clearable clearable
value-key="name" value-key="name"
style="width: 100%;" style="width: 100%;"
popper-class="custom-field-bpartner-info" popper-class="custom-field-bpartner-info"
@focus="isFocus = true" @focus="isFocus = true"
@blur="isFocus = false" @blur="isFocus = false"
@select="handleSelect"
> >
<template
slot="prefix"
>
<i
:class="metadata.icon"
/>
</template>
<template slot="suffix"> <template slot="suffix">
<i <i
class="el-icon-arrow-down el-input__icon" class="el-icon-arrow-down el-input__icon"
/> />
</template> </template>
<template slot-scope="props">
<div class="header">
<b>{{ props.item.name }} </b>
</div>
<span class="info">
{{ props.item.value }}
</span>
</template>
</el-autocomplete> </el-autocomplete>
</template> </template>
@ -52,6 +69,7 @@ export default {
} }
return { return {
recordsBusinessPartners: [],
controlDisplayed: this.displayedValue, controlDisplayed: this.displayedValue,
isFocus: false, isFocus: false,
isLoading: false, isLoading: false,
@ -191,80 +209,69 @@ export default {
} }
}, },
localSearch(stringToMatch, callBack) { localSearch(stringToMatch, callBack) {
if (this.isEmptyValue(stringToMatch)) { const localListSearch = this.metadata.loadAll
// not show list const results = stringToMatch ? localListSearch.filter(this.createFilter(stringToMatch)) : localListSearch
callBack([]) // 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 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) callBack(results)
}, },
createFilter(stringToMatch) {
return (find) => {
return (find.name.toLowerCase().indexOf(stringToMatch.toLowerCase()) === 0)
}
},
remoteSearch(searchValue) { remoteSearch(searchValue) {
return new Promise(resolve => { return new Promise(resolve => {
const message = { const message = {
message: 'Sin resultados coincidentes con la busqueda', message: this.$t('notifications.searchWithOutRecords'),
type: 'info', type: 'info',
showClose: true showClose: true
} }
this.$store.dispatch('getLookupListFromServer', { this.$store.dispatch(this.metadata.searchServer, {
parentUuid: this.metadata.parentUuid, pageNumber: 1,
containerUuid: this.metadata.containerUuid, searchValue
tableName: this.metadata.reference.tableName,
query: this.metadata.reference.query,
isAddBlankValue: true,
blankValue: this.blankOption.id,
valuesList: searchValue
}) })
.then(() => { .then((response) => {
const recordsList = this.getterLookupAll const recordsList = this.metadata.loadAll
if (this.isEmptyValue(recordsList)) { if (this.isEmptyValue(recordsList)) {
this.$message(message) this.$message(message)
} }
return response
resolve(recordsList)
}) })
.catch(error => { .catch(error => {
console.warn(error.message) console.warn(error.message)
this.$message(message) this.$message(message)
resolve([]) return []
})
.finally(() => {
this.isLoading = false
}) })
}) })
},
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
})
} }
} }
} }