TreeSelect 树形选择
含有下拉菜单的树形选择器
Markup Schema 案例
vue
<template>
<Form :form="form" @auto-submit="onSubmit">
<SchemaField>
<SchemaStringField
name="address"
title="异步数据源"
required
x-decorator="FormItem"
x-component="TreeSelect"
:x-component-props="{
style: {
width: '240px',
},
clearable: true,
filterable: true,
checkStrictly: true,
}"
/>
</SchemaField>
<Submit>提交</Submit>
</Form>
</template>
<script>
import { createForm, onFieldReact } from '@formily/core'
import { createSchemaField } from '@formily/vue'
import { Formily } from 'element-plus-x'
import { action } from '@formily/reactive'
import axios from 'axios'
const { TreeSelect, Form, FormItem, Submit } = Formily
const transformAddress = (data = {}) => {
return Object.entries(data).reduce((buf, [key, value]) => {
if (typeof value === 'string')
return buf.concat({
label: value,
value: key,
})
const { name, code, cities, districts } = value
const _cities = transformAddress(cities)
const _districts = transformAddress(districts)
return buf.concat({
label: name,
value: code,
children: _cities.length
? _cities
: _districts.length
? _districts
: undefined,
})
}, [])
}
const useAddress = (pattern) => {
onFieldReact(pattern, (field) => {
field.loading = true
axios('https://unpkg.com/china-location@2.1.0/dist/location.json')
.then((res) => res.data)
.then(
action.bound((data) => {
field.dataSource = transformAddress(data)
field.loading = false
})
)
})
}
const form = createForm({
effects: () => {
useAddress('address')
},
})
const fields = createSchemaField({
components: {
FormItem,
TreeSelect,
},
})
export default {
components: { Form, ...fields, Submit },
data() {
return {
form,
}
},
methods: {
onSubmit(value) {
console.log(value)
},
},
}
</script>
隐藏源代码
JSON Schema 案例
vue
<template>
<Form :form="form" label-width="120" @auto-submit="onSubmit">
<SchemaField :schema="schema" />
<Submit>提交</Submit>
</Form>
</template>
<script setup>
import { createForm } from '@formily/core'
import { createSchemaField } from '@formily/vue'
import { Formily } from 'element-plus-x'
const { TreeSelect, Form, FormItem, Submit } = Formily
const data = [
{
value: '1',
label: 'Level one 1',
disabled: true,
children: [
{
value: '1-1',
label: 'Level two 1-1',
disabled: true,
children: [
{
disabled: true,
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 renderContent = (h, { data }) => {
return h(
'span',
{
style: {
color: '#626AEF',
},
},
`${data.label}[${data.value}]`
)
}
const schema = {
type: 'object',
properties: {
select: {
type: 'string',
title: '同步数据源',
required: true,
enum: data,
'x-decorator': 'FormItem',
'x-component': 'TreeSelect',
'x-component-props': {
style: {
width: '320px',
},
},
},
select2: {
type: 'string',
title: '自定义内容',
required: true,
enum: data,
'x-decorator': 'FormItem',
'x-component': 'TreeSelect',
'x-component-props': {
style: {
width: '320px',
},
renderContent,
},
},
},
}
const form = createForm()
const { SchemaField } = createSchemaField({
components: {
FormItem,
TreeSelect,
},
})
const onSubmit = (value) => {
console.log(value)
}
</script>
隐藏源代码
Template 案例
vue
<template>
<Form :form="form" @auto-submit="onSubmit">
<Field
name="address"
title="懒加载"
required
:decorator="[FormItem]"
:component="[
TreeSelect,
{
style: {
width: '240px',
},
lazy: true,
load,
checkStrictly: true,
clearable: true,
},
]"
/>
<Submit>提交</Submit>
</Form>
</template>
<script>
import { createForm } from '@formily/core'
import { Field } from '@formily/vue'
import { Formily } from 'element-plus-x'
const { TreeSelect, Form, FormItem, Submit } = Formily
let id = 0
const load = (node, resolve) => {
if (node.isLeaf) return resolve([])
setTimeout(() => {
resolve([
{
value: ++id,
label: `lazy load node${id}`,
},
{
value: ++id,
label: `lazy load node${id}`,
isLeaf: true,
},
])
}, 400)
}
const form = createForm({
effects: () => {},
})
export default {
components: { Form, Field, Submit },
data() {
return {
FormItem,
TreeSelect,
form,
load,
}
},
methods: {
onSubmit(value) {
console.log(value)
},
},
}
</script>
隐藏源代码
API
参考 ElTreeSelect