Appearance
SelectV2Dispatcher
Element Plus v2 的 el-select 组件分发器,添加了只读功能。
基本用法
Please select
vue
<template>
<el-select-v2-dispatcher
v-model="value"
:options="options"
placeholder="Please select"
style="width: 240px"
/>
</template>
<script lang="ts" setup>
import { ref } from "vue";
const initials = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"];
const value = ref();
const options = Array.from({ length: 1000 }).map((_, idx) => ({
value: `Option ${idx + 1}`,
label: `${initials[idx % 10]}${idx}`,
}));
</script>
隐藏源码
禁用选择器本身或选项
选择禁用 Select 或者 Select 中的某个选项。
Please select
Please select
vue
<template>
<el-select-v2-dispatcher
v-model="value"
filterable
:options="options"
placeholder="Please select"
style="width: 240px; margin-right: 16px; vertical-align: middle"
multiple
/>
<el-select-v2-dispatcher
v-model="value"
disabled
filterable
:options="options"
placeholder="Please select"
style="width: 240px; vertical-align: middle"
multiple
/>
</template>
<script lang="ts" setup>
import { ref } from "vue";
const initials = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"];
const value = ref([]);
const options = Array.from({ length: 1000 }).map((_, idx) => ({
value: `Option${idx + 1}`,
label: `${initials[idx % 10]}${idx}`,
disabled: idx % 10 === 0,
}));
</script>
隐藏源码
尺寸
使用 size 属性改变选择器大小。 除了默认大小外,还有另外两个选项: large, small。
Please select
Please select
Please select
vue
<template>
<div class="flex flex-wrap gap-16px items-center">
<el-select-v2-dispatcher
v-model="value"
:options="options"
placeholder="Please select"
size="large"
style="width: 240px"
/>
<el-select-v2-dispatcher
v-model="value"
:options="options"
placeholder="Please select"
style="width: 240px"
/>
<el-select-v2-dispatcher
v-model="value"
:options="options"
placeholder="Please select"
size="small"
style="width: 240px"
/>
</div>
</template>
<script lang="ts" setup>
import { ref } from "vue";
const initials = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"];
const value = ref();
const options = Array.from({ length: 1000 }).map((_, idx) => ({
value: `Option ${idx + 1}`,
label: `${initials[idx % 10]}${idx}`,
}));
</script>
<style scoped>
.example-showcase .el-select-v2 {
margin-right: 20px;
}
</style>
隐藏源码
多选
多选选择器使用 tag 组件来展示已选中的选项。
Please select
vue
<template>
<el-select-v2-dispatcher
v-model="value"
:options="options"
placeholder="Please select"
style="width: 240px"
multiple
/>
</template>
<script lang="ts" setup>
import { ref } from "vue";
const initials = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"];
const value = ref([]);
const options = ref(
Array.from({ length: 1000 }).map((_, idx) => ({
value: `Option ${idx + 1}`,
label: `${initials[idx % 10]}${idx}`,
})),
);
</script>
隐藏源码
自定义选项的渲染模板
Please select
vue
<template>
<el-select-v2-dispatcher
v-model="value"
:options="options"
:props="props"
placeholder="Please select"
style="width: 240px"
filterable
multiple
/>
</template>
<script lang="ts" setup>
import { ref } from 'vue'
const initials = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
const props = {
label: 'name',
value: 'id',
}
const value = ref([])
const options = Array.from({ length: 1000 }).map((_, idx) => ({
id: `Option ${idx + 1}`,
name: `${initials[idx % 10]}${idx}`,
}))
</script>
隐藏源码
创建临时选项
创建并选中未包含在初始选项中的条目。
通过使用 allow-create 属性,用户可以通过输入框创建新项目。 为了使 allow-create 正常工作, filterable 的值必须为 true。 本例还使用了 default-first-option 属性, 在该属性为 true 的情况下,按下回车就可以选中当前选项列表中的第一个选项,无需使用鼠标或键盘方向键进行定位。
Please select
Please select
set reserve-keyword false
Please select
vue
<template>
<div style="flex: auto">
<div>
<el-select-v2-dispatcher
v-model="value1"
:options="options"
placeholder="Please select"
style="width: 240px; margin-right: 16px; vertical-align: middle"
allow-create
default-first-option
filterable
multiple
clearable
/>
<el-select-v2-dispatcher
v-model="value2"
:options="options"
placeholder="Please select"
style="width: 240px; vertical-align: middle"
allow-create
default-first-option
filterable
clearable
/>
</div>
<div>
<p style="margin-top: 20px; margin-bottom: 8px">
set reserve-keyword false
</p>
<el-select-v2-dispatcher
v-model="value3"
:options="options"
placeholder="Please select"
style="width: 240px; margin-right: 16px; vertical-align: middle"
allow-create
default-first-option
filterable
multiple
clearable
:reserve-keyword="false"
/>
</div>
</div>
</template>
<script lang="ts" setup>
import { ref } from "vue";
const initials = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"];
const value1 = ref([]);
const value2 = ref();
const value3 = ref([]);
const options = Array.from({ length: 1000 }).map((_, idx) => ({
value: `Option ${idx + 1}`,
label: `${initials[idx % 10]}${idx}`,
}));
</script>
隐藏源码
使用 value-key 键属性
vue
<template>
<el-select-v2-dispatcher
v-model="value"
:options="options"
placeholder="Please select"
value-key="name"
style="width: 240px"
/>
</template>
<script lang="ts" setup>
import { ref } from 'vue'
const initials = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
const value = ref({ name: 'Option 1', test: 'test 0' })
const options = Array.from({ length: 1000 }).map((_, idx) => ({
value: {
name: `Option ${idx + 1}`,
test: `test ${idx % 3}`,
},
label: `${initials[idx % 10]}${idx}`,
}))
</script>
<style scoped>
.example-showcase .el-select-v2 {
margin-right: 20px;
}
</style>
隐藏源码
自定义选项
当您的 options 格式不同于默认格式 您可以通过 props 属性自定义 options
Please select
vue
<template>
<el-select-v2-dispatcher
v-model="value"
:options="options"
:props="props"
placeholder="Please select"
style="width: 240px"
filterable
multiple
/>
</template>
<script lang="ts" setup>
import { ref } from 'vue'
const initials = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
const props = {
label: 'name',
value: 'id',
}
const value = ref([])
const options = Array.from({ length: 1000 }).map((_, idx) => ({
id: `Option ${idx + 1}`,
name: `${initials[idx % 10]}${idx}`,
}))
</script>
隐藏源码
自定义标签
将自定义的标签插入 el-select 的 slot 中即可。 collapse-tags, collapse-tags-tooltip, max-collapse-tags 在此模式下不生效.
vue
<template>
<el-select-v2-dispatcher
v-model="value"
multiple
placeholder="Select"
:options="colors"
style="width: 240px"
>
<template #default="{ item }">
<div class="flex items-center">
<el-tag :color="item.value" style="margin-right: 8px" size="small" />
<span :style="{ color: item.value }">{{ item.label }}</span>
</div>
</template>
<template #tag>
<el-tag v-for="color in value" :key="color" :color="color" />
</template>
</el-select-v2-dispatcher>
</template>
<script lang="ts" setup>
import { ref } from 'vue'
const value = ref<string[]>([])
const colors = [
{
value: '#E63415',
label: 'red',
},
{
value: '#FF6600',
label: 'orange',
},
{
value: '#FFDE0A',
label: 'yellow',
},
{
value: '#1EC79D',
label: 'green',
},
{
value: '#14CCCC',
label: 'cyan',
},
{
value: '#4167F0',
label: 'blue',
},
{
value: '#6222C9',
label: 'purple',
},
]
colors.forEach((color) => {
value.value.push(color.value)
})
</script>
<style scoped>
.el-tag {
border: none;
aspect-ratio: 1;
}
</style>
隐藏源码
空值配置
若想配置如空字符串为有效值而不是空值,可以配置 empty-values 为 [null, undefined].
如果您想要将清空值更改为 null, 请设置 value-on-clear 为 null
vue
<template>
<el-select-v2-dispatcher
v-model="value"
:options="options"
:empty-values="[null, undefined]"
:value-on-clear="null"
clearable
placeholder="Select"
style="width: 240px"
@clear="handleClear"
/>
</template>
<script lang="ts" setup>
import { ref } from 'vue'
import { ElMessage } from 'element-plus'
const value = ref('')
const initials = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
const options = Array.from({ length: 1000 }).map((_, idx) => ({
value: `Option ${idx + 1}`,
label: `${initials[idx % 10]}${idx}`,
}))
options.unshift({
value: '',
label: 'All',
})
const handleClear = () => {
ElMessage.info(`The clear value is: ${value.value}`)
}
</script>
隐藏源码
自定义标签
: Option 1
vue
<template>
<div class="flex flex-wrap gap-16px items-center">
<el-select-v2-dispatcher
v-model="value1"
:options="options"
placeholder="Select"
style="width: 240px"
clearable
>
<template #label="{ label, value }">
<span>{{ label }}: </span>
<span style="font-weight: bold">{{ value }}</span>
</template>
</el-select-v2-dispatcher>
<el-select-v2-dispatcher
v-model="value2"
:options="options"
placeholder="Select"
style="width: 240px"
clearable
multiple
>
<template #label="{ label, value }">
<span>{{ label }}: </span>
<span style="font-weight: bold">{{ value }}</span>
</template>
</el-select-v2-dispatcher>
</div>
</template>
<script lang="ts" setup>
import { ref } from 'vue'
const initials = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
const value1 = ref<string>('Option 1')
const value2 = ref<string[]>(['Option 1'])
const options = Array.from({ length: 1000 }).map((_, idx) => ({
value: `Option ${idx + 1}`,
label: `${initials[idx % 10]}${idx}`,
}))
</script>
<style scoped></style>
隐藏源码
Slots
使用 slots 中的 ${Config.namespace}Reader, ${Config.namespace}Writer 覆盖读写状态渲染函数。
writer: Option 1
vue
<template>
<el-select-dispatcher
v-model="select"
style="width: 240px"
placeholder="Please input"
>
<template #[`${Config.namespace}Reader`]>
<span style="color: green" key="reader">reader: {{ select }}</span>
</template>
<template #[`${Config.namespace}Writer`]>
<span style="color: red" key="writer">writer: {{ select }}</span>
</template>
<el-option
v-for="item in options"
:key="item.value"
:label="item.label"
:value="item.value"
/>
</el-select-dispatcher>
</template>
<script lang="ts" setup>
import { ref } from "vue";
import { Config } from "element-plus-form-dispatcher";
let index = 0;
const initials = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"];
const options = Array.from({ length: 1000 }).map((_, idx) => ({
value: `Option ${idx + 1}`,
label: `${initials[idx % 10]}${idx}`,
}));
const select = ref<string>(options[index].value);
const toggleValue = () => {
const length = options.length;
setTimeout(() => {
index = (index + 1) % length;
select.value = options[index].value;
toggleValue();
}, 2000);
};
toggleValue();
</script>
隐藏源码
API
与 el-select-v2 的 props attrs slots 完全一致