ArrayItems 自增列表
表单自增,删除,上移,下移,拖拽排序。
基础用法
使用array-items
组件的default
插槽来动态渲染每一行
使用append
插槽来追加一些内容。
vue
<template>
<el-form :model="form" label-width="auto" style="max-width: 600px">
<el-form-item prop="list" label="字符串数组">
<el-array-items>
<template #default="{ $index }">
<div style="display: flex; margin-bottom: 8px">
<SortHandle />
<el-input v-model="form['list'][$index]" style="margin: 0 12px" />
<Remove />
<MoveDown />
<MoveUp />
</div>
</template>
<template #append>
<Addition title="添加" :default-value="''" />
</template>
</el-array-items>
</el-form-item>
</el-form>
<!-- <pre>{{ form.list }}</pre> -->
</template>
<script lang="ts" setup>
import { reactive } from 'vue'
import { ElArrayItems } from 'element-plus-x'
const { Addition, Remove, SortHandle, MoveDown, MoveUp } = ElArrayItems
const form = reactive({
list: ['1', '2', '3', '4', '5'],
})
</script>
隐藏源代码
对象数组
使用 from 表单组件的 v-model
绑定对应的 prop
vue
<template>
<el-form :model="form" label-width="auto">
<el-form-item prop="list" label="对象数组">
<el-array-items>
<template #default="{ $record }">
<div style="display: flex; margin-bottom: 8px">
<SortHandle />
<el-form-item label="输入框">
<el-input v-model="$record['input']" style="width: 180px" />
</el-form-item>
<el-form-item label="日期">
<el-date-picker
v-model="$record['date']"
type="date"
placeholder="选择日期"
/>
</el-form-item>
<el-form-item label="选择器">
<el-select-v2
v-model="$record['select']"
:options="options"
placeholder="请选择"
style="width: 180px"
/>
</el-form-item>
<Remove style="margin-left: 12px" />
<MoveDown />
<MoveUp />
</div>
</template>
<template #addition="{ field }">
<Addition
v-if="field.fieldValue.length < 5"
title="添加"
:default-value="{}"
/>
</template>
</el-array-items>
</el-form-item>
</el-form>
<!-- <pre>{{ form.list }}</pre> -->
</template>
<script lang="ts" setup>
import { reactive } from 'vue'
import { ElArrayItems } from 'element-plus-x'
const { Addition, Remove, SortHandle, MoveDown, MoveUp } = ElArrayItems
const form = reactive({
list: [
{
input: 'Summer',
date: '2025-01-15T16:00:00.000Z',
select: 'Option 1',
},
],
})
const initials = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
const options = Array.from({ length: 1000 }).map((_, idx) => ({
value: `Option ${idx + 1}`,
label: `${initials[idx % 10]}${idx}`,
}))
</script>
隐藏源代码
校验
使用 form-item
组件的 rules 属性即可,更多自定义校验,请参考官方文档
vue
<template>
<el-form ref="ruleFormRef" :model="form" label-width="auto">
<el-form-item prop="list">
<el-array-items>
<template #default="{ $index }">
<div style="display: flex; margin-bottom: 8px">
<SortHandle />
<el-form-item
label="输入框"
:prop="`list[${$index}]['input']`"
:rules="{
required: true,
message: 'input can not be null',
trigger: 'change',
}"
>
<el-input
v-model="form['list'][$index]['input']"
style="width: 180px"
placeholder="Please input"
/>
</el-form-item>
<el-form-item
label="日期"
:prop="`list[${$index}]['date']`"
:rules="{
required: true,
message: 'date can not be null',
trigger: 'change',
}"
>
<el-date-picker
v-model="form['list'][$index]['date']"
type="date"
placeholder="Please select"
/>
</el-form-item>
<el-form-item
label="选择器"
:prop="`list[${$index}]['select']`"
:rules="{
required: true,
message: 'select can not be null',
trigger: 'change',
}"
>
<el-select-v2
v-model="form['list'][$index]['select']"
:options="options"
placeholder="Please select"
style="width: 180px"
/>
</el-form-item>
<Remove style="margin-left: 12px" />
<MoveDown />
<MoveUp />
</div>
</template>
<template #addition="{ field }">
<Addition
v-if="field.fieldValue.length < 5"
title="添加"
:default-value="{}"
/>
</template>
</el-array-items>
</el-form-item>
<el-form-item>
<el-button type="primary" @click="submitForm(ruleFormRef)">
Create
</el-button>
<el-button @click="resetForm(ruleFormRef)">Reset</el-button>
</el-form-item>
</el-form>
<!-- <pre>{{ form.list }}</pre> -->
</template>
<script lang="ts" setup>
import { reactive, ref } from 'vue'
import { ElArrayItems } from 'element-plus-x'
import type { FormInstance } from 'element-plus'
const { Addition, Remove, SortHandle, MoveDown, MoveUp } = ElArrayItems
const ruleFormRef = ref<FormInstance>()
const form = reactive({
list: [
{
input: 'Summer',
date: '2025-01-15T16:00:00.000Z',
select: 'Option 1',
},
],
})
const initials = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
const options = Array.from({ length: 1000 }).map((_, idx) => ({
value: `Option ${idx + 1}`,
label: `${initials[idx % 10]}${idx}`,
}))
const resetForm = (formEl: FormInstance | undefined) => {
if (!formEl) return
formEl.resetFields()
}
const submitForm = async (formEl: FormInstance | undefined) => {
if (!formEl) return
await formEl.validate((valid, fields) => {
if (valid) {
console.log('submit!')
} else {
console.log('error submit!', fields)
}
})
}
</script>
隐藏源代码
禁用
使用 disable
属性可禁用全部
vue
<template>
<el-form :model="form" label-width="auto" disabled>
<el-form-item prop="list" label="对象数组">
<el-array-items>
<template #default="{ $record }">
<div style="display: flex; margin-bottom: 8px">
<SortHandle />
<el-form-item label="输入框">
<el-input v-model="$record['input']" style="width: 180px" />
</el-form-item>
<el-form-item label="日期">
<el-date-picker
v-model="$record['date']"
type="date"
placeholder="选择日期"
/>
</el-form-item>
<el-form-item label="选择器">
<el-select-v2
v-model="$record['select']"
:options="options"
placeholder="请选择"
style="width: 180px"
/>
</el-form-item>
<Remove style="margin-left: 12px" />
<MoveDown />
<MoveUp />
</div>
</template>
<template #addition="{ field }">
<Addition
v-if="field.fieldValue.length < 5"
title="添加"
:default-value="{}"
/>
</template>
</el-array-items>
</el-form-item>
</el-form>
<!-- <pre>{{ form.list }}</pre> -->
</template>
<script lang="ts" setup>
import { reactive } from 'vue'
import { ElArrayItems } from 'element-plus-x'
const { Addition, Remove, SortHandle, MoveDown, MoveUp } = ElArrayItems
const form = reactive({
list: [
{
input: 'Summer1',
date: '2025-01-15T16:00:00.000Z',
select: 'Option 1',
},
{
input: 'Summer2',
date: '2025-01-15T16:00:00.000Z',
select: 'Option 2',
},
{
input: 'Summer3',
date: '2025-01-15T16:00:00.000Z',
select: 'Option 3',
},
],
})
const initials = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
const options = Array.from({ length: 1000 }).map((_, idx) => ({
value: `Option ${idx + 1}`,
label: `${initials[idx % 10]}${idx}`,
}))
</script>
隐藏源代码
Index
Index
序号
可以使用formatter
来格式化展示内容
vue
<template>
<el-form :model="form" label-width="auto">
<el-form-item prop="list">
<el-array-items>
<template #default="{ $index }">
<div style="display: flex; margin-bottom: 8px">
<Index />
<Index
style="margin: 0 10px"
:formatter="(index) => String(index + 1)"
/>
<el-input v-model="form['list'][$index]" style="margin: 0 12px" />
<Remove />
<MoveDown />
<MoveUp />
</div>
</template>
<template #append>
<Addition title="添加" :default-value="''" />
</template>
</el-array-items>
</el-form-item>
</el-form>
<!-- <pre>{{ form.list }}</pre> -->
</template>
<script lang="ts" setup>
import { reactive } from 'vue'
import { ElArrayItems } from 'element-plus-x'
const { Index, Addition, Remove, MoveDown, MoveUp } = ElArrayItems
const form = reactive({
list: ['1', '2', '3', '4', '5'],
})
</script>
隐藏源代码
Addition
Addition
组件本质就是el-button
组件的扩展,你可以当作 el-button 组件来使用。
title
method
defaultValue
属性是 Addition
组件的扩展属性
vue
<template>
<el-form :model="form" label-width="auto">
<el-form-item prop="list">
<el-array-items>
<template #default="{ $index }">
<div style="display: flex; margin-bottom: 8px">
<SortHandle />
<el-input v-model="form['list'][$index]" style="margin: 0 12px" />
<Remove />
<MoveDown />
<MoveUp />
</div>
</template>
<template #append>
<Addition style="width: auto" type="primary" plain :icon="CirclePlus">
添加条目
</Addition>
</template>
</el-array-items>
</el-form-item>
</el-form>
<!-- <pre>{{ form.list }}</pre> -->
</template>
<script lang="ts" setup>
import { reactive } from 'vue'
import { ElArrayItems } from 'element-plus-x'
import { CirclePlus } from '@element-plus/icons-vue'
const { Addition, Remove, SortHandle, MoveDown, MoveUp } = ElArrayItems
const form = reactive({
list: ['1', '2', '3'],
})
</script>
隐藏源代码
Remove
Remove
组件本质就是el-button
组件的扩展,你可以当作 el-button 组件来使用。
title
属性是 Addition
组件的扩展属性
vue
<template>
<el-form :model="form" label-width="auto">
<el-form-item prop="list">
<el-array-items>
<template #default="{ $index }">
<div style="display: flex; margin-bottom: 8px">
<SortHandle />
<el-input v-model="form['list'][$index]" style="margin: 0 12px" />
<Remove link type="danger" size="small" icon> Remove </Remove>
<Remove type="danger" :icon="Delete" :link="false" />
<Remove :link="false" size="small" type="danger" icon>
Delete
</Remove>
</div>
</template>
<template #append>
<Addition title="添加" :default-value="''" />
</template>
</el-array-items>
</el-form-item>
</el-form>
<!-- <pre>{{ form.list }}</pre> -->
</template>
<script lang="ts" setup>
import { reactive } from 'vue'
import { ElArrayItems } from 'element-plus-x'
import { Delete } from '@element-plus/icons-vue'
const { Addition, Remove, SortHandle } = ElArrayItems
const form = reactive({
list: ['1', '2', '3'],
})
</script>
隐藏源代码
MoveDown/MoveUp
MoveDown
和 MoveUp
组件本质就是el-button
组件的扩展,你可以当作 el-button 组件来使用。
title
属性是 MoveDown
和 MoveUp
组件的扩展属性
vue
<template>
<el-form :model="form" label-width="auto">
<el-form-item prop="list">
<el-array-items>
<template #default="{ $index }">
<div style="display: flex; margin-bottom: 8px">
<SortHandle />
<el-input v-model="form['list'][$index]" style="margin: 0 12px" />
<Remove />
<MoveDown :icon="SortDown" />
<MoveUp :icon="SortUp" />
</div>
</template>
<template #append>
<Addition title="添加" :default-value="''" />
</template>
</el-array-items>
</el-form-item>
</el-form>
<!-- <pre>{{ form.list }}</pre> -->
</template>
<script lang="ts" setup>
import { reactive } from 'vue'
import { ElArrayItems } from 'element-plus-x'
import { SortDown, SortUp } from '@element-plus/icons-vue'
const { Addition, Remove, SortHandle, MoveDown, MoveUp } = ElArrayItems
const form = reactive({
list: ['1', '2', '3'],
})
</script>
隐藏源代码
SortHandle
SortHandle
组件本质就是el-button
组件的扩展,你可以当作 el-button 组件来使用。
vue
<template>
<el-form :model="form" label-width="auto">
<el-form-item prop="list">
<el-array-items>
<template #default="{ $index }">
<div style="display: flex; margin-bottom: 8px">
<SortHandle color="#626aef" plain />
<el-input v-model="form['list'][$index]" style="margin: 0 12px" />
<Remove />
<MoveDown />
<MoveUp />
</div>
</template>
<template #append>
<Addition title="添加" :default-value="''" />
</template>
</el-array-items>
</el-form-item>
</el-form>
<!-- <pre>{{ form.list }}</pre> -->
</template>
<script lang="ts" setup>
import { reactive } from 'vue'
import { ElArrayItems } from 'element-plus-x'
const { Addition, Remove, SortHandle, MoveDown, MoveUp } = ElArrayItems
const form = reactive({
list: ['1', '2', '3', '4', '5'],
})
</script>
隐藏源代码
API
ArrayItems Slots
插槽名 | 说明 | 参数 |
---|---|---|
default | 动态循环遍历的内容 | { field:FormItemContext, $index: number, $record: any } |
append | 前置插槽 | { field:FormItemContext } |
addition | 比如放置一些添加按钮插槽 | { field:FormItemContext } |
append | 后置插槽 | { field:FormItemContext } |
Index
属性名 | 说明 | 类型 | 默认值 |
---|---|---|---|
index | 索引值 | number | - |
formatter | 格式化的内容 | formatter?: (index: number) => string | - |
Addition
属性名 | 说明 | 类型 | 默认值 |
---|---|---|---|
title | 按钮内容 | string | - |
method | 数组前置插入还是后置插入 | enum | push |
defaultValue | 往数组里面插入的值 | any | - |
el-button | 属性,事件,插槽 | - | - |
Remove
属性名 | 说明 | 类型 | 默认值 |
---|---|---|---|
title | 按钮内容 | string | - |
el-button | 属性,事件,插槽 | - | - |
MoveDown/MoveUp
属性名 | 说明 | 类型 | 默认值 |
---|---|---|---|
title | 按钮内容 | string | - |
el-button | 属性,事件,插槽 | - | - |
SortHandle
属性名 | 说明 | 类型 | 默认值 |
---|---|---|---|
el-button | 属性,事件,插槽 | - | - |