當選項過多時,使用下拉菜單展示并選擇內(nèi)容。
適用廣泛的基礎(chǔ)單選
v-model的值為當前被選中的el-option的 value 屬性值
<template>
<el-select v-model="value" placeholder="請選擇">
<el-option
v-for="item in options"
:key="item.value"
:label="item.label"
:value="item.value"
>
</el-option>
</el-select>
</template>
<script>
export default {
data() {
return {
options: [
{
value: '選項1',
label: '黃金糕',
},
{
value: '選項2',
label: '雙皮奶',
},
{
value: '選項3',
label: '蚵仔煎',
},
{
value: '選項4',
label: '龍須面',
},
{
value: '選項5',
label: '北京烤鴨',
},
],
value: '',
}
},
}
</script>
在el-option中,設(shè)定disabled值為 true,即可禁用該選項
<template>
<el-select v-model="value" placeholder="請選擇">
<el-option
v-for="item in options"
:key="item.value"
:label="item.label"
:value="item.value"
:disabled="item.disabled"
>
</el-option>
</el-select>
</template>
<script>
export default {
data() {
return {
options: [
{
value: '選項1',
label: '黃金糕',
},
{
value: '選項2',
label: '雙皮奶',
disabled: true,
},
{
value: '選項3',
label: '蚵仔煎',
},
{
value: '選項4',
label: '龍須面',
},
{
value: '選項5',
label: '北京烤鴨',
},
],
value: '',
}
},
}
</script>
選擇器不可用狀態(tài)
為el-select設(shè)置disabled屬性,則整個選擇器不可用
<template>
<el-select v-model="value" disabled placeholder="請選擇">
<el-option
v-for="item in options"
:key="item.value"
:label="item.label"
:value="item.value"
>
</el-option>
</el-select>
</template>
<script>
export default {
data() {
return {
options: [
{
value: '選項1',
label: '黃金糕',
},
{
value: '選項2',
label: '雙皮奶',
},
{
value: '選項3',
label: '蚵仔煎',
},
{
value: '選項4',
label: '龍須面',
},
{
value: '選項5',
label: '北京烤鴨',
},
],
value: '',
}
},
}
</script>
包含清空按鈕,可將選擇器清空為初始狀態(tài)
為el-select設(shè)置clearable屬性,則可將選擇器清空。需要注意的是,clearable屬性僅適用于單選。
<template>
<el-select v-model="value" clearable placeholder="請選擇">
<el-option
v-for="item in options"
:key="item.value"
:label="item.label"
:value="item.value"
>
</el-option>
</el-select>
</template>
<script>
export default {
data() {
return {
options: [
{
value: '選項1',
label: '黃金糕',
},
{
value: '選項2',
label: '雙皮奶',
},
{
value: '選項3',
label: '蚵仔煎',
},
{
value: '選項4',
label: '龍須面',
},
{
value: '選項5',
label: '北京烤鴨',
},
],
value: '',
}
},
}
</script>
適用性較廣的基礎(chǔ)多選,用 Tag 展示已選項
為el-select設(shè)置multiple屬性即可啟用多選,此時v-model的值為當前選中值所組成的數(shù)組。默認情況下選中值會以 Tag 的形式展現(xiàn),你也可以設(shè)置collapse-tags屬性將它們合并為一段文字。
<template>
<el-select v-model="value1" multiple placeholder="請選擇">
<el-option
v-for="item in options"
:key="item.value"
:label="item.label"
:value="item.value"
>
</el-option>
</el-select>
<el-select
v-model="value2"
multiple
collapse-tags
style="margin-left: 20px;"
placeholder="請選擇"
>
<el-option
v-for="item in options"
:key="item.value"
:label="item.label"
:value="item.value"
>
</el-option>
</el-select>
</template>
<script>
export default {
data() {
return {
options: [
{
value: '選項1',
label: '黃金糕',
},
{
value: '選項2',
label: '雙皮奶',
},
{
value: '選項3',
label: '蚵仔煎',
},
{
value: '選項4',
label: '龍須面',
},
{
value: '選項5',
label: '北京烤鴨',
},
],
value1: [],
value2: [],
}
},
}
</script>
可以自定義備選項
將自定義的 HTML 模板插入el-option的 slot 中即可。
<template>
<el-select v-model="value" placeholder="請選擇">
<el-option
v-for="item in cities"
:key="item.value"
:label="item.label"
:value="item.value"
>
<span style="float: left">{{ item.label }}</span>
<span
style="float: right; color: var(--el-text-color-secondary); font-size: 13px"
>{{ item.value }}</span
>
</el-option>
</el-select>
</template>
<script>
export default {
data() {
return {
cities: [
{
value: 'Beijing',
label: '北京',
},
{
value: 'Shanghai',
label: '上海',
},
{
value: 'Nanjing',
label: '南京',
},
{
value: 'Chengdu',
label: '成都',
},
{
value: 'Shenzhen',
label: '深圳',
},
{
value: 'Guangzhou',
label: '廣州',
},
],
value: '',
}
},
}
</script>
備選項進行分組展示
使用?
el-option-group
?對備選項進行分組,它的?label
?屬性為分組名
<template>
<el-select v-model="value" placeholder="請選擇">
<el-option-group
v-for="group in options"
:key="group.label"
:label="group.label"
>
<el-option
v-for="item in group.options"
:key="item.value"
:label="item.label"
:value="item.value"
>
</el-option>
</el-option-group>
</el-select>
</template>
<script>
export default {
data() {
return {
options: [
{
label: '熱門城市',
options: [
{
value: 'Shanghai',
label: '上海',
},
{
value: 'Beijing',
label: '北京',
},
],
},
{
label: '城市名',
options: [
{
value: 'Chengdu',
label: '成都',
},
{
value: 'Shenzhen',
label: '深圳',
},
{
value: 'Guangzhou',
label: '廣州',
},
{
value: 'Dalian',
label: '大連',
},
],
},
],
value: '',
}
},
}
</script>
可以利用搜索功能快速查找選項
為el-select添加filterable屬性即可啟用搜索功能。默認情況下,Select 會找出所有l(wèi)abel屬性包含輸入值的選項。如果希望使用其他的搜索邏輯,可以通過傳入一個filter-method來實現(xiàn)。filter-method為一個Function,它會在輸入值發(fā)生變化時調(diào)用,參數(shù)為當前輸入值。
<template>
<el-select v-model="value" filterable placeholder="請選擇">
<el-option
v-for="item in options"
:key="item.value"
:label="item.label"
:value="item.value"
>
</el-option>
</el-select>
</template>
<script>
export default {
data() {
return {
options: [
{
value: '選項1',
label: '黃金糕',
},
{
value: '選項2',
label: '雙皮奶',
},
{
value: '選項3',
label: '蚵仔煎',
},
{
value: '選項4',
label: '龍須面',
},
{
value: '選項5',
label: '北京烤鴨',
},
],
value: '',
}
},
}
</script>
從服務器搜索數(shù)據(jù),輸入關(guān)鍵字進行查找
為了啟用遠程搜索,需要將filterable和remote設(shè)置為true,同時傳入一個remote-method。remote-method為一個Function,它會在輸入值發(fā)生變化時調(diào)用,參數(shù)為當前輸入值。需要注意的是,如果el-option是通過v-for指令渲染出來的,此時需要為el-option添加key屬性,且其值需具有唯一性,比如此例中的item.value。
<template>
<el-select
v-model="value"
multiple
filterable
remote
reserve-keyword
placeholder="請輸入關(guān)鍵詞"
:remote-method="remoteMethod"
:loading="loading"
>
<el-option
v-for="item in options"
:key="item.value"
:label="item.label"
:value="item.value"
>
</el-option>
</el-select>
</template>
<script>
export default {
data() {
return {
options: [],
value: [],
list: [],
loading: false,
states: [
'Alabama',
'Alaska',
'Arizona',
'Arkansas',
'California',
'Colorado',
'Connecticut',
'Delaware',
'Florida',
'Georgia',
'Hawaii',
'Idaho',
'Illinois',
'Indiana',
'Iowa',
'Kansas',
'Kentucky',
'Louisiana',
'Maine',
'Maryland',
'Massachusetts',
'Michigan',
'Minnesota',
'Mississippi',
'Missouri',
'Montana',
'Nebraska',
'Nevada',
'New Hampshire',
'New Jersey',
'New Mexico',
'New York',
'North Carolina',
'North Dakota',
'Ohio',
'Oklahoma',
'Oregon',
'Pennsylvania',
'Rhode Island',
'South Carolina',
'South Dakota',
'Tennessee',
'Texas',
'Utah',
'Vermont',
'Virginia',
'Washington',
'West Virginia',
'Wisconsin',
'Wyoming',
],
}
},
mounted() {
this.list = this.states.map((item) => {
return { value: `value:${item}`, label: `label:${item}` }
})
},
methods: {
remoteMethod(query) {
if (query !== '') {
this.loading = true
setTimeout(() => {
this.loading = false
this.options = this.list.filter((item) => {
return item.label.toLowerCase().indexOf(query.toLowerCase()) > -1
})
}, 200)
} else {
this.options = []
}
},
},
}
</script>
可以創(chuàng)建并選中選項中不存在的條目
使用allow-create屬性即可通過在輸入框中輸入文字來創(chuàng)建新的條目。注意此時filterable必須為真。本例還使用了default-first-option屬性,在該屬性打開的情況下,按下回車就可以選中當前選項列表中的第一個選項,無需使用鼠標或鍵盤方向鍵進行定位。
<template>
<el-select
v-model="value"
multiple
filterable
allow-create
default-first-option
placeholder="請選擇文章標簽"
>
<el-option
v-for="item in options"
:key="item.value"
:label="item.label"
:value="item.value"
>
</el-option>
</el-select>
</template>
<script>
export default {
data() {
return {
options: [
{
value: 'HTML',
label: 'HTML',
},
{
value: 'CSS',
label: 'CSS',
},
{
value: 'JavaScript',
label: 'JavaScript',
},
],
value: [],
}
},
}
</script>
如果 Select 的綁定值為對象類型,請務必指定 value-key 作為它的唯一性標識。
參數(shù) | 說明 | 類型 | 可選值 | 默認值 |
---|---|---|---|---|
model-value / v-model | 綁定值 | string / number / boolean / object | — | — |
multiple | 是否多選 | boolean | — | false |
disabled | 是否禁用 | boolean | — | false |
value-key | 作為 value 唯一標識的鍵名,綁定值為對象類型時必填 | string | — | value |
size | 輸入框尺寸 | string | medium/small/mini | — |
clearable | 是否可以清空選項 | boolean | — | false |
collapse-tags | 多選時是否將選中值按文字的形式展示 | boolean | — | false |
multiple-limit | 多選時用戶最多可以選擇的項目數(shù),為 0 則不限制 | number | — | 0 |
name | select input 的 name 屬性 | string | — | — |
autocomplete | select input 的 autocomplete 屬性 | string | — | off |
placeholder | 占位符 | string | — | 請選擇 |
filterable | 是否可搜索 | boolean | — | false |
allow-create | 是否允許用戶創(chuàng)建新條目,需配合 filterable 使用 | boolean | — | false |
filter-method | 自定義搜索方法 | function | — | — |
remote | 是否為遠程搜索 | boolean | — | false |
remote-method | 遠程搜索方法 | function | — | — |
loading | 是否正在從遠程獲取數(shù)據(jù) | boolean | — | false |
loading-text | 遠程加載時顯示的文字 | string | — | 加載中 |
no-match-text | 搜索條件無匹配時顯示的文字,也可以使用#empty 設(shè)置 | string | — | 無匹配數(shù)據(jù) |
no-data-text | 選項為空時顯示的文字,也可以使用#empty 設(shè)置 | string | — | 無數(shù)據(jù) |
popper-class | Select 下拉框的類名 | string | — | — |
reserve-keyword | 多選且可搜索時,是否在選中一個選項后保留當前的搜索關(guān)鍵詞 | boolean | — | false |
default-first-option | 在輸入框按下回車,選擇第一個匹配項。需配合 filterable 或 remote 使用 | boolean | - | false |
popper-append-to-body | 是否將彈出框插入至 body 元素。在彈出框的定位出現(xiàn)問題時,可將該屬性設(shè)置為 false | boolean | - | true |
automatic-dropdown | 對于不可搜索的 Select,是否在輸入框獲得焦點后自動彈出選項菜單 | boolean | - | false |
clear-icon | 自定義清空圖標的類名 | string | — | el-icon-circle-close |
事件名稱 | 說明 | 回調(diào)參數(shù) |
---|---|---|
change | 選中值發(fā)生變化時觸發(fā) | 目前的選中值 |
visible-change | 下拉框出現(xiàn)/隱藏時觸發(fā) | 出現(xiàn)則為 true,隱藏則為 false |
remove-tag | 多選模式下移除 tag 時觸發(fā) | 移除的 tag 值 |
clear | 可清空的單選模式下用戶點擊清空按鈕時觸發(fā) | — |
blur | 當 input 失去焦點時觸發(fā) | (event: Event) |
focus | 當 input 獲得焦點時觸發(fā) | (event: Event) |
name | 說明 |
---|---|
— | Option 組件列表 |
prefix | Select 組件頭部內(nèi)容 |
empty | 無選項時的列表 |
參數(shù) | 說明 | 類型 | 可選值 | 默認值 |
---|---|---|---|---|
value | 選項的值 | string / number / boolean / object | — | — |
label | 選項的標簽,若不設(shè)置則默認與 value 相同 | string/number | — | — |
disabled | 是否禁用該選項 | boolean | — | false |
方法名 | 說明 | 參數(shù) |
---|---|---|
focus | 使 input 獲取焦點 | - |
blur | 使 input 失去焦點,并隱藏下拉框 | - |
更多建議: