vue element form 封装
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 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 | <template> <el-row :gutter= "$attrs.gutter" > {{ formData }} <el-form v-bind= "$attrs" v-on= "$listeners" :label-width= "$attrs.labelWidth || '140px'" > <template v- for = "item in formHeader" > <el-col :key= "item.prop" v- if = "!item.hidden" :span= "item.span || allSpan" : class = "item.right ? 'flexAlignCenterRight' : ''" > <el-form-item v-bind= "item" :rules= " item.required ? [ { required: true, message: `${item.label.replace(/\(.*?\)/g, '')}必填`, trigger: 'blur', }, ] : null " v-required= " item.dir === true || (item.required && item.dir !== false) ? formData[item.prop] : '1' " > <!-- 渲染函数 --> <template v- if = "item.render" > <ex-slot :render= "item.render" :row= "formData" /> </template> <!-- 上传 --> <template v- else - if = "item.type === 'upload'" > <el-upload ref= "upload" style= "width: 200px" :file-list= "field101fileList" :action= "field101Action" :multiple= "item.multiple || false" :before-upload= "field101BeforeUpload" :accept= "item.accept || '*'" :disabled= "item.disabled || false" > <el-button size= "small" type= "primary" icon= "el-icon-upload" >点击上传</el-button > <div slot= "tip" class = "el-upload__tip" > {{ item.upload__tip }} </div> </el-upload> </template> <!-- 日期范围选择器 --> <template v- else - if = "item.type === 'daterange'" > <el-date-picker type= "daterange" v-model= "formData[item.prop]" v-bind= "item" :format= "item.format || 'yyyy-MM-dd'" :value-format= "item.valueFormat || 'yyyy-MM-dd'" :start-placeholder= "item.startPlaceholder || '开始日期'" :end-placeholder= "item.endPlaceholder || '结束日期'" :range-separator= "item.rangeSeparator || '至'" ></el-date-picker> </template> <!-- 日期选择器 --> <template v- else - if = "item.type === 'date'" > <el-date-picker v-model= "formData[item.prop]" :format= "item.format || 'yyyy-MM-dd'" :value-format= "item.valueFormat || 'yyyy-MM-dd'" v-bind= "item" ></el-date-picker> </template> <!-- 时间选择器 --> <template v- else - if = "item.type === 'timerange'" > <el-time-picker v-model= "formData[item.prop]" v-bind= "item" is-range :format= "item.format || 'HH:mm:ss'" :value-format= "item.valueFormat || 'HH:mm:ss'" :start-placeholder= "item.startPlaceholder || '开始时间'" :end-placeholder= "item.endPlaceholder || '结束时间'" :range-separator= "item.rangeSeparator || '至'" ></el-time-picker> </template> <!-- 时间选择器 --> <template v- else - if = "item.type === 'time'" > <el-time-picker v-model= "formData[item.prop]" :format= "item.format || 'HH:mm:ss'" :value-format= "item.valueFormat || 'HH:mm:ss'" :picker-options= "{ selectableRange: '00:00:00-23:59:59' }" v-bind= "item" ></el-time-picker> </template> <!-- 下拉 --> <template v- else - if = "item.type === 'select'" > <el-select v-model= "formData[item.prop]" v-bind= "item" :multiple= "item.multiple || false" :filterable= "item.filterable || true" :disabled= "item.disabled || false" > <el-option v- for = "(el, i) in item.options" :key= "i" :label= "el.label" :value= "el.value" :disabled= "el.disabled" > </el-option> </el-select> </template> <!-- 单选 --> <template v- else - if = "item.type === 'radio'" > <el-radio-group v-model= "formData[item.prop]" v-bind= "item" :disabled= "item.disabled || false" > <el-radio v- for = "(el, i) in item.options" :key= "i" :label= "el.value" :disabled= "el.disabled" :border= "el.border" > {{ el.label }} </el-radio> </el-radio-group> </template> <!-- 滑块 --> <template v- else - if = "item.type === 'switch'" > <el- switch style= "width: 200px" v-model= "formData[item.prop]" :active-text= "item.activeText" :inactive-text= "item.inactiveText" :active-color= "item.activeColor ? item.activeColor : '#13ce66'" :inactive-color= " item.inactiveColor ? item.inactiveColor : '#ff4949' " :disabled= "item.disabled || false" ></el- switch > </template> <!-- 多选 --> <template v- else - if = "item.type === 'checkbox'" > <el-checkbox-group v-model= "formData[item.prop]" v-bind= "item" :disabled= "item.disabled || false" :min= "item.min" :max= "item.max" > <el-checkbox v- for = "(el, i) in item.options" :key= "i" :label= "el.value" :disabled= "el.disabled" :border= "el.border" > {{ el.label }} </el-checkbox> </el-checkbox-group> </template> <!-- 输入型 --> <!-- 多行 考虑加入富文本--> <template v- else - if = "item.type === 'textarea'" > <el-input type= "textarea" v-model.trim= "formData[item.prop]" v-bind= "item" > <template slot= "prepend" v- if = "item.prepend" >{{ item.prepend }}</template> <template slot= "append" v- if = "item.append" >{{ item.append }}</template> </el-input> </template> <!-- 计数 --> <template v- else - if = "item.type === 'number' || item.type === 'price'" > <!-- {{item}}v-model.trim= "formData[item.prop]" --> <!-- <el-input :value= "formatterValue(item)" @input= "change($event,item)" @change= "change($event,item)" ></el-input> --> <el-input-number v-model.trim= "formData[item.prop]" :controls= "item.controls || false" :step= "item.step || 0.01" v-bind= "commonProp(item)" :min= " item.min !== null && item.min !== undefined ? item.min : 0.0 " :precision= " item.precision !== null && item.precision !== undefined ? item.precision : 2 " :max= " item.max !== null && item.max !== undefined ? item.max : 100000000 " :step-strictly= "item.stepStrictly || true" ></el-input-number> </template> <!-- 单行 --> <template v- else > <el-input v-model.trim= "formData[item.prop]" v-bind= "item" :prefix-icon= "item.prefixIcon" :suffix-icon= "item.suffixIcon" style= "width: 200px" > <template slot= "prepend" v- if = "item.prepend" >{{ item.prepend }}</template> <template slot= "append" v- if = "item.append" >{{ item.append }}</template> </el-input> </template> </el-form-item> </el-col> </template> </el-form> </el-row> </template> <script> // 自定义内容的组件 let exSlot = { functional: true , props: { row: Object, render: Function, }, render: (h, data) => { const params = { row: data.props.row, }; return data.props.render(h, params); }, }; export default { name: "form-default" , inheritAttrs: false , components: { exSlot }, props: { //所有col统一栅格 allSpan: { type: Number, default : 12, }, //所有控件统一宽度 allWidth: { type: [String, Number], default : "200px" , }, formData: { type: Object, default : function () { return {}; }, }, // 表头数据 formHeader: { type: Array, default : function () { return []; }, }, }, computed: { // formatterValue(e) { // // if (this.formatter && this.precisionValue !== null) { // // return this.formatter(this.precisionValue); // // } else { // // return this.precisionValue; // // } // console.log(e.target); // // return 55 // }, }, data() { return { field101Action: "https://jsonplaceholder.typicode.com/posts/" , field101Action: "#" , field101fileList: [], }; }, // computed: { // comHeader() { // return this.formHeader; // }, // }, created() { this .formHeader.forEach((item) => { if ( item.format === "array" && Object.prototype.toString.call( this .formData[item.prop]) !== "[object Array]" ) { this .$set( this .formData, item.prop, []); } }); }, methods: { formatterValue(item){ if (item.formatter){ return item.formatter( this .formData[item.prop]) } else { return this .formData[item.prop] } // console.log(item.formatter(50000)); // console.log(this.change); // return item.formatter(this.formData[item.prop]) }, change(event,item) { if (item.parser){ this .formData[item.prop]=item.parser(event) } else { this .formData[item.prop]=event } }, validate() { return this .$refs.elForm.validate(); }, clearValidate() { this .$refs.elForm.clearValidate(); }, proppath(row, path) { let arr = path.split( "." ); let a = row; arr.forEach((item) => { a = a[item]; }); return a; }, field101BeforeUpload(file) { let isRightSize = file.size / 1024 / 1024 < 2; if (!isRightSize) { this .$message.error( "文件大小超过 2MB" ); } console.log(file.type); let isAccept = new RegExp( "application" ).test(file.type); if (isAccept) { this .$message.error( "应该选择非.exe等类型的文件" ); } return isRightSize && isAccept; }, }, }; </script> <style lang= "scss" scoped> ::v-deep .el-input-number .el-input__inner { text-align: left; } </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 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 | <template> <FormDefault :formData= "info" :formHeader= "defaultHeader" :rules= "rules" class = "mt10" ref= "elForm" :model= "info" size= "mini" ></FormDefault> </template> <script> import FormDefault from "./form.vue" ; export default { components: { FormDefault }, data() { return { info: { f1: 12, }, rules: { f1: [{ required: true }], }, defaultHeader: [ { prop: "f0" , label: "评测预算费(元)" , placeholder: "请输5入金额" , type: "upload" , required: true , render: (h, params) => { return [<div>4545</div>]; }, }, { prop: "f1" , label: "评测预算费(元)" , placeholder: "请输5入金额" , type: "number" , formatter: (value) => `${value}`.replace(/\B(?=(\d{3})+(?!\d))/g, "," ), parser: (value) => `${value}`.replace(/$s?|(,*)/g, "" ), required: true , }, { prop: "f2" , label: "研发预算费(元)" , placeholder: "请输入金额" , type: "daterange" , }, { prop: "f3" , label: "专利预算费(元)" , placeholder: "请输入金额" , type: "date" , }, { prop: "f4" , label: "专家验收预算费(元)" , placeholder: "请输入金额" , type: "timerange" , }, { prop: "f53" , label: "采购预算费(元)" , placeholder: "请输入金额" , type: "time" , }, { prop: "f54" , label: "实施预算费(元))" , placeholder: "请输入金额" , type: "select" , multiple: true , options: [ { value: "选项1" , label: "黄金糕" , }, { value: "选项2" , label: "双皮奶" , }, { value: "选项3" , label: "蚵仔煎" , }, ], }, { prop: "f5" , label: "其他预算费(元)" , placeholder: "请输入金额" , type: "radio" , options: [ { value: "选项1" , label: "黄金糕" , }, { value: "选项2" , label: "双皮奶" , }, { value: "选项3" , label: "蚵仔煎" , }, ], }, { prop: "f6" , label: "论文预算费(元)" , placeholder: "请输入金额" , type: "switch" , }, { prop: "f7" , label: "论文预算费(元)" , placeholder: "请输入金额" , type: "checkbox" , format: "array" , options: [ { value: "选项1" , label: "黄金糕" , }, { value: "选项2" , label: "双皮奶" , }, { value: "选项3" , label: "蚵仔煎" , }, ], }, { prop: "f8" , label: "论文预算费(元)" , placeholder: "请输入金额" , type: "textarea" , }, { prop: "f9" , label: "论文预算费(元)" , placeholder: "请输入金额" , type: "number" , }, { prop: "f10" , label: "论文预算费(元)" , }, { prop: "sum" , label: "预算总额(元)" , placeholder: "自动计算" , render: (h, params) => { return [<span>4454545</span>]; }, }, ], }; }, }; </script> <style scoped> </style> |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
· SQL Server 2025 AI相关能力初探