[update] 修改select2

This commit is contained in:
ibuler
2020-04-07 14:38:12 +08:00
parent 4f57fd3ba1
commit 8ce5f57a20
3 changed files with 77 additions and 34 deletions

View File

@@ -1,11 +1,11 @@
<template> <template>
<el-select <el-select
v-model="vaultOrDefault" v-model="valueSafe"
v-loadmore="loadMore" v-loadmore="loadMore"
:options="options" :options="optionsSafe"
:remote-method="filterOptions" :remote-method="filterOptions"
:loading="loading" :loading="loading"
multiple :multiple="multiple"
filterable filterable
remote remote
v-bind="$attrs" v-bind="$attrs"
@@ -13,11 +13,12 @@
v-on="$listeners" v-on="$listeners"
> >
<el-option <el-option
v-for="item in options" v-for="item in optionsSafe"
:key="item.value" :key="item.value"
:label="item.label" :label="item.label"
:value="item.value" :value="item.value"
/> >
</el-option>
</el-select> </el-select>
</template> </template>
@@ -37,7 +38,7 @@ export default {
* 如果元素滚动到底, 下面等式返回true, 没有则返回false: * 如果元素滚动到底, 下面等式返回true, 没有则返回false:
* ele.scrollHeight - ele.scrollTop === ele.clientHeight; * ele.scrollHeight - ele.scrollTop === ele.clientHeight;
*/ */
const condition = this.scrollHeight - this.scrollTop <= this.clientHeight const condition = this.scrollHeight - this.scrollTop - 30 <= this.clientHeight
if (condition) { if (condition) {
binding.value() binding.value()
} }
@@ -58,13 +59,25 @@ export default {
type: Function, type: Function,
default: null default: null
}, },
processSelected: {
type: Function,
default: null
},
options: { options: {
type: Array, type: Array,
default: () => [] default: () => ([])
}, },
value: { value: {
type: [Array, String, Number, Boolean], type: [Array, String, Number, Boolean],
default: () => [] default: () => ([])
},
initial: {
type: Array,
default: () => ([])
},
multiple: {
type: Boolean,
default: true
} }
}, },
data() { data() {
@@ -91,7 +104,13 @@ export default {
const more = !!data.next const more = !!data.next
return { results: results, pagination: more } return { results: results, pagination: more }
}, },
vaultOrDefault: this.value || [] defaultProcessSelected: data => {
return data.map(item => {
return { label: item.name, value: item.id }
})
},
valueSafe: [...this.value],
optionsSafe: this.options || []
} }
}, },
computed: { computed: {
@@ -101,11 +120,35 @@ export default {
processResultsOrDefault() { processResultsOrDefault() {
return this.processResults || this.defaultProcessResults return this.processResults || this.defaultProcessResults
}, },
optionsOrDefault() { processSelectedOrDefault() {
return this.options || [] return this.processSelected || this.defaultProcessSelected
},
initialOptions() {
const options = this.processSelectedOrDefault(this.initial)
return options
},
optionsValues() {
return this.optionsSafe.map((v) => v.value)
},
initialValues() {
return this.initialOptions.map(v => v.value)
}
},
watch: {
initialOptions: (newValue) => {
const notInclude = newValue.filter(v => {
return this.optionsValues.indexOf(v.value) === -1
})
this.optionsSafe = [...notInclude, ...this.optionsSafe]
const notIncludeValues = notInclude.map(v => v.value)
this.valueSafe = [...notIncludeValues, ...this.valueSafe]
} }
}, },
mounted() { mounted() {
if (this.initialOptions) {
this.valueSafe = [...this.initialValues, ...this.valueSafe]
this.optionsSafe = [...this.initialOptions, ...this.optionsSafe]
}
if (this.url) { if (this.url) {
this.getOptions() this.getOptions()
} }
@@ -128,14 +171,13 @@ export default {
}, },
filterOptions(query) { filterOptions(query) {
this.resetParams() this.resetParams()
this.options = [] this.optionsSafe = []
this.params.search = query this.params.search = query
this.getOptions() this.getOptions()
}, },
getOptions() { getOptions() {
this.loading = true this.loading = true
const params = this.makeParamsOrDefault(this.params) const params = this.makeParamsOrDefault(this.params)
console.log(params)
this.$axios.get(this.url, { params: params }).then(resp => { this.$axios.get(this.url, { params: params }).then(resp => {
this.loading = false this.loading = false
const data = this.processResultsOrDefault(resp) const data = this.processResultsOrDefault(resp)
@@ -143,11 +185,12 @@ export default {
this.params.hasMore = false this.params.hasMore = false
} }
data.results.forEach((v) => { data.results.forEach((v) => {
this.options.push(v) if (this.optionsValues.indexOf(v.value) === -1) {
this.optionsSafe.push(v)
}
}) })
}).catch(err => { }).catch(err => {
console.log(err) console.log(err)
this.options = []
}).then(() => { }).then(() => {
this.loading = false this.loading = false
}) })

View File

@@ -8,7 +8,7 @@
<FormGroupHeader slot="id:phone" title="认证" :line="true" /> <FormGroupHeader slot="id:phone" title="认证" :line="true" />
</DataForm> </DataForm>
</IBox> </IBox>
</page> </Page>
</template> </template>
<script> <script>

View File

@@ -12,7 +12,7 @@
<span>组下用户</span> <span>组下用户</span>
</div> </div>
<div> <div>
<Select2 v-model="value" :url="url" /> <Select2 v-model="select2.value" v-bind="select2" />
</div> </div>
</el-card> </el-card>
</el-col> </el-col>
@@ -52,23 +52,23 @@ export default {
} }
], ],
cardTitle: '基本信息', cardTitle: '基本信息',
placeholder: '请选择', select2: {
url: '/api/v1/users/users/', url: '/api/v1/users/users/',
value: [ initial: [
{ {
label: 'hello', name: 'hello',
value: '1a775bbf-6861-4acb-8ae4-2f684794c8cc' id: '1a775bbf-6861-4acb-8ae4-2f684794c8cc'
}, },
{ {
label: 'test', name: 'test',
value: '4dccdf84-7728-4de0-a507-67c905b3091b' id: '4dccdf84-7728-4de0-a507-67c905b3091b'
}, },
{ {
label: 'whold', name: 'whold',
value: 'c5ec4b91-1fb2-478e-89bc-5a4abc0f9c6c' id: 'c5ec4b91-1fb2-478e-89bc-5a4abc0f9c6c'
} }
], ]
options: [] },
} }
}, },
computed: { computed: {