基于uniapp和uview1.0的简易表单生成器

基于uniapp和uview1.0的简易表单生成器

在新的项目中,有的表单项相同,那么能不能将表单form-item循环生成出来?只需要传递配置项与规则即可自动生成

说干就干

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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
​```
//表单Form.vue封装 基于uview1.0的input输入框 可以传递类型为text,textarea或者select下拉框
//其他类型需要使用插槽或者其他方式插入 可以绑定form数据
<template>
<view class="">
<view class="formbox">
<u-form :model="form" ref="uForm" label-width="150rpx">
<u-form-item
v-for="(item,index) in formItems"
:key="index"
:label="item.labelName"
:prop="item.prop"
:border-bottom="item.borderBottom"
:label-width="item.labelWidth"
:style="{'position' : item.prop === 'des' ? 'relative' : '' }"
>
<u-input
v-if="item.showInput"
v-model="form[item.prop]"
:placeholder="item.placeholder"
:type="item.type"
:border="item.border"
:height="item.height"
:class="item.class"
:maxlength="item.maxlength"
:custom-style="item.inputStyle ? item.inputStyle : null"
:clearable="false"
@click="clickChange(item)"/>
<view v-if="item.prop === 'fileList'" class="filebox">
<view class="title">
请上传附件({{ fileLength }}/5)
</view>
<view class="">
<u-upload
ref="uUpload"
:action="action"
:auto-upload="false"
max-count="5"
@on-list-change="imgChange"
></u-upload>
</view>
<view class="uploadtip">
<u-icon
name="info-circle"
color="#F7B500"
size="26"
style="margin-right: 12rpx;"
></u-icon>
附件支持图片和视频,支持jpg/png/mp4格式
</view>
</view>
<view class="countbox" v-if="item.prop === 'des'">
{{ form.des.length ? form.des.length : 0 }}/300
</view>
</u-form-item>
</u-form>
<u-picker
mode="time"
:params="params"
v-model="showList['timeshow']"
@confirm="timechange"
@cancel="timecancel"></u-picker>
<view v-if="methodsList.length">
<u-action-sheet
v-for="(item,index) in methodsList"
:key="index"
:list="paramsData[item.propList]"
v-model="showList[item.prop + 'show']"
@click="selectchange"
></u-action-sheet>
</view>
</view>
</view>
</template>

<script>
export default {
props:{
// 传递的下拉框数据
paramsData:{
type:Object,
default:()=>({})
},
rules:{
type:Object,
default:()=>({})
},
formItems:{
type:Array,
default:()=>([])
}
},
data(){
return {
// 数据绑定
form:{},
// 控制显示
showList:{},
params: {
year: true,
month: true,
day: true,
hour: true,
minute: true,
second: false
},
// 非真实地址
action: 'http://www.example.com/upload',
fileLength:0,
methodsList:[],
selectList:{}
}
},
watch:{},
onReady() {
this.formItems.forEach((items)=>{
this.$set(this.form,items.prop,'')
this.$set(this.showList,items.prop + 'show' ,false)
this.form.fileList = []
if(items.type === 'select'){
if(items.prop === 'time') return
const dataStr = items.prop + 'List'
const fnStr = items.prop + 'change'
this.methodsList.push({
prop:items.prop,
propList:dataStr
})
}
})
setTimeout(()=>{
this.$refs.uForm.setRules(this.rules);
},500)
},
methods:{
clickChange(item){
this.selectList = {
prop:item.prop,
formName:item.prop + 'List'
}
this.showList[item.prop + 'show'] = true
},
selectchange(index){
if(this.selectList){
this.form[this.selectList.prop] = this.paramsData[this.selectList.formName][index].text;
}
},
timechange(obj){
let timestr = Object.values(obj).join('-')
this.form.time = timestr
},
timecancel(){
this.show2 = false
},
// 上传图片变化
imgChange(lists,name){
this.fileLength = lists.length
this.form.fileList = lists
},
validateForm(){
let flag
this.$refs.uForm.validate(valid => {
if (valid) {
// 上传文件
// his.$refs.uUpload[0].upload()
flag = true
} else {
flag = false
}
})
return flag
},
}
}
</script>

<style lang="scss" scoped>
@import './index.scss'
</style>
//传入配置项 验证规则
export const rules = {
content: [
{
required: true,
message: '',
trigger: ['blur','change']
}
],
......
fileList:[
{
type:'array',
required: true,
message: '',
trigger: ['blur', 'change']
}
],
}

//表单配置
export const formItems = [
......
{
labelName:' ',
labelWidth:'0',
type:'textarea',
prop:'content',
placeholder:'默认文字',
height:'420',
class:'desbox',
border:false,
borderBottom:false,
maxlength:'300',
showInput:true,
inputStyle:{
'background': '#F5F5F5',
'border-radius':'8px',
'padding':'22rpx 26rpx'
}
}
]