Tree Select
Tree select
Markup Schema Example
<script lang="ts" setup>
import { createForm } from '@silver-formily/core'
import { FormItem, Submit, TreeSelect } from '@silver-formily/element-plus'
import { createSchemaField, FormProvider } from '@silver-formily/vue'
const data = [
{
value: '1',
label: 'Level one 1',
children: [
{
value: '1-1',
label: 'Level two 1-1',
children: [
{
value: '1-1-1',
label: 'Level three 1-1-1',
},
],
},
],
},
{
value: '2',
label: 'Level one 2',
children: [
{
value: '2-1',
label: 'Level two 2-1',
children: [
{
value: '2-1-1',
label: 'Level three 2-1-1',
},
],
},
{
value: '2-2',
label: 'Level two 2-2',
children: [
{
value: '2-2-1',
label: 'Level three 2-2-1',
},
],
},
],
},
{
value: '3',
label: 'Level one 3',
children: [
{
value: '3-1',
label: 'Level two 3-1',
children: [
{
value: '3-1-1',
label: 'Level three 3-1-1',
},
],
},
{
value: '3-2',
label: 'Level two 3-2',
children: [
{
value: '3-2-1',
label: 'Level three 3-2-1',
},
],
},
],
},
]
const form = createForm()
const { SchemaField, SchemaStringField } = createSchemaField({
components: {
FormItem,
TreeSelect,
},
})
function log(value: Record<string, any>) {
console.log(value)
}
</script>
<template>
<FormProvider :form="form">
<SchemaField>
<SchemaStringField
name="treeSelect"
title="树形选择"
x-decorator="FormItem"
x-component="TreeSelect"
:enum="data"
/>
</SchemaField>
<Submit @submit="log">
提交
</Submit>
</FormProvider>
</template>JSON Schema Example
<script lang="ts" setup>
import { createForm } from '@silver-formily/core'
import { Form, FormItem, Submit, TreeSelect } from '@silver-formily/element-plus'
import { createSchemaField } from '@silver-formily/vue'
const data = [
{
value: '1',
label: 'Level one 1',
children: [
{
value: '1-1',
label: 'Level two 1-1',
children: [
{
value: '1-1-1',
label: 'Level three 1-1-1',
},
],
},
],
},
{
value: '2',
label: 'Level one 2',
children: [
{
value: '2-1',
label: 'Level two 2-1',
children: [
{
value: '2-1-1',
label: 'Level three 2-1-1',
},
],
},
{
value: '2-2',
label: 'Level two 2-2',
children: [
{
value: '2-2-1',
label: 'Level three 2-2-1',
},
],
},
],
},
{
value: '3',
label: 'Level one 3',
children: [
{
value: '3-1',
label: 'Level two 3-1',
children: [
{
value: '3-1-1',
label: 'Level three 3-1-1',
},
],
},
{
value: '3-2',
label: 'Level two 3-2',
children: [
{
value: '3-2-1',
label: 'Level three 3-2-1',
},
],
},
],
},
]
const form = createForm()
const schema = {
type: 'object',
properties: {
treeSelect: {
'type': 'string',
'title': '树形选择',
'enum': data,
'x-decorator': 'FormItem',
'x-component': 'TreeSelect',
},
},
}
const { SchemaField } = createSchemaField({
components: {
FormItem,
TreeSelect,
},
})
async function onSubmit(value: Record<string, any>) {
console.log(value)
}
</script>
<template>
<Form :form="form" label-align="left" :label-width="160">
<SchemaField :schema="schema" />
<Submit @submit="onSubmit">
提交
</Submit>
</Form>
</template>Template Example
<script lang="ts" setup>
import { createForm } from '@silver-formily/core'
import { FormItem, Submit, TreeSelect } from '@silver-formily/element-plus'
import { Field, FormProvider } from '@silver-formily/vue'
const data = [
{
value: '1',
label: 'Level one 1',
children: [
{
value: '1-1',
label: 'Level two 1-1',
children: [
{
value: '1-1-1',
label: 'Level three 1-1-1',
},
],
},
],
},
{
value: '2',
label: 'Level one 2',
children: [
{
value: '2-1',
label: 'Level two 2-1',
children: [
{
value: '2-1-1',
label: 'Level three 2-1-1',
},
],
},
{
value: '2-2',
label: 'Level two 2-2',
children: [
{
value: '2-2-1',
label: 'Level three 2-2-1',
},
],
},
],
},
{
value: '3',
label: 'Level one 3',
children: [
{
value: '3-1',
label: 'Level two 3-1',
children: [
{
value: '3-1-1',
label: 'Level three 3-1-1',
},
],
},
{
value: '3-2',
label: 'Level two 3-2',
children: [
{
value: '3-2-1',
label: 'Level three 3-2-1',
},
],
},
],
},
]
const form = createForm()
function log(value: Record<string, any>) {
console.log(value)
}
</script>
<template>
<FormProvider :form="form">
<Field
name="treeSelect"
title="树形选择"
:decorator="[FormItem]"
:component="[TreeSelect]"
:data-source="data"
initial-value="1-1-1"
/>
<Submit @submit="log">
提交
</Submit>
</FormProvider>
</template>Multiple selection and deselecting
Set multiple: true to enable multiple selection. Selected nodes are rendered as tags; click the close icon on a tag to deselect a single node, and use clearable: true to clear all selections at once. In multiple mode the form value is an array of node values.
<script lang="ts" setup>
import { createForm } from '@silver-formily/core'
import { FormItem, TreeSelect } from '@silver-formily/element-plus'
import { Field, FormConsumer, FormProvider } from '@silver-formily/vue'
const data = [
{
value: '1',
label: 'Level one 1',
children: [
{
value: '1-1',
label: 'Level two 1-1',
children: [
{
value: '1-1-1',
label: 'Level three 1-1-1',
},
],
},
],
},
{
value: '2',
label: 'Level one 2',
children: [
{
value: '2-1',
label: 'Level two 2-1',
children: [
{
value: '2-1-1',
label: 'Level three 2-1-1',
},
],
},
{
value: '2-2',
label: 'Level two 2-2',
children: [
{
value: '2-2-1',
label: 'Level three 2-2-1',
},
],
},
],
},
{
value: '3',
label: 'Level one 3',
children: [
{
value: '3-1',
label: 'Level two 3-1',
children: [
{
value: '3-1-1',
label: 'Level three 3-1-1',
},
],
},
{
value: '3-2',
label: 'Level two 3-2',
children: [
{
value: '3-2-1',
label: 'Level three 3-2-1',
},
],
},
],
},
]
const form = createForm()
</script>
<template>
<FormProvider :form="form">
<Field
name="treeSelect"
title="多选树形选择"
:decorator="[FormItem]"
:component="[TreeSelect, { multiple: true, clearable: true, defaultExpandAll: true }]"
:data-source="data"
:initial-value="['1-1-1', '2-1-1']"
/>
<FormConsumer v-slot="{ form: currentForm }">
<pre>{{ JSON.stringify(currentForm.values, null, 2) }}</pre>
</FormConsumer>
</FormProvider>
</template>{
"treeSelect": [
"1-1-1",
"2-1-1"
]
}API
See https://element-plus.org/en-US/component/tree-select.html
Get Instance
Used to access the ElTreeSelect instance. For exposed methods, see the Element Plus documentation. The node-filtering demo shows the usage pattern. This is mainly used to control tree expansion and selection.
const treeSelectRef: Ref<TreeSelectInstance> = fieldRef.value.invoke('getTreeSelectRef')Slots
All slots from the original component are supported, and each slot additionally receives the field value in its slot scope for easier access.
Object values and property selection
Set optionAsValue: true to submit option objects. The optional optionValueKeys?: string[] strictly selects top-level properties. Omit it to return complete objects; an empty array returns an empty object, and missing properties are ignored. optionAsValue defaults to false.
Projection affects only the submitted value, never dataSource. Include the identity property (value, or the configured nodeKey / rowKey) in the allowlist. Labels are resolved from the complete data source, so the form value does not need a label.
const componentProps = {
optionAsValue: true,
optionValueKeys: ['value'],
}
// dataSource: [{ value: 1, label: 'One', extra: 'Business data' }]
// Form value: { value: 1 }<script setup lang="ts">
import { createForm } from '@silver-formily/core'
import { FormItem, TreeSelect } from '@silver-formily/element-plus'
import { Field, FormConsumer, FormProvider } from '@silver-formily/vue'
const form = createForm()
const options = [
{ value: 1, label: 'Alpha', extra: 'Not stored in the form' },
{ value: 2, label: 'Beta', extra: 'Not stored in the form' },
]
</script>
<template>
<FormProvider :form="form">
<Field
name="choice"
title="精简对象值"
:decorator="[FormItem]"
:component="[TreeSelect, { optionAsValue: true, optionValueKeys: ['value'], clearable: true }]"
:data-source="options"
/>
<FormConsumer v-slot="{ form: currentForm }">
<pre>{{ JSON.stringify(currentForm.values, null, 2) }}</pre>
</FormConsumer>
</FormProvider>
</template>{}