# TreeSelect
树选择器
# Markup Schema 同步数据源案例
<template>
<FormProvider :form="form">
<SchemaField>
<SchemaStringField
name="select"
title="选择框"
x-decorator="FormItem"
x-component="TreeSelect"
:x-component-props="{
style: {
width: '200px',
},
}"
:enum="[
{
label: '选项1',
value: 1,
children: [
{
title: 'Child Node1',
value: '0-0-0',
key: '0-0-0',
},
{
title: 'Child Node2',
value: '0-0-1',
key: '0-0-1',
},
{
title: 'Child Node3',
value: '0-0-2',
key: '0-0-2',
},
],
},
{
label: '选项2',
value: 2,
children: [
{
title: 'Child Node3',
value: '0-1-0',
key: '0-1-0',
},
{
title: 'Child Node4',
value: '0-1-1',
key: '0-1-1',
},
{
title: 'Child Node5',
value: '0-1-2',
key: '0-1-2',
},
],
},
]"
/>
</SchemaField>
<Submit @submit="log">提交</Submit>
</FormProvider>
</template>
<script>
import { createForm } from '@formily/core'
import { createSchemaField, FormProvider } from '@formily/vue'
import { FormItem, TreeSelect, Submit } from '@formily/antdv'
const form = createForm()
const fields = createSchemaField({
components: {
FormItem,
TreeSelect,
},
})
export default {
components: { FormProvider, ...fields, Submit },
data() {
return {
form,
}
},
methods: {
log(value) {
console.log(value)
},
},
}
</script>
# Markup Schema 异步联动数据源案例
<template>
<FormProvider :form="form">
<SchemaField>
<SchemaNumberField
name="linkage"
title="联动选择框"
x-decorator="FormItem"
x-component="Select"
:enum="[
{ label: '发请求1', value: 1 },
{ label: '发请求2', value: 2 },
]"
:x-component-props="{
style: {
width: '200px',
},
}"
/>
<SchemaStringField
name="select"
title="异步选择框"
x-decorator="FormItem"
x-component="TreeSelect"
:x-component-props="{
style: {
width: '200px',
},
}"
/>
</SchemaField>
<Submit @submit="log">提交</Submit>
</FormProvider>
</template>
<script>
import { createForm, onFieldReact } from '@formily/core'
import { createSchemaField, FormProvider } from '@formily/vue'
import { FormItem, TreeSelect, Submit, Select } from '@formily/antdv'
import { action } from '@formily/reactive'
const useAsyncDataSource = (pattern, service) => {
onFieldReact(pattern, (field) => {
field.loading = true
service(field).then(
action.bound((data) => {
field.dataSource = data
field.loading = false
})
)
})
}
const fields = createSchemaField({
components: {
FormItem,
TreeSelect,
Select,
},
})
export default {
components: { FormProvider, ...fields, Submit },
data() {
const form = createForm({
effects: () => {
useAsyncDataSource('select', async (field) => {
const linkage = field.query('linkage').get('value')
if (!linkage) return []
return new Promise((resolve) => {
setTimeout(() => {
if (linkage === 1) {
resolve([
{
label: 'AAA',
value: 'aaa',
children: [
{
title: 'Child Node1',
value: '0-0-0',
key: '0-0-0',
},
{
title: 'Child Node2',
value: '0-0-1',
key: '0-0-1',
},
{
title: 'Child Node3',
value: '0-0-2',
key: '0-0-2',
},
],
},
{
label: 'BBB',
value: 'ccc',
children: [
{
title: 'Child Node1',
value: '0-1-0',
key: '0-1-0',
},
{
title: 'Child Node2',
value: '0-1-1',
key: '0-1-1',
},
{
title: 'Child Node3',
value: '0-1-2',
key: '0-1-2',
},
],
},
])
} else if (linkage === 2) {
resolve([
{
label: 'CCC',
value: 'ccc',
children: [
{
title: 'Child Node1',
value: '0-0-0',
key: '0-0-0',
},
{
title: 'Child Node2',
value: '0-0-1',
key: '0-0-1',
},
{
title: 'Child Node3',
value: '0-0-2',
key: '0-0-2',
},
],
},
{
label: 'DDD',
value: 'ddd',
children: [
{
title: 'Child Node1',
value: '0-1-0',
key: '0-1-0',
},
{
title: 'Child Node2',
value: '0-1-1',
key: '0-1-1',
},
{
title: 'Child Node3',
value: '0-1-2',
key: '0-1-2',
},
],
},
])
}
}, 1500)
})
})
},
})
return {
form,
}
},
methods: {
log(value) {
console.log(value)
},
},
}
</script>
# JSON Schema 同步数据源案例
<template>
<FormProvider :form="form">
<SchemaField :schema="schema" />
<Submit @submit="log">提交</Submit>
</FormProvider>
</template>
<script>
import { createForm } from '@formily/core'
import { createSchemaField, FormProvider } from '@formily/vue'
import { FormItem, TreeSelect, Submit } from '@formily/antdv'
const form = createForm()
const schema = {
type: 'object',
properties: {
select: {
type: 'string',
title: '选择框',
'x-decorator': 'FormItem',
'x-component': 'TreeSelect',
enum: [
{
label: '选项1',
value: 1,
children: [
{
title: 'Child Node1',
value: '0-0-0',
key: '0-0-0',
},
{
title: 'Child Node2',
value: '0-0-1',
key: '0-0-1',
},
{
title: 'Child Node3',
value: '0-0-2',
key: '0-0-2',
},
],
},
{
label: '选项2',
value: 2,
children: [
{
title: 'Child Node1',
value: '0-1-0',
key: '0-1-0',
},
{
title: 'Child Node2',
value: '0-1-1',
key: '0-1-1',
},
{
title: 'Child Node3',
value: '0-1-2',
key: '0-1-2',
},
],
},
],
'x-component-props': {
style: {
width: '200px',
},
},
},
},
}
const fields = createSchemaField({
components: {
FormItem,
TreeSelect,
},
})
export default {
components: { FormProvider, ...fields, Submit },
data() {
return {
form,
schema,
}
},
methods: {
log(value) {
console.log(value)
},
},
}
</script>
# JSON Schema 异步联动数据源案例
<template>
<FormProvider :form="form">
<SchemaField :schema="schema" :scope="{ useAsyncDataSource, loadData }" />
<Submit @submit="log">提交</Submit>
</FormProvider>
</template>
<script>
import { createForm } from '@formily/core'
import { createSchemaField, FormProvider } from '@formily/vue'
import { FormItem, TreeSelect, Submit, Select } from '@formily/antdv'
import { action } from '@formily/reactive'
const loadData = async (field) => {
const linkage = field.query('linkage').get('value')
if (!linkage) return []
return new Promise((resolve) => {
setTimeout(() => {
if (linkage === 1) {
resolve([
{
label: 'AAA',
value: 'aaa',
children: [
{
title: 'Child Node1',
value: '0-0-0',
key: '0-0-0',
},
{
title: 'Child Node2',
value: '0-0-1',
key: '0-0-1',
},
{
title: 'Child Node3',
value: '0-0-2',
key: '0-0-2',
},
],
},
{
label: 'BBB',
value: 'ccc',
children: [
{
title: 'Child Node1',
value: '0-1-0',
key: '0-1-0',
},
{
title: 'Child Node2',
value: '0-1-1',
key: '0-1-1',
},
{
title: 'Child Node3',
value: '0-1-2',
key: '0-1-2',
},
],
},
])
} else if (linkage === 2) {
resolve([
{
label: 'CCC',
value: 'ccc',
children: [
{
title: 'Child Node1',
value: '0-0-0',
key: '0-0-0',
},
{
title: 'Child Node2',
value: '0-0-1',
key: '0-0-1',
},
{
title: 'Child Node3',
value: '0-0-2',
key: '0-0-2',
},
],
},
{
label: 'DDD',
value: 'ddd',
children: [
{
title: 'Child Node1',
value: '0-1-0',
key: '0-1-0',
},
{
title: 'Child Node2',
value: '0-1-1',
key: '0-1-1',
},
{
title: 'Child Node3',
value: '0-1-2',
key: '0-1-2',
},
],
},
])
}
}, 1500)
})
}
const useAsyncDataSource = (service) => (field) => {
field.loading = true
service(field).then(
action.bound((data) => {
field.dataSource = data
field.loading = false
})
)
}
const schema = {
type: 'object',
properties: {
linkage: {
type: 'string',
title: '联动选择框',
enum: [
{ label: '发请求1', value: 1 },
{ label: '发请求2', value: 2 },
],
'x-decorator': 'FormItem',
'x-component': 'Select',
'x-component-props': {
style: {
width: '200px',
},
},
},
select: {
type: 'string',
title: '异步选择框',
'x-decorator': 'FormItem',
'x-component': 'TreeSelect',
'x-component-props': {
style: {
width: '200px',
},
},
'x-reactions': ['{{useAsyncDataSource(loadData)}}'],
},
},
}
const form = createForm()
const fields = createSchemaField({
components: {
FormItem,
TreeSelect,
Select,
},
})
export default {
components: { FormProvider, ...fields, Submit },
data() {
return {
form,
schema,
}
},
methods: {
useAsyncDataSource,
loadData,
log(value) {
console.log(value)
},
},
}
</script>
# Template 同步数据源案例
<template>
<FormProvider :form="form">
<Field
name="select"
title="选择框"
:dataSource="[
{
label: '选项1',
value: 1,
children: [
{
title: 'Child Node1',
value: '0-0-0',
key: '0-0-0',
},
{
title: 'Child Node2',
value: '0-0-1',
key: '0-0-1',
},
{
title: 'Child Node3',
value: '0-0-2',
key: '0-0-2',
},
],
},
{
label: '选项2',
value: 2,
children: [
{
title: 'Child Node3',
value: '0-1-0',
key: '0-1-0',
},
{
title: 'Child Node4',
value: '0-1-1',
key: '0-1-1',
},
{
title: 'Child Node5',
value: '0-1-2',
key: '0-1-2',
},
],
},
]"
:decorator="[FormItem]"
:component="[
TreeSelect,
{
style: {
width: '200px',
},
},
]"
/>
<Submit @submit="log">提交</Submit>
</FormProvider>
</template>
<script>
import { createForm } from '@formily/core'
import { Field, FormProvider } from '@formily/vue'
import { FormItem, TreeSelect, Submit } from '@formily/antdv'
const form = createForm()
export default {
components: { FormProvider, Field, Submit },
data() {
return {
form,
FormItem,
TreeSelect,
}
},
methods: {
log(value) {
console.log(value)
},
},
}
</script>
# Template 异步联动数据源案例
<template>
<FormProvider :form="form">
<Field
name="linkage"
title="联动选择框"
:dataSource="[
{ label: '发请求1', value: 1 },
{ label: '发请求2', value: 2 },
]"
:decorator="[FormItem]"
:component="[
Select,
{
style: {
width: '200px',
},
},
]"
/>
<Field
name="select"
title="异步选择框"
:decorator="[FormItem]"
:component="[
TreeSelect,
{
style: {
width: '200px',
},
},
]"
/>
<Submit @submit="log">提交</Submit>
</FormProvider>
</template>
<script>
import { createForm, onFieldReact } from '@formily/core'
import { Field, FormProvider } from '@formily/vue'
import { FormItem, TreeSelect, Submit, Select } from '@formily/antdv'
import { action } from '@formily/reactive'
const useAsyncDataSource = (pattern, service) => {
onFieldReact(pattern, (field) => {
field.loading = true
service(field).then(
action.bound((data) => {
field.dataSource = data
field.loading = false
})
)
})
}
const form = createForm({
effects: () => {
useAsyncDataSource('select', async (field) => {
const linkage = field.query('linkage').get('value')
if (!linkage) return []
return new Promise((resolve) => {
setTimeout(() => {
if (linkage === 1) {
resolve([
{
label: 'AAA',
value: 'aaa',
children: [
{
title: 'Child Node1',
value: '0-0-0',
key: '0-0-0',
},
{
title: 'Child Node2',
value: '0-0-1',
key: '0-0-1',
},
{
title: 'Child Node3',
value: '0-0-2',
key: '0-0-2',
},
],
},
{
label: 'BBB',
value: 'ccc',
children: [
{
title: 'Child Node1',
value: '0-1-0',
key: '0-1-0',
},
{
title: 'Child Node2',
value: '0-1-1',
key: '0-1-1',
},
{
title: 'Child Node3',
value: '0-1-2',
key: '0-1-2',
},
],
},
])
} else if (linkage === 2) {
resolve([
{
label: 'CCC',
value: 'ccc',
children: [
{
title: 'Child Node1',
value: '0-0-0',
key: '0-0-0',
},
{
title: 'Child Node2',
value: '0-0-1',
key: '0-0-1',
},
{
title: 'Child Node3',
value: '0-0-2',
key: '0-0-2',
},
],
},
{
label: 'DDD',
value: 'ddd',
children: [
{
title: 'Child Node1',
value: '0-1-0',
key: '0-1-0',
},
{
title: 'Child Node2',
value: '0-1-1',
key: '0-1-1',
},
{
title: 'Child Node3',
value: '0-1-2',
key: '0-1-2',
},
],
},
])
}
}, 1500)
})
})
},
})
export default {
components: { FormProvider, Field, Submit },
data() {
return {
form,
FormItem,
TreeSelect,
Select,
}
},
methods: {
log(value) {
console.log(value)
},
},
}
</script>
# API
参考 https://antdv.com/components/tree-select-cn/ (opens new window)