Appearance
SelectDispatcher
Element Plus 的 el-select 组件分发器,添加了只读功能。
基本用法
Select
vue
<template>
<el-select-dispatcher
v-model="value"
placeholder="Select"
style="width: 240px"
>
<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";
const value = ref("");
const options = [
{
value: "Value1",
label: "Label1",
},
{
value: "Value2",
label: "Label2",
},
{
value: "Value3",
label: "Label3",
},
{
value: "Value4",
label: "Label4",
},
{
value: "Value5",
label: "Label5",
},
];
</script>
隐藏源码
禁用状态
禁用整个选择器组件
为 el-select 设置 disabled 属性,则整个选择器不可用。
vue
<template>
<el-select-dispatcher
v-model="value"
disabled
placeholder="Select"
style="width: 240px"
>
<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";
const value = ref("Option1");
const options = [
{
value: "Option1",
label: "Option1",
},
{
value: "Option2",
label: "Option2",
},
{
value: "Option3",
label: "Option3",
},
{
value: "Option4",
label: "Option4",
},
{
value: "Option5",
label: "Option5",
},
];
</script>
隐藏源码
尺寸
使用 size 属性改变选择器大小。 除了默认大小外,还有另外两个选项: large, small。
Select
Select
Select
vue
<template>
<div class="flex flex-wrap gap-16px items-center">
<el-select-dispatcher
v-model="value"
placeholder="Select"
size="large"
style="width: 200px"
>
<el-option
v-for="item in options"
:key="item.value"
:label="item.label"
:value="item.value"
/>
</el-select-dispatcher>
<el-select-dispatcher
v-model="value"
placeholder="Select"
style="width: 200px"
>
<el-option
v-for="item in options"
:key="item.value"
:label="item.label"
:value="item.value"
/>
</el-select-dispatcher>
<el-select-dispatcher
v-model="value"
placeholder="Select"
size="small"
style="width: 200px"
>
<el-option
v-for="item in options"
:key="item.value"
:label="item.label"
:value="item.value"
/>
</el-select-dispatcher>
</div>
</template>
<script lang="ts" setup>
import { ref } from "vue";
const value = ref("");
const options = [
{
value: "Option1",
label: "Option1",
},
{
value: "Option2",
label: "Option2",
},
{
value: "Option3",
label: "Option3",
},
{
value: "Option4",
label: "Option4",
},
{
value: "Option5",
label: "Option5",
},
];
</script>
隐藏源码
多选
多选选择器使用 tag 组件来展示已选中的选项。
default
Select
use collapse-tags
Select
use collapse-tags-tooltip
Select
use max-collapse-tags
Select
vue
<template>
<div class="m-16px">
<p>default</p>
<el-select-dispatcher
v-model="value1"
multiple
placeholder="Select"
style="width: 240px"
>
<el-option
v-for="item in options"
:key="item.value"
:label="item.label"
:value="item.value"
/>
</el-select-dispatcher>
</div>
<div class="m-16px">
<p>use collapse-tags</p>
<el-select-dispatcher
v-model="value2"
multiple
collapse-tags
placeholder="Select"
style="width: 240px"
>
<el-option
v-for="item in options"
:key="item.value"
:label="item.label"
:value="item.value"
/>
</el-select-dispatcher>
</div>
<div class="m-16px">
<p>use collapse-tags-tooltip</p>
<el-select-dispatcher
v-model="value3"
multiple
collapse-tags
collapse-tags-tooltip
placeholder="Select"
style="width: 240px"
>
<el-option
v-for="item in options"
:key="item.value"
:label="item.label"
:value="item.value"
/>
</el-select-dispatcher>
</div>
<div class="m-16px">
<p>use max-collapse-tags</p>
<el-select-dispatcher
v-model="value4"
multiple
collapse-tags
collapse-tags-tooltip
:max-collapse-tags="3"
placeholder="Select"
style="width: 240px"
>
<el-option
v-for="item in options"
:key="item.value"
:label="item.label"
:value="item.value"
/>
</el-select-dispatcher>
</div>
</template>
<script lang="ts" setup>
import { ref } from "vue";
const value1 = ref([]);
const value2 = ref([]);
const value3 = ref([]);
const value4 = ref([]);
const options = [
{
value: "Option1",
label: "Option1",
},
{
value: "Option2",
label: "Option2",
},
{
value: "Option3",
label: "Option3",
},
{
value: "Option4",
label: "Option4",
},
{
value: "Option5",
label: "Option5",
},
];
</script>
隐藏源码
将选项进行分组
你可以为选项进行分组来区分不同的选项
使用 el-option-group 对备选项进行分组,它的 label 属性为分组名
Select
vue
<template>
<el-select-dispatcher
v-model="value"
placeholder="Select"
style="width: 240px"
>
<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-group>
</el-select-dispatcher>
</template>
<script lang="ts" setup>
import { ref } from "vue";
const value = ref("");
const options = [
{
label: "Popular cities",
options: [
{
value: "1",
label: "Shanghai",
},
{
value: "2",
label: "Beijing",
},
],
},
{
label: "City name",
options: [
{
value: "3",
label: "Chengdu",
},
{
value: "4",
label: "Shenzhen",
},
{
value: "5",
label: "Guangzhou",
},
{
value: "6",
label: "Dalian",
},
],
},
];
</script>
隐藏源码
使用值键 value-key 属性
如果 Select 的绑定值为对象类型,请务必指定 value-key 作为它的唯一性标识。
通过使用 value-key 属性,可以正确处理带有重复 label 的数据。 这样虽然 label 是重复的,但任可通过 id 来确认唯一性。
Select
selected option's description: no select
vue
<template>
<div class="m-16px">
<el-select-dispatcher
v-model="value"
value-key="id"
placeholder="Select"
style="width: 240px"
>
<el-option
v-for="item in options"
:key="item.id"
:label="item.label"
:value="item"
/>
</el-select-dispatcher>
<p>
selected option's description:
{{ value ? value.desc : "no select" }}
</p>
</div>
</template>
<script setup lang="ts">
import { ref } from "vue";
type Option = {
id: number;
label: string;
desc: string;
};
const value = ref<Option>();
const options = ref([
{ id: 1, label: "Option A", desc: "Option A - 230506" },
{ id: 2, label: "Option B", desc: "Option B - 230506" },
{ id: 3, label: "Option C", desc: "Option C - 230506" },
{ id: 4, label: "Option A", desc: "Option A - 230507" },
]);
</script>
隐藏源码
自定义标签 (tag)
您可以自定义标签。
将自定义的标签插入 el-select 的 slot 中即可。 collapse-tags, collapse-tags-tooltip, max-collapse-tags 在此模式下不生效.
vue
<template>
<el-select-dispatcher
v-model="value"
multiple
placeholder="Select"
style="width: 240px"
>
<el-option
v-for="item in colors"
:key="item.value"
:label="item.label"
:value="item.value"
>
<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>
</el-option>
<template #tag>
<el-tag v-for="color in value" :key="color" :color="color" />
</template>
</el-select-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>
隐藏源码
自定义标签 (label)
: Option1
vue
<template>
<div class="flex flex-wrap gap-16px items-center">
<el-select-dispatcher
v-model="value1"
placeholder="Select"
style="width: 240px"
clearable
>
<template #label="{ label, value }">
<span>{{ label }}: </span>
<span style="font-weight: bold">{{ value }}</span>
</template>
<el-option
v-for="item in options"
:key="item.value"
:label="item.label"
:value="item.value"
/>
</el-select-dispatcher>
<el-select-dispatcher
v-model="value2"
placeholder="Select"
style="width: 240px"
clearable
multiple
>
<template #label="{ label, value }">
<span>{{ label }}: </span>
<span style="font-weight: bold">{{ value }}</span>
</template>
<el-option
v-for="item in options"
:key="item.value"
:label="item.label"
:value="item.value"
/>
</el-select-dispatcher>
</div>
</template>
<script lang="ts" setup>
import { ref } from "vue";
const value1 = ref<string>("Option1");
const value2 = ref<string[]>(["Option1"]);
const options = [
{
value: "Option1",
label: "Label1",
},
{
value: "Option2",
label: "Label2",
},
{
value: "Option3",
label: "Label3",
},
{
value: "Option4",
label: "Label4",
},
{
value: "Option5",
label: "Label5",
},
];
</script>
<style scoped></style>
隐藏源码
Slots
使用 slots 中的 ${Config.namespace}Reader, ${Config.namespace}Writer 覆盖读写状态渲染函数
writer: Option1
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 options = [
{
value: "Option1",
label: "Option1",
},
{
value: "Option2",
label: "Option2",
},
{
value: "Option3",
label: "Option3",
},
{
value: "Option4",
label: "Option4",
},
{
value: "Option5",
label: "Option5",
},
];
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 的 props attrs slots 完全一致