Vue elementUI 自定義表單模板組件功能實現(xiàn)
更新時間:2022年12月24日 10:47:10 作者:周圍都是小趴菜
在項目開發(fā)中,我們會遇到這種需求,在管理后臺添加自定義表單,在指定的頁面使用定義好的表單,這篇文章主要介紹了Vue elementUI 自定義表單模板組件,需要的朋友可以參考下
elementUI 實現(xiàn)一個自定義的表單模板組件注:該功能基于elementUI
背景:在項目開發(fā)中,我們會遇到這種需求,在管理后臺添加自定義表單,在指定的頁面使用定義好的表單
直接上代碼:
<template> <div> <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body> <div class="addBox"> <el-form :model="form" label-width="90px" ref="form" :rules="rules"> <el-form-item label="選項名稱" lebel-width="80" prop="label"> <el-input placeholder="請輸入自定義選項名稱" maxlength="20" clearable v-model="form.label" @input="change($event)"> </el-input> </el-form-item> <el-form-item label="選項提示" lebel-width="80" prop="tips"> <el-input @input="change($event)" placeholder="請輸入提示內容" maxlength="20" clearable v-model="form.tips"> </el-input> </el-form-item> <el-form-item label="選項類型" prop="value"> <el-cascader clearable ref="cascader" v-model="value" :options="options" @change="handleChange"> </el-cascader> </el-form-item> <el-form-item v-if="radioCount" label="最多選幾項"> <el-input-number v-model="form.radioCount" controls-position="right" :min="1"></el-input-number> <!-- <el-input type="number" v-model="form.radioCount"></el-input> --> </el-form-item> <el-form-item v-if="isRadio" v-for="(domain, index) in radioOptions" :key="domain.id" :label="'添加選項' + (index + 1)" prop="name"> <el-row> <el-col :span="18"> <el-input placeholder="請輸入選擇框選項" v-model="domain.name"></el-input> </el-col> <el-col :span="6"> <el-button @click="addType" v-if="index == 0">新增</el-button> </el-col> <el-col :span="6"> <el-button @click.prevent="removeType(domain)" v-if="index > 0">刪除</el-button> </el-col> </el-row> </el-form-item> </el-form> </div> <div slot="footer" class="dialog-footer"> <el-button type="primary" @click="submitForm">確 定</el-button> <el-button @click="cancel">取 消</el-button> </div> </el-dialog> </div> </template> <script> export default { name: 'WorkspaceJsonProjectCopy', data() { return { title: '新增選項', isRadio: false, value: [], rules: {}, open: true, radioCount: false, form: { radioOptions: [{}], label: '' }, radioOptions: [{}], options: [{ value: "input", label: "輸入框", children: [{ value: "phone", label: "手機號", children: [{ value: "0", label: "非必填" }, { value: "1", label: "必填" } ] }, { value: "idcard", label: "身份證號", children: [{ value: "0", label: "非必填" }, { value: "1", label: "必填" } ] }, { value: "link", label: "鏈接", children: [{ value: "0", label: "非必填" }, { value: "1", label: "必填" } ] }, { value: "email", label: "郵箱", children: [{ value: "0", label: "非必填" }, { value: "1", label: "必填" } ] }, { value: "other", label: "其他", children: [{ value: "0", label: "非必填" }, { value: "1", label: "必填" } ] } ] }, { value: "radio", label: "選擇框", children: [{ value: "danxuan", label: "單選", children: [{ value: "0", label: "非必填" }, { value: "1", label: "必填" } ] }, { value: "duoxuan", label: "多選", children: [{ value: "0", label: "非必填" }, { value: "1", label: "必填" } ] } ] }, { value: "image", label: "圖片", children: [{ value: "0", label: "非必填" }, { value: "1", label: "必填" } ] }, { value: "date", label: "日期—年月日", children: [{ value: "0", label: "非必填" }, { value: "1", label: "必填" } ] }, { value: "datetime", label: "時間—時分秒", children: [{ value: "0", label: "非必填" }, { value: "1", label: "必填" } ] }, ], }; }, mounted() { }, methods: { change() { this.$forceUpdate(); }, // 彈窗選擇選項類型 handleChange(value) { this.value = value if (value[0] && value[0] == 'radio' && value[1] == 'danxuan') { this.radioOptions = [{}] this.isRadio = true this.radioCount = false } if (value[0] && value[0] == 'radio' && value[1] == 'duoxuan') { this.radioOptions = [{}] this.isRadio = true this.radioCount = true } if (value[0] && value[0] != 'radio') { this.radioOptions = [{}] this.isRadio = false this.radioCount = false } }, // 重置彈窗 reset() { this.form = {} this.value = [] this.radioOptions = [{}] this.isRadio = false this.radioCount = false, this.isEdit = false const _cascader = this.$refs.cascader if (_cascader) { _cascader.$refs.panel.checkedValue = []; _cascader.$refs.panel.activePath = []; _cascader.$refs.panel.syncActivePath() } this.resetForm("form"); }, // 取消彈窗 cancel() { this.open = false; this.form = {} this.reset() }, // 新增添加選擇框選項 addType() { this.radioOptions.push({}) }, // 刪除新增的選擇框選項 removeType(item) { var index = this.radioOptions.indexOf(item); if (index !== -1) { this.radioOptions.splice(index, 1); } }, // 獲取創(chuàng)建時間 getDate() { var date = new Date() var Y = date.getFullYear() + '-' var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-' var D = (date.getDate() < 10 ? '0' + date.getDate() : date.getDate()) + ' ' var h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':' var m = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':' var s = (date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds()) return Y + M + D + h + m + s }, submitForm: function () { var value = this.value this.$refs["form"].validate((valid) => { if (valid) { if (value[0] == 'input') { var submitData = JSON.stringify({ createTime: this.getDate(), label: this.form.label, type: 'input', inputType: value[1], mustFill: value[2], tips: this.form.tips, radioCount: null, radioOption: null }) } else if (value[0] == 'radio' && value[1] == 'danxuan') { var options = [] this.form.radioOptions = this.radioOptions for (var item of this.form.radioOptions) { options.push(item.name) } var subOption = options.join(';') console.log(options.join(';')) var submitData = JSON.stringify({ createTime: this.getDate(), label: this.form.label, type: 'radio', radioCount: 1, mustFill: value[2], radioOption: subOption, tips: this.form.tips, inputType: null }) console.log(this.form.radioOptions) } else if (value[0] == 'radio' && value[1] == 'duoxuan') { var options = [] this.form.radioOptions = this.radioOptions for (var item of this.form.radioOptions) { options.push(item.name) } var subOption = options.join(';') console.log(options.join(';')) var submitData = JSON.stringify({ createTime: this.getDate(), label: this.form.label, type: 'radio', mustFill: value[2], radioOption: subOption, tips: this.form.tips, radioCount: this.form.radioCount, inputType: null }) } else if (value[0] == 'image') { var submitData = JSON.stringify({ createTime: this.getDate(), label: this.form.label, type: 'image', mustFill: value[1], id: this.id, tips: this.form.tips, inputType: null, radioCount: null, radioOption: null }) } else if (value[0] == 'date') { var submitData = JSON.stringify({ createTime: this.getDate(), label: this.form.label, type: 'date', mustFill: value[1], id: this.id, tips: this.form.tips, inputType: null, radioCount: null, radioOption: null }) } else if (value[0] == 'datetime') { var submitData = JSON.stringify({ createTime: this.getDate(), label: this.form.label, type: 'datetime', mustFill: value[1], id: this.id, tips: this.form.tips, inputType: null, radioCount: null, radioOption: null }) } console.log(submitData) } }) }, }, }; </script>
到此這篇關于Vue elementUI 自定義表單模板組件的文章就介紹到這了,更多相關Vue elementUI 自定義表單模板組件內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Vue實現(xiàn)監(jiān)聽某個元素滾動,親測有效
這篇文章主要介紹了Vue實現(xiàn)監(jiān)聽某個元素滾動,親測有效!具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-07-07