对 element-ui 的 form 组件进行封装,扩展多个常用类型,例如:
- 输入框
- 选择器
- 多选框
- 日期选择
- 日期段选择
- 时间选择
- 计数器
- 开关
- 滑块
- 颜色选择
- 评分
- 级联
- 段落标题
- 富文本
- 表格
通过配置可灵活配置,并且与 ElProSearch 、 ElProTable 、 ElProAdanceSearch 的配置项有联动,可以共用同一份配置,非常方便
# 示例
# 典型表单
包括各种表单项,比如输入框、选择器、开关、单选框、多选框等。
复制代码
# 弹窗中的表单
很多情况下结合弹窗使用
defaultValue
可以设置默认值,isPreview
为预览模式,预览模板可以使用el-tag
渲染显示,使用isTag
和tagOptions
复制代码
# 行内表单
当垂直方向空间受限且表单较简单时,可以在一行内放置表单。
可以在columnOption
的style
里调整表单组件的样式
复制代码
# 表单联动
很多时候需要根据前面表单的值,动态显示后面的表单是否显示。如果想在隐藏后删掉当前值,可以使用watch
监听
复制代码
# 根据接口的值获取 options
很多时候需要根据接口的值获取 option
复制代码
# 对齐方式
根据具体目标和制约因素,选择最佳的标签对齐方式。
复制代码
# 表单验证
在防止用户犯错的前提下,尽可能让用户更早地发现并纠正错误。
如果只需要验证是否必填,只需要required
为true
就可以。
复制代码
# 自定义校验规则
这个例子中展示了如何使用自定义验证规则来完成密码的二次验证。
复制代码
# 表单类型
引入表单类型
import { types } from 'vue-element-pro-components/packages'
export const types = {
input: 'input', // 输入框
select: 'select', // 选择框
number: 'number', // 计数器
checkBox: 'checkBox', // 多选框
check: 'check', // 单个复选框
radio: 'radio', // 单选框
date: 'date', // 日期
switch: 'switch', // 开关
title: 'title', // 标题
time: 'time', // 时间
slider: 'slider', // 滑块
rate: 'rate', // 评分
color: 'color', // 颜色
cascader: 'cascader', // 级联
table: 'table', // 表格
editor: 'editor' // 富文本
}
# Attributes
参数 | 说明 | 类型 | 可选值 | 默认值 |
---|---|---|---|---|
v-model | 表单数据 | object | — | {} |
columns | 表单配置项 | [object, array] | — | {} |
formOptions | 官方支持的参数集合 | object | — | {} |
loading | 表单 loading | boolean | — | false |
isRow | 是否启用栅格布局 | boolean | — | true |
showNum | 搜索组件内部使用,显示可见的表单项个数 | [number, string] | — | 2 |
isCollapse | 搜索组件内部使用,是否开启展开收起 | boolean | — | false |
showAll | 搜索组件内部使用,是否展开收起 | boolean | — | true |
tableDialogOptions | 表单类型-table 的弹窗 options | object | — | {} |
tableFormOptions | 表单类型-table 的表单 options | object | — | {} |
tableOptions | 表单类型-table 的表格 options | object | — | {} |
rowOptions | el-row 配置 | object | — | { gutter: 10 } |
isSearch | 是否是搜索 | boolean | — | false |
isPreview | 是否预览 | boolean | — | false |
# Slot
name | 说明 |
---|---|
columnBefore | 表单内前面的内容 参数(form) |
columnAfter | 表单内后面的内容 参数(form) |
formBefore | 表单前面的内容 参数(form) |
formAfter | 表单后面的内容 参数(form) |
${key}All | 替换 form-item 的插槽 参数(form) |
${key} | 替换组件的插槽 参数(form) |
rowBefore | el-form-item 之前的内容 参数(form) |
rowAfter | el-form-item 之后的内容 参数(form) |
${key}Prefix | 同 input prefix 插槽 参数(form) |
${key}Suffix | 同 input suffix 插槽 参数(form) |
${key}Prepend | 同 input prepend 插槽 参数(form) |
${key}Append | 同 input append 插槽 参数(form) |
# Refs Events
# 表单内容重置
resetFields()
// 获取表单ref实例 调用
this.$refs.dataForm.resetFields()
# 表单内容验证
checkRule()
// 提交
async submitForm() {
const { dataForm } = this.$refs;
try {
const checkResult = await dataForm.checkRule();
if (checkResult) {
//...do something
}
} catch (error) {
console.log(error);
}
}
# column 配置项
此格式 form、search、table、advanceSearch 通用
总体格式为一个对象。类似
export function getMetaData() {
return {
key1: {},
key2: {}
}
}
使用一个函数来返回一个对象,优点是可以使用组件内部的this
。
只需要调用的时候这样:
import { getMetaData } from "./meta-data";
// 生命周期函数里初始化调用 传入组件this
created() {
this.columns = getMetaData.call(this)
},
name | 说明 | 类型 |
---|---|---|
label | 表单的 label | string |
tableLabel | 当 label 和表格中的显示不一致时,优先匹配 tableLabel | string |
searchLabel | 当 label 和搜索中的显示不一致时,优先匹配 searchLabel | string |
tooltip | form-item 后面的小提示 | string |
defaultValue | 默认值 | any |
type | 表单的 type,不填默认为 input | string |
required | 是否必填 | boolean |
rules | 检验数组 | array |
columnOption | 组件的内部 options,例如 maxlength,placeholder | object |
options | 下拉框/单选框/多选框的 options 默认[{name,value}]格式 | array/function(form) |
showInSearch | 是否显示在搜索框中 | boolean |
showInTable | 是否显示在表格中 | boolean |
showInAdvance | 是否显示在高级查询中 | boolean |
formItemOption | form-item 的 options,常用比如 label-width | object |
formColumnShow | 是否在表单里显示,接收一个函数或者布尔值,返回 boolean false 隐藏 | boolean/function(form) |
span | 栅格布局 比例 默认 18 一共 24 | number |
colSpanOption | el-col 的配置项 | object |
showFormat | 表格格式化,例如 select/radio 传的值为 1,2,3,渲染成 options 的 name | function(val, row, scope) |
tableColumnOption | 表格 el-column 的配置项,例如 width,showOverflowTooltip | object |
onChange | 表单的 change 回调事件 | function(item) |
copy | 表格内是否显示复制图标 | boolean |
isTag | 表格内是否用 el-tag 渲染 | boolean |
tagOptions | el-tag 的配置项,也可以直接写 tag 的类型 | string/object/function |