Select 选择器
当选项过多时,使用下拉菜单展示并选择内容。
基础用法
适用广泛的基础单选,通过 options 属性传入选项数据。
vue
<template>
<div style="width: 240px;">
<NSelect v-model="value" :options="options" placeholder="请选择" />
</div>
</template>
<script setup>
import { ref } from 'vue'
const value = ref('')
const options = [
{ label: '选项一', value: '1' },
{ label: '选项二', value: '2' },
{ label: '选项三', value: '3' },
]
</script>有禁用选项
在选项中设置 disabled 为 true 即可禁用该选项。
vue
<template>
<div style="width: 240px;">
<NSelect v-model="value" :options="options" placeholder="请选择" />
</div>
</template>
<script setup>
import { ref } from 'vue'
const value = ref('')
const options = [
{ label: '选项一', value: '1' },
{ label: '选项二', value: '2', disabled: true },
{ label: '选项三', value: '3' },
]
</script>禁用状态
设置 disabled 属性来禁用整个选择器。
vue
<template>
<div style="width: 240px;">
<NSelect v-model="value" :options="options" disabled placeholder="请选择" />
</div>
</template>
<script setup>
import { ref } from 'vue'
const value = ref('')
const options = [
{ label: '选项一', value: '1' },
{ label: '选项二', value: '2' },
{ label: '选项三', value: '3' },
]
</script>可清空
设置 clearable 属性来显示清空按钮。
vue
<template>
<div style="width: 240px;">
<NSelect v-model="value" :options="options" clearable placeholder="请选择" />
</div>
</template>
<script setup>
import { ref } from 'vue'
const value = ref('')
const options = [
{ label: '选项一', value: '1' },
{ label: '选项二', value: '2' },
{ label: '选项三', value: '3' },
]
</script>多选
设置 multiple 属性来启用多选。
vue
<template>
<div style="width: 240px;">
<NSelect v-model="value" :options="options" multiple placeholder="请选择" />
</div>
</template>
<script setup>
import { ref } from 'vue')
const value = ref([])
const options = [
{ label: '选项一', value: '1' },
{ label: '选项二', value: '2' },
{ label: '选项三', value: '3' },
]
</script>折叠多选标签
设置 collapse-tags 属性来折叠多选标签。
vue
<template>
<div style="width: 240px;">
<NSelect
v-model="value"
:options="options"
multiple
collapseTags
placeholder="请选择"
/>
</div>
</template>
<script setup>
import { ref } from 'vue'
const value = ref([])
const options = [
{ label: '选项一', value: '1' },
{ label: '选项二', value: '2' },
{ label: '选项三', value: '3' },
]
</script>可搜索
设置 filterable 属性来启用搜索功能。
vue
<template>
<div style="width: 240px;">
<NSelect v-model="value" :options="options" filterable placeholder="请选择" />
</div>
</template>
<script setup>
import { ref } from 'vue'
const value = ref('')
const options = [
{ label: '选项一', value: '1' },
{ label: '选项二', value: '2' },
{ label: '选项三', value: '3' },
]
</script>远程搜索
设置 remote 和 filterable 属性来启用远程搜索。
vue
<template>
<div style="width: 240px;">
<NSelect
v-model="value"
:options="options"
filterable
remote
:loading="loading"
placeholder="请输入搜索"
@remote-search="handleSearch"
/>
</div>
</template>
<script setup>
import { ref } from 'vue'
const value = ref('')
const loading = ref(false)
const options = ref([])
const handleSearch = (query) => {
if (query) {
loading.value = true
setTimeout(() => {
loading.value = false
options.value = [
{ label: query + ' - 结果1', value: query + '1' },
{ label: query + ' - 结果2', value: query + '2' },
]
}, 500)
}
}
</script>尺寸
使用 size 属性来设置选择器大小。
vue
<template>
<div style="width: 240px;">
<NSelect v-model="value1" :options="options" size="large" placeholder="大型选择器" />
<div style="margin-top: 12px;">
<NSelect v-model="value2" :options="options" placeholder="默认选择器" />
</div>
<div style="margin-top: 12px;">
<NSelect v-model="value3" :options="options" size="small" placeholder="小型选择器" />
</div>
</div>
</template>
<script setup>
import { ref } from 'vue'
const value1 = ref('')
const value2 = ref('')
const value3 = ref('')
const options = [
{ label: '选项一', value: '1' },
{ label: '选项二', value: '2' },
{ label: '选项三', value: '3' },
]
</script>API
Props
| 属性 | 说明 | 类型 | 默认值 |
|---|---|---|---|
| modelValue | 绑定值 | any | — |
| options | 选项数据 | SelectOption[] | [] |
| multiple | 是否多选 | boolean | false |
| disabled | 是否禁用 | boolean | false |
| size | 尺寸 | 'large' | 'default' | 'small' | 'default' |
| clearable | 是否可以清空选项 | boolean | false |
| collapseTags | 多选时是否将选中值按文字的形式展示 | boolean | false |
| filterable | 是否可搜索 | boolean | false |
| remote | 是否为远程搜索 | boolean | false |
| loading | 是否正在加载远程数据 | boolean | false |
| placeholder | 占位符 | string | '请选择' |
SelectOption
| 属性 | 说明 | 类型 | 默认值 |
|---|---|---|---|
| label | 选项的标签 | string | — |
| value | 选项的值 | any | — |
| disabled | 是否禁用 | boolean | false |
| children | 子选项 | SelectOption[] | — |
Events
| 事件名 | 说明 | 类型 |
|---|---|---|
| update:modelValue | 绑定值变化时触发 | (value: any) => void |
| change | 选中值发生变化时触发 | (value: any) => void |
| remote-search | 远程搜索时触发 | (query: string) => void |
| focus | 获取焦点时触发 | (event: FocusEvent) => void |
| blur | 失去焦点时触发 | (event: FocusEvent) => void |
| clear | 清空时触发 | () => void |
Slots
| 插槽名 | 说明 |
|---|---|
| header | 下拉框顶部内容 |
| footer | 下拉框底部内容 |
| empty | 无选项时的列表 |
方法
| 方法名 | 说明 | 类型 |
|---|---|---|
| focus | 使选择器获取焦点 | () => void |
| blur | 使选择器失去焦点 | () => void |
| clear | 清空选中的值 | () => void |