Cascader 级联选择
多层级选择器
基本用法
绑定一个数组
<template>
<tu-space vertical>
<tu-cascader v-model:value="value" :options="options"/>
<tu-cascader v-model:value="value" :options="options" disabled/>
</tu-space>
</template>
<script setup lang="ts">
import { ref } from 'vue'
import type { CascaderOption } from '../../../src/components/cascader/src/props'
function createOptions(depth = 3, iterator = 1, prefix = '') {
let i = -1
const length = 12
const options: CascaderOption[] = []
while (++i <= length) {
const disabled = i / (2 + iterator) === 1
const id = i + 1
if (iterator === 1) {
options[i] = {
label: `level ${id}`,
value: `${id}`,
disabled,
children: createOptions(depth, iterator + 1, `${id}`)
}
} else if (iterator === depth) {
options[i] = {
label: `level ${prefix}-${id}`,
value: `${prefix}-${id}`,
disabled
}
} else {
options[i] = {
label: `level ${prefix}-${id}`,
value: `${prefix}-${id}`,
disabled,
children: createOptions(depth, iterator + 1, `${prefix}-${id}`)
}
}
}
return options
}
const value = ref([])
const options = ref<CascaderOption[]>(createOptions())
</script>
<style>
.demo-cascader .tu-cascader {
width: 320px;
}
</style>
展开源码
可清空
<template>
<tu-cascader v-model:value="value" :options="options" clearable/>
</template>
<script setup lang="ts">
import { ref } from 'vue'
import type { CascaderOption } from '../../../src/components/cascader/src/props'
function createOptions(depth = 3, iterator = 1, prefix = '') {
let i = -1
const length = 12
const options: CascaderOption[] = []
while (++i <= length) {
const disabled = i / (2 + iterator) === 1
const id = i + 1
if (iterator === 1) {
options[i] = {
label: `level ${id}`,
value: `${id}`,
disabled,
children: createOptions(depth, iterator + 1, `${id}`)
}
} else if (iterator === depth) {
options[i] = {
label: `level ${prefix}-${id}`,
value: `${prefix}-${id}`,
disabled
}
} else {
options[i] = {
label: `level ${prefix}-${id}`,
value: `${prefix}-${id}`,
disabled,
children: createOptions(depth, iterator + 1, `${prefix}-${id}`)
}
}
}
return options
}
const value = ref([])
const options = ref<CascaderOption[]>(createOptions())
</script>
<style>
.demo-cascader .tu-cascader {
width: 320px;
}
</style>
展开源码
尺寸
<template>
<tu-space vertical>
<tu-cascader v-model:value="value" :options="options" size="small"/>
<tu-cascader v-model:value="value" :options="options" />
<tu-cascader v-model:value="value" :options="options" size="large"/>
</tu-space>
</template>
<script setup lang="ts">
import { ref } from 'vue'
import type { CascaderOption } from '../../../src/components/cascader/src/props'
function createOptions(depth = 3, iterator = 1, prefix = '') {
let i = -1
const length = 12
const options: CascaderOption[] = []
while (++i <= length) {
const disabled = i / (2 + iterator) === 1
const id = i + 1
if (iterator === 1) {
options[i] = {
label: `level ${id}`,
value: `${id}`,
disabled,
children: createOptions(depth, iterator + 1, `${id}`)
}
} else if (iterator === depth) {
options[i] = {
label: `level ${prefix}-${id}`,
value: `${prefix}-${id}`,
disabled
}
} else {
options[i] = {
label: `level ${prefix}-${id}`,
value: `${prefix}-${id}`,
disabled,
children: createOptions(depth, iterator + 1, `${prefix}-${id}`)
}
}
}
return options
}
const value = ref([])
const options = ref<CascaderOption[]>(createOptions())
</script>
<style>
.demo-cascader .tu-cascader {
width: 320px;
}
</style>
展开源码
Cascader 属性
名称 | 说明 | 类型 | 默认值 |
---|---|---|---|
value | 选中的值 | Array<number |string> | null | undefined |
options | 选择列表 | CascaderOption[] | [] |
value-field | CascaderOption 的 value 字段名 | string | 'value' |
label-field | CascaderOption 的 label 字段名 | string | 'label' |
children-field | CascaderOption 的 children 字段名 | string | 'children' |
disabled-field | CascaderOption 的 disabled 字段名 | string | 'disabled' |
placeholder | 提示内容 | string | undefined |
clearable | 是否可清空 | boolean | false |