# ArrayItems
自增列表,对于简单的自增编辑场景比较适合,或者对于空间要求高的场景比较适合
注意:该组件只适用于 Schema 场景
# Markup Schema 案例
<template>
<FormProvider :form="form">
<SchemaField>
<SchemaArrayField
name="string_array"
title="字符串数组"
x-decorator="FormItem"
x-component="ArrayItems"
>
<SchemaVoidField x-component="Space">
<SchemaVoidField
x-decorator="FormItem"
x-component="ArrayItems.SortHandle"
/>
<SchemaStringField
x-decorator="FormItem"
required
name="input"
x-component="Input"
:x-component-props="{
style: {
width: '160px',
},
}"
/>
<SchemaVoidField
x-decorator="FormItem"
x-component="ArrayItems.Remove"
/>
</SchemaVoidField>
<SchemaVoidField x-component="ArrayItems.Addition" title="添加条目" />
</SchemaArrayField>
<SchemaArrayField
name="array"
title="对象数组"
x-decorator="FormItem"
x-component="ArrayItems"
>
<SchemaObjectField>
<SchemaVoidField x-component="Space">
<SchemaVoidField
x-decorator="FormItem"
x-component="ArrayItems.SortHandle"
/>
<SchemaStringField
x-decorator="FormItem"
required
title="日期"
name="date"
x-component="DatePicker"
:x-component-props="{
type: 'daterange',
style: {
width: '160px',
},
}"
/>
<SchemaStringField
x-decorator="FormItem"
required
title="输入框"
name="input"
x-component="Input"
/>
<SchemaStringField
x-decorator="FormItem"
required
title="选择框"
name="select"
:enum="[
{ label: '选项1', value: 1 },
{ label: '选项2', value: 2 },
]"
x-component="Select"
:x-component-props="{
style: {
width: 160,
},
}"
/>
<SchemaVoidField
x-decorator="FormItem"
x-component="ArrayItems.Remove"
/>
</SchemaVoidField>
</SchemaObjectField>
<SchemaVoidField x-component="ArrayItems.Addition" title="添加条目" />
</SchemaArrayField>
<SchemaArrayField
name="array2"
title="对象数组"
x-decorator="FormItem"
x-component="ArrayItems"
:x-component-props="{ style: { width: '600px' } }"
>
<SchemaObjectField x-decorator="ArrayItems.Item">
<SchemaVoidField x-component="Space">
<SchemaVoidField
x-decorator="FormItem"
x-component="ArrayItems.SortHandle"
/>
<SchemaStringField
x-decorator="FormItem"
required
title="日期"
name="date"
x-component="DatePicker"
:x-component-props="{
type: 'daterange',
style: {
width: '250px',
},
}"
/>
<SchemaStringField
x-decorator="FormItem"
required
title="输入框"
name="input"
x-component="Input"
/>
<SchemaVoidField
x-decorator="FormItem"
x-component="ArrayItems.Remove"
/>
</SchemaVoidField>
</SchemaObjectField>
<SchemaVoidField x-component="ArrayItems.Addition" title="添加条目" />
</SchemaArrayField>
</SchemaField>
<FormButtonGroup>
<Submit @submit="log">提交</Submit>
</FormButtonGroup>
</FormProvider>
</template>
<script>
import { createForm } from '@formily/core'
import { FormProvider, createSchemaField } from '@formily/vue'
import {
FormItem,
FormButtonGroup,
Submit,
Input,
Select,
Space,
DatePicker,
ArrayItems,
} from '@formily/antdv'
import { Button } from 'ant-design-vue'
const SchemaField = createSchemaField({
components: {
FormItem,
Space,
Input,
Select,
DatePicker,
ArrayItems,
},
})
export default {
components: {
FormProvider,
FormButtonGroup,
Button,
Submit,
...SchemaField,
},
data() {
const form = createForm()
return {
form,
}
},
methods: {
log(values) {
console.log(values)
},
},
}
</script>
<style lang="scss" scoped></style>
# JSON Schema 案例
<template>
<FormProvider :form="form">
<SchemaField :schema="schema" />
<Submit @submit="log">提交</Submit>
</FormProvider>
</template>
<script>
import { createForm } from '@formily/core'
import { FormProvider, createSchemaField } from '@formily/vue'
import {
FormButtonGroup,
Submit,
FormItem,
Space,
Input,
Select,
DatePicker,
ArrayItems,
} from '@formily/antdv'
import { Button } from 'ant-design-vue'
const SchemaField = createSchemaField({
components: {
FormItem,
Space,
Input,
Select,
DatePicker,
ArrayItems,
},
})
export default {
components: {
FormProvider,
FormButtonGroup,
Button,
Submit,
...SchemaField,
},
data() {
const form = createForm()
const schema = {
type: 'object',
properties: {
string_array: {
type: 'array',
'x-component': 'ArrayItems',
'x-decorator': 'FormItem',
title: '字符串数组',
items: {
type: 'void',
'x-component': 'Space',
properties: {
sort: {
type: 'void',
'x-decorator': 'FormItem',
'x-component': 'ArrayItems.SortHandle',
},
input: {
type: 'string',
'x-decorator': 'FormItem',
'x-component': 'Input',
},
remove: {
type: 'void',
'x-decorator': 'FormItem',
'x-component': 'ArrayItems.Remove',
},
},
},
properties: {
add: {
type: 'void',
title: '添加条目',
'x-component': 'ArrayItems.Addition',
},
},
},
array: {
type: 'array',
'x-component': 'ArrayItems',
'x-decorator': 'FormItem',
title: '对象数组',
items: {
type: 'object',
properties: {
space: {
type: 'void',
'x-component': 'Space',
properties: {
sort: {
type: 'void',
'x-decorator': 'FormItem',
'x-component': 'ArrayItems.SortHandle',
},
date: {
type: 'string',
title: '日期',
'x-decorator': 'FormItem',
'x-component': 'DatePicker',
'x-component-props': {
type: 'daterange',
style: {
width: '250px',
},
},
},
input: {
type: 'string',
title: '输入框',
'x-decorator': 'FormItem',
'x-component': 'Input',
},
select: {
type: 'string',
title: '下拉框',
enum: [
{ label: '选项1', value: 1 },
{ label: '选项2', value: 2 },
],
'x-decorator': 'FormItem',
'x-component': 'Select',
'x-component-props': {
style: {
width: '250px',
},
},
},
remove: {
type: 'void',
'x-decorator': 'FormItem',
'x-component': 'ArrayItems.Remove',
},
},
},
},
},
properties: {
add: {
type: 'void',
title: '添加条目',
'x-component': 'ArrayItems.Addition',
},
},
},
array2: {
type: 'array',
'x-component': 'ArrayItems',
'x-decorator': 'FormItem',
'x-component-props': { style: { width: '600px' } },
title: '对象数组',
items: {
type: 'object',
'x-decorator': 'ArrayItems.Item',
properties: {
space: {
type: 'void',
'x-component': 'Space',
properties: {
sort: {
type: 'void',
'x-decorator': 'FormItem',
'x-component': 'ArrayItems.SortHandle',
},
date: {
type: 'string',
title: '日期',
'x-decorator': 'FormItem',
'x-component': 'DatePicker',
'x-component-props': {
type: 'daterange',
style: {
width: '250px',
},
},
},
input: {
type: 'string',
title: '输入框',
'x-decorator': 'FormItem',
'x-component': 'Input',
},
remove: {
type: 'void',
'x-decorator': 'FormItem',
'x-component': 'ArrayItems.Remove',
},
},
},
},
},
properties: {
add: {
type: 'void',
title: '添加条目',
'x-component': 'ArrayItems.Addition',
},
},
},
},
}
return {
form,
schema,
}
},
methods: {
log(values) {
console.log(values)
},
},
}
</script>
<style lang="scss" scoped></style>
# API
# ArrayItems
继承 HTMLDivElement Props
# ArrayItems.Item
列表区块
继承 HTMLDivElement Props
# ArrayItems.SortHandle
拖拽手柄
参考 https://antdv.com/components/button-cn/ (opens new window)
# ArrayItems.Addition
添加按钮
扩展属性
属性名 | 类型 | 描述 | 默认值 |
---|---|---|---|
title | string | 文案 | |
method | 'push' | 'unshift' | 添加方式 | 'push' |
defaultValue | any | 默认值 |
其余参考 https://antdv.com/components/button-cn/ (opens new window)
注意:title 属性可以接收 Field 模型中的 title 映射,也就是在 Field 上传 title 也是生效的
# ArrayItems.Remove
删除按钮
属性名 | 类型 | 描述 | 默认值 |
---|---|---|---|
title | string | 文案 |
其余参考 https://antdv.com/components/icon-cn/ (opens new window)
注意:title 属性可以接收 Field 模型中的 title 映射,也就是在 Field 上传 title 也是生效的
# ArrayItems.MoveDown
下移按钮
属性名 | 类型 | 描述 | 默认值 |
---|---|---|---|
title | string | 文案 |
其余参考 https://antdv.com/components/icon-cn/ (opens new window)
注意:title 属性可以接收 Field 模型中的 title 映射,也就是在 Field 上传 title 也是生效的
# ArrayItems.MoveUp
上移按钮
属性名 | 类型 | 描述 | 默认值 |
---|---|---|---|
title | string | 文案 |
其余参考 https://antdv.com/components/icon-cn/ (opens new window)
注意:title 属性可以接收 Field 模型中的 title 映射,也就是在 Field 上传 title 也是生效的
# ArrayItems.Index
索引渲染器
无属性
# ArrayItems.useIndex
读取当前渲染行索引的 Hook
# ArrayItems.useRecord
读取当前渲染记录的 Hook