這個(gè)組件目前在測(cè)試當(dāng)中,如果在使用中發(fā)現(xiàn)任何漏洞和問(wèn)題,請(qǐng)?jiān)?nbsp;Github 中提交 issue 以便我們進(jìn)行處理
在數(shù)據(jù)量爆發(fā)的今天,很多時(shí)候一個(gè)選擇器可能從服務(wù)器加載非常多的數(shù)據(jù),然而瀏覽器在一次性把這些數(shù)據(jù)渲染到頁(yè)面上的時(shí)候會(huì)出現(xiàn)卡頓甚至是崩潰的情況,所以虛擬化技術(shù)應(yīng)運(yùn)而生,為了更好的用戶體驗(yàn)和更好的使用體驗(yàn),我們決定添加這個(gè)組件。
適用廣泛的基礎(chǔ)選擇器
<template>
<el-select-v2
v-model="value"
:options="options"
placeholder="請(qǐng)選擇"
style="width: 240px;"
/>
</template>
<script>
const initials = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
export default {
data() {
return {
options: Array.from({ length: 1000 }).map((_, idx) => ({
value: `選項(xiàng)${idx + 1}`,
label: `${initials[idx % 10]}${idx}`,
})),
value: '',
}
},
}
</script>
最基礎(chǔ)的多選選擇器
<template>
<el-select-v2
v-model="value"
:options="options"
placeholder="請(qǐng)選擇"
style="width: 240px;"
multiple
/>
</template>
<script>
const initials = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
export default {
data() {
return {
options: Array.from({ length: 1000 }).map((_, idx) => ({
value: `選項(xiàng)${idx + 1}`,
label: `${initials[idx % 10]}${idx}`,
})),
value: [],
}
},
}
</script>
<template>
<el-select-v2
v-model="value"
:options="options"
placeholder="請(qǐng)選擇"
style="width: 240px;"
multiple
collapse-tags
/>
</template>
<script>
const initials = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
export default {
data() {
return {
options: Array.from({ length: 1000 }).map((_, idx) => ({
value: `選項(xiàng)${idx + 1}`,
label: `${initials[idx % 10]}${idx}`,
})),
value: [],
}
},
}
</script>
當(dāng)選項(xiàng)過(guò)多時(shí),可通過(guò)匹配篩選
<template>
<el-select-v2
v-model="value"
filterable
:options="options"
placeholder="請(qǐng)選擇"
style="width: 240px;"
multiple
/>
</template>
<script>
const initials = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
export default {
data() {
return {
options: Array.from({ length: 1000 }).map((_, idx) => ({
value: `選項(xiàng)${idx + 1}`,
label: `${initials[idx % 10]}${idx}`,
})),
value: [],
}
},
}
</script>
您可以選擇禁用 Select 或者 Select 的 Option
<template>
<el-select-v2
v-model="value"
filterable
:options="options"
placeholder="請(qǐng)選擇"
style="width: 240px; margin-right: 16px; vertical-align: middle;"
multiple
/>
<el-select-v2
disabled
v-model="value"
filterable
:options="options"
placeholder="請(qǐng)選擇"
style="width: 240px; vertical-align: middle;"
multiple
/>
</template>
<script>
const initials = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
export default {
data() {
return {
options: Array.from({ length: 1000 }).map((_, idx) => ({
value: `選項(xiàng)${idx + 1}`,
label: `${initials[idx % 10]}${idx}`,
disabled: idx % 10 === 0,
})),
value: [],
}
},
}
</script>
選項(xiàng)分組
我們可以為選項(xiàng)分組,只需要滿足例子里的這個(gè) pattern
<template>
<el-select-v2
v-model="value"
filterable
:options="options"
placeholder="請(qǐng)選擇"
style="width: 240px;"
multiple
/>
</template>
<script>
const initials = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
export default {
data() {
return {
options: Array.from({ length: 10 }).map((_, idx) => {
const label = idx + 1
return {
value: `組 ${label}`,
label: `組 ${label}`,
options: Array.from({ length: 10 }).map((_, idx) => ({
value: `選項(xiàng)${idx + 1 + 10 * label}`,
label: `${initials[idx % 10]}${idx + 1 + 10 * label}`,
})),
}
}),
value: [],
}
},
}
</script>
我們也可以通過(guò)自己自定義模板來(lái)渲染自己想要的內(nèi)容。
<template>
<el-select-v2
v-model="value"
filterable
:options="options"
placeholder="請(qǐng)選擇"
style="width: 240px;"
multiple
>
<template #default="{item}">
<span style="margin-right: 8px;">{{ item.label }}</span>
<span style="color: var(--el-text-color-secondary); font-size: 13px">
{{ item.value }}
</span>
</template>
</el-select-v2>
</template>
<script>
const initials = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
export default {
data() {
return {
options: Array.from({ length: 1000 }).map((_, idx) => ({
value: `選項(xiàng)${idx + 1}`,
label: `${initials[idx % 10]}${idx}`,
})),
value: [],
}
},
}
</script>
一鍵刪除所有的選項(xiàng)(也可適用于單選)
<template>
<el-select-v2
v-model="value1"
:options="options"
placeholder="請(qǐng)選擇"
style="width: 240px; margin-right: 16px; vertical-align: middle;"
multiple
clearable
/>
<el-select-v2
v-model="value2"
:options="options"
placeholder="請(qǐng)選擇"
style="width: 240px; vertical-align: middle;"
clearable
/>
</template>
<script>
const initials = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
export default {
data() {
return {
options: Array.from({ length: 1000 }).map((_, idx) => ({
value: `選項(xiàng)${idx + 1}`,
label: `${initials[idx % 10]}${idx}`,
})),
value1: [],
value2: '',
}
},
}
</script>
可以創(chuàng)建并選中選項(xiàng)中不存在的條目
使用allow-create屬性即可通過(guò)在輸入框中輸入文字來(lái)創(chuàng)建新的條目。注意此時(shí)filterable必須為真。
<template>
<el-select-v2
v-model="value1"
:options="options"
placeholder="請(qǐng)選擇"
style="width: 240px; margin-right: 16px; vertical-align: middle;"
allow-create
filterable
multiple
clearable
/>
<el-select-v2
v-model="value2"
:options="options"
placeholder="請(qǐng)選擇"
style="width: 240px; vertical-align: middle;"
allow-create
filterable
clearable
/>
</template>
<script>
const initials = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
export default {
data() {
return {
options: Array.from({ length: 1000 }).map((_, idx) => ({
value: `選項(xiàng)${idx + 1}`,
label: `${initials[idx % 10]}${idx}`,
})),
value1: [],
value2: '',
}
},
}
</script>
從服務(wù)器搜索數(shù)據(jù),輸入關(guān)鍵字進(jìn)行查找
為了啟用遠(yuǎn)程搜索,需要將filterable和remote設(shè)置為true,同時(shí)傳入一個(gè)remote-method。remote-method為一個(gè)Function,它會(huì)在輸入值發(fā)生變化時(shí)調(diào)用,參數(shù)為當(dāng)前輸入值。
<template>
<el-select-v2
v-model="value"
style="width: 240px"
multiple
size="medium"
filterable
remote
:remote-method="remoteMethod"
clearable
:options="options"
:loading="loading"
placeholder="請(qǐng)輸入關(guān)鍵詞"
/>
</template>
<script>
export default {
created() {
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 = []
}
},
},
data() {
return {
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',
],
options: [],
value: [],
}
},
}
</script>
SelectV2 Attributes
參數(shù) | 說(shuō)明 | 類(lèi)型 | 可選值 | 默認(rèn)值 |
---|---|---|---|---|
model-value / v-model | 綁定值 | string / number / boolean / object | — | — |
multiple | 是否多選 | boolean | — | false |
disabled | 是否禁用 | boolean | — | false |
value-key | 作為 value 唯一標(biāo)識(shí)的鍵名,綁定值為對(duì)象類(lèi)型時(shí)必填 | string | — | value |
size | 輸入框尺寸 | string | medium/small/mini | — |
clearable | 是否可以清空選項(xiàng) | boolean | — | false |
collapse-tags | 多選時(shí)是否將選中值按文字的形式展示 | boolean | — | false |
multiple-limit | 多選時(shí)用戶最多可以選擇的項(xiàng)目數(shù),為 0 則不限制 | number | — | 0 |
name | select input 的 name 屬性 | string | — | — |
autocomplete | select input 的 autocomplete 屬性 | string | — | off |
placeholder | 占位符 | string | — | 請(qǐng)選擇 |
filterable | 是否可搜索 | boolean | — | false |
filter-method | 自定義搜索方法 | function | — | — |
remote | 是否為遠(yuǎn)程搜索 | boolean | — | false |
remote-method | 遠(yuǎn)程搜索方法 | function | — | — |
allow-create | 是否允許用戶創(chuàng)建新條目,需配合 filterable 使用 | boolean | — | false |
no-data-text | 選項(xiàng)為空時(shí)顯示的文字,也可以使用#empty 設(shè)置 | string | — | 無(wú)數(shù)據(jù) |
popper-class | Select 下拉框的類(lèi)名 | string | — | — |
popper-append-to-body | 是否將彈出框插入至 body 元素。在彈出框的定位出現(xiàn)問(wèn)題時(shí),可將該屬性設(shè)置為 false | boolean | - | false |
popper-options | 用戶定制化 popper 的行為, 更多請(qǐng)查看文檔popper.js | object | - | - |
automatic-dropdown | 對(duì)于不可搜索的 Select,是否在輸入框獲得焦點(diǎn)后自動(dòng)彈出選項(xiàng)菜單 | boolean | - | false |
clear-icon | 自定義清空?qǐng)D標(biāo)的類(lèi)名 | string | — | el-icon-circle-close |
height | 面板的高度,每項(xiàng)的高度為 34px | number | - | 170 |
事件名稱(chēng) | 說(shuō)明 | 回調(diào)參數(shù) |
---|---|---|
change | 選中值發(fā)生變化時(shí)觸發(fā) | 目前的選中值 |
visible-change | 下拉框出現(xiàn)/隱藏時(shí)觸發(fā) | 出現(xiàn)則為 true,隱藏則為 false |
remove-tag | 多選模式下移除 tag 時(shí)觸發(fā) | 移除的 tag 值 |
clear | 可清空的單選模式下用戶點(diǎn)擊清空按鈕時(shí)觸發(fā) | — |
blur | 當(dāng) input 失去焦點(diǎn)時(shí)觸發(fā) | (event: Event) |
focus | 當(dāng) input 獲得焦點(diǎn)時(shí)觸發(fā) | (event: Event) |
name | 說(shuō)明 |
---|---|
default | Option 模板 |
empty | 無(wú)選項(xiàng)時(shí)的列表 |
更多建議: