介绍

DragForm (拖拽表单)

开发初心就是为了减少工作量 提供高扩展性的表单组件 免费版本也支持扩展组件

安装

npm i element-easy-form element-plus  --save
1

安装异常更换npm镜像仓库

推荐 tencent

  npm ---------- https://registry.npmjs.org/
  yarn --------- https://registry.yarnpkg.com/
  tencent ------ https://mirrors.cloud.tencent.com/npm/
  cnpm --------- https://r.cnpmjs.org/
  taobao ------- https://registry.npmmirror.com/
  npmMirror ---- https://skimdb.npmjs.com/registry/

1
2
3
4
5
6
7
import { createApp } from 'vue'

//表单设计器
import elementEasyForm from 'element-easy-form'
import 'element-easy-form/dist/style.css'
//element ui
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'

//vue3.0 
import App from './App.vue'
const app = createApp(App);
app.use(elementEasyForm)
app.use(ElementPlus)
app.mount('#app')


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

DragForm 参数

属性名说明数据类型默认值
customoComponents自定义组件DragFormType[]null
modelValue原始数据DragFormType[][]

DragForm 事件

事件名说明参数
save保存原始数据原始数据

DragForm Exposes

事件名说明参数
getData回调函数返回 原始数据回调函数返回 原始数据

ElementFormRender 参数

属性名说明数据类型默认值
formJson表单json数据DragFormType[]参考下面案例
model提交表单数据object参考下面案例
formAttrsform表单属性 同element-plusobject参考下面案例

ElementFormRender Exposes

事件名说明参数
validate触发表单校验参考下面案例

扩展自定义组件

DragFormType 接口

属性名说明数据类型默认值
label与 el-form-item labelString""
prop与 el-form-item propString""
formType生成数据类型 DragForm或者ElementEasyFormStringJSONType
type数据类型StringAttrType
title组件名字String参考下面案例
formItemAttrsel-form-item 属性Object参考下面案例
attrs组件属性Object参考下面案例
componentName组件名字String参考下面案例
attrsJson组件属性选项DragFormType[] 数据类型参考下面案例
rules与 el-form-item rulesObject[] 数据类型[]
hidden是否展示隐藏布尔值BooleanDragFormData接口
children子组件DragFormType[]参考下面案例

BooleanDragFormData接口

名字数据类型说明
matchPatternstring匹配模式
typestring如下SelectFuntion
value布尔值false 显示 true 隐藏
dataSelect数组选择器返回数据 一般不用
dataJs函数参数 (config:FormItemJSON,data:any) 、返回值 布尔值 config当前组件数据 data表单数据对象

SelectFuntion枚举

名字数据类型说明
SELECTstring页面配置适用
FUNCTIONstring手写js函数

JSONType枚举

组件数据需要

名字数据类型说明
DRAGFORMstring适用drag-form组件
ELEMENTEASYFORMstring适用element-easy-form组件
ALLstring以上都适用

AttrType枚举

名字数据类型说明
FORMITEMstring只有el-form-item 属性需要 自定义组件一般不需要
COMPONENTstring当前数据是组件类型
LAYOUTstring当前数据是布局容器类型

接口、枚举

函数名字说明默认值
ComponentType枚举 组件名字 内置的 element-plus组件名字参考下面案例
DragFormType接口参考下面案例

自定义组件演示案例

案例源代码open in new window

配置文件 custom.js

import {
    elFormItem,
    elFormplaceholder,
    AttrType,
    ComponentType,
    JSONType,
    compontent,
  } from "element-easy-form";
  
  const attrsJson = [
    {
      label: "类型",
      prop: "type",
      type: AttrType.COMPONENT,
      defaultValue: "text",
  
      componentName: ComponentType.ElSelect,
      attrs: {
        placeholder: "请选择类型",
        clearable: true,
      },
  
      rules: [{ required: true, message: "类型不能为空", trigger: "change" }],
      children: [
        {
          componentName: ComponentType.ElOption,
          value: "text",
          label: "文本框",
        },
        {
          componentName: ComponentType.ElOption,
          label: "密码框",
          value: "password",
        },
        {
          componentName: ComponentType.ElOption,
          label: "文本域",
          value: "textarea",
        },
        {
          componentName: ComponentType.ElOption,
          label: "数字",
          value: "number",
        },
      ],
    },
    ...elFormItem({
      label: "客户",
      prop: "customer",
    }),
    {
      type: AttrType.COMPONENT,
      label: "默认值",
      prop: "defaultValue",
  
      defaultValue: "",
      componentName: ComponentType.ElInput,
      attrs: {
        placeholder: "请输入",
      },
      rules: [{ required: false, message: "", trigger: "change" }],
    },
    elFormplaceholder("请输入"),
    {
      type: AttrType.COMPONENT,
      label: "最大输入长度",
      prop: "maxlength",
  
      defaultValue: "",
      componentName: ComponentType.ElInput,
      attrs: {
        placeholder: "请输入",
      },
      rules: [{ required: false, message: "名字不能为空", trigger: "change" }],
    },
    {
      type: AttrType.COMPONENT,
      label: "最小输入长度",
      prop: "minlength",
  
      componentName: ComponentType.ElInput,
      defaultValue: "",
      attrs: {
        placeholder: "请输入",
      },
      rules: [{ required: false, message: "名字不能为空", trigger: "change" }],
    },
  ];
  
  export const custom = [
    compontent,
    {
      title: "主表字段",
      attrs: {},
      prop: "container",
      type: AttrType.LAYOUT,
      children: [
        {
          label: "选择客户",
          formType: JSONType.ALL,
          type: AttrType.COMPONENT,
          title: "选择客户",
          formItemAttrs: {},
          attrs: {
            placeholder: "请输入",
          },
          rules: [],
          prop: "",
          attrsJson: attrsJson,
          componentName: ComponentType.ElInput, // 组件名称,用于生成代码
        },
      ],
    },
  ];

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115

设计表单demo

<template>
    <dragForm :customoComponents="customoComponents"></dragForm>
</template>

<script>
import {custom} from './custom'
import {componentList} from "element-easy-form";
export default {
  name: 'App',
  components: {
  },
  data(){
    return {
      customoComponents:[
  componentList[0],
  ...custom
]
    }
  }
}
</script>

<style>
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}
</style>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33

渲染表单demo

<template>
  <div class="p20 h100">
   <ElementFormRender
    ref="elementFormrenderRef"
    :formJson="dragFormJson.formJson"
     :formAttrs="dragFormJson.formAttrs"
    :model="dragFormJson.model"
    ></ElementFormRender>

<el-button @click="handlePost">提交</el-button> 

  </div>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from "vue";
const elementEasyFormRef = ref<any>();
const elementFormrenderRef = ref<any>();
const handlePost =async () =>{
  const data =await elementFormrenderRef.value.validate()
  console.log('data :>> ', data);
}
const dragFormJson = ref({
  "formJson": [
    {
      "attrs": {
        "gutter": 0,
        "justify": "start",
        "align": "middle"
      },
      "type": "LAYOUT",
      "formType": "all",
      "componentName": "ElRow",
      "children": [
        {
          "type": "LAYOUT",
          "componentName": "ElCol",
          "attrs": {
            "span": 12,
            "offset": 0,
            "push": 0,
            "pull": 0
          },
          "children": [
            {
              "label": "阎娜",
              "type": "COMPONENT",
              "formItemAttrs": {},
              "formType": "all",
              "attrs": {
                "placeholder": "请输入",
                "type": "text"
              },
              "rules": [],
              "prop": "JessicaRobertHarris",
              "componentName": "ElInput",
              "hidden": {
                "matchPattern": "&&",
                "type": "select",
                "dataSelect": [],
                "value": false,
                "dataJs": "function hidden(config,data){\n      return false;\n    }"
              }
            }
          ]
        },
        {
          "type": "LAYOUT",
          "componentName": "ElCol",
          "attrs": {
            "span": 12,
            "offset": 0,
            "push": 0,
            "pull": 0
          },
          "children": [
            {
              "label": "史秀英",
              "type": "COMPONENT",
              "formItemAttrs": {},
              "formType": "all",
              "attrs": {
                "placeholder": "请输入",
                "type": "text"
              },
              "rules": [],
              "prop": "NancyDonnaTaylor",
              "componentName": "ElInput",
              "hidden": {
                "matchPattern": "&&",
                "type": "select",
                "dataSelect": [],
                "value": false,
                "dataJs": "function hidden(config,data){\n      return false;\n    }"
              }
            }
          ]
        }
      ],
      "hidden": {
        "matchPattern": "&&",
        "type": "select",
        "dataSelect": [],
        "value": false,
        "dataJs": "function hidden(config,data){\n      return false;\n    }"
      }
    }
  ],
  "model": {
    "JessicaRobertHarris": "",
    "NancyDonnaTaylor": ""
  },
  "formAttrs": {
    "hide-required-asterisk": false,
    "inline-message": false,
    "status-icon": false,
    "label-width": "150px",
    "scroll-to-error": false
  }
});
const count = ref(0);
const state: any = reactive({
  activeDate: "",
});
const handleScopeOf = (e: any) => {
  console.log("ehandleScopeOf :>> ", e.next());
};


</script>

<style scoped lang="scss">


</style>


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137

ElementEasyForm (二开表单)

demo

<template>
<div class="p20">
   <ElementEasyForm ref="elementEasyFormRef" :formJson="formJson"></ElementEasyForm>
</div>
  
</template>

<script setup lang="ts">
import {ref,reactive,onMounted} from 'vue';
import {useRouter,useRoute} from 'vue-router'
import {ElementEasyForm} from 'element-easy-form';
const elementEasyFormRef = ref<any>();  

const formJson = ref<any>({
  "rowAttrs": {
    "gutter": 0,
    "justify": "start",
    "align": "middle"
  },
  "schema": [
    {
      "label": "蒋伟",
      "type": "COMPONENT",
      "formType": "all",
      "formItemAttrs": {},
      "attrs": {
        "defaultValue": []
      },
      "rules": [],
      "prop": "MaryEricGarcia",
      "componentName": "ElCheckboxGroup",
      "children": [
        {
          "componentName": "ElCheckbox",
          "label": "选项1"
        }
      ],
      "hidden": {
        "matchPattern": "&&",
        "type": "select",
        "dataSelect": [],
        "value": false,
        "dataJs": "function hidden(config,data){\n      return false;\n    }"
      },
      "colAttrs": {
        "span": 12,
        "offset": 0,
        "push": 0,
        "pull": 0
      }
    },
    {
      "prop": "BrendaKarenClark",
      "label": "孔刚",
      "formType": "all",
      "type": "COMPONENT",
      "attrs": {
        "placeholder": "请选择",
        "multiple": false,
        "disabled": false,
        "clearable": false,
        "collapse-tags": false,
        "multiple-limit": 0
      },
      "formItemAttrs": {},
      "rules": [],
      "componentName": "ElSelect",
      "children": [
        {
          "componentName": "ElOption",
          "value": "1",
          "label": "选项1"
        }
      ],
      "hidden": {
        "matchPattern": "&&",
        "type": "select",
        "dataSelect": [],
        "value": false,
        "dataJs": "function hidden(config,data){\n      return false;\n    }"
      },
      "colAttrs": {
        "span": 12,
        "offset": 0,
        "push": 0,
        "pull": 0
      }
    }
  ],
  "model": {
    "MaryEricGarcia": [],
    "BrendaKarenClark": ""
  },
  "formAttrs": {
    "hide-required-asterisk": false,
    "inline-message": false,
    "status-icon": false,
    "scroll-to-error": false
  }
});
 const count =ref(0);
 const state:any = reactive({
   activeDate:""
 })
 const handleScopeOf = (e:any)=>{
  console.log('ehandleScopeOf :>> ', e.next());
  
}


</script>

<style  scoped lang="scss" >

</style>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116

ElementEasyForm formJson 属性

属性名数据类型说明
schemaArray定义表单的schema,格式如下:schema
modelObject表单数据 于element-ui的model一致
formAttrsObject表单组件el-form attrs 一样
rowAttrsObject表单组件el-row attrs 一样

schema 属性

属性名数据类型说明
labelstring标签文本
propstringmodel 的键名。 它可以是一个属性的值(如 a.b.0 或 [a', 'b', '0'])。 在使用了 validate、resetFields 的方法时,该属性是必填的。
componentNamestringelement-ui组件名字,如 el-input el-select el-date-picker
attrsObject表单组件 属性,如 el-input el-select el-date-picker
colAttrsObjectel-col 属性
formItemAttrsObjectformItem 属性
rules表单校验规则
rendertsxtsx数据渲染函数
renderLabeltsxtsx数据渲染函数
eventsFunction表单组件 事件
children子集比如选择器组件

ElementEasyForm Solts

方法名说明
up动态表单顶部 el-form子标签
under动态表单底部 el-form子标签

ElementEasyFormItem Solts

方法名说明
prop+'Label'自定义组件
prop自定义组件

ElementEasyForm Exposes

方法名数据类型说明
validateFunction验证表单数据

需要源码 联系方式

QQ

微信