Vue elementUI 自定义表单模板组件功能实现
作者:周围都是小趴菜 发布时间:2023-07-02 16:37:46
标签:Vue,elementUI,自定义,表单,组件
elementUI 实现一个自定义的表单模板组件注:该功能基于elementUI
背景:在项目开发中,我们会遇到这种需求,在管理后台添加自定义表单,在指定的页面使用定义好的表单
直接上代码:
<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);
}
},
// 获取创建时间
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>
来源:https://blog.csdn.net/weixin_44590591/article/details/128384778


猜你喜欢
- models.py:from django.db import models # 出版社class Publisher(models.Mod
- 方法一:在php中,抓取https的网站,提示如下的错误内容:Warning: file_get_contents() [function.
- 本文实例讲述了sql server实现递归查询的方法示例。分享给大家供大家参考,具体如下:有时候面对树结构的数据时需要进行递归查询,网上找了
- 通过创建变量var a = 'something' + '  
- 系统模块import os系统模块用于对系统进行操作。常用方法os模块的常用方法有数十种之多,本文中只选出最常用的几种,其余的还有权限操作、
- 4个不常用HTML标签optgroup、sub、sup和bdo运行代码框:<title>4个不常用HTML标签optgroup、
- php代码实现读取文件头判断文件类型,支持图片、rar、exe等后缀。案例:<?php $filename = "11.jp
- 前言我们知道在某些停机测试场景,是需要人为禁用crs/has的自启动的,防止过程中主机反复重启对数据库集群造成影响。使用crsctl dis
- 1、安装pip install mediapipe2、代码实现# -*- coding: utf-8 -*-""&quo
- Lightbox里面的一个函数,能把页面实际的高宽与浏览器可视面积的高宽保存在一个数组中..非常好用.什么是Lightbox?下载light
- 前言我原本是学C\C++,这是本人第一篇关于python的文章。请多多关照!对于python为什么要打包成exe文件,是因为传输源文件以及源
- 关于SQL Server数据库的一切信息都保存在它的系统表格里。我怀疑你是否花过比较多的时间来检查系统表格,因为你总是忙于用户表格。但是,你
- Oracle的逻辑运算符也是用在SQL语句中必不可少的因素,一共有三个逻辑运算符意义and双值运算符,如果左右两个条件都为真,则得到的值就为
- 1. 新建项目在命令行窗口下输入scrapy startproject scrapytest, 如下然后就自动创建了相应的文件,如下2. 修
- 重装电脑,在windows和虚拟机里面的Ubuntu里都安装了Pycharm专业版,安装的时候我都选择了vim插件,装好之后打开发现ctrl
- Vision Transformer(VIT)Vision Transformer(ViT)是一种新兴的图像分类模型,它使用了类似于自然语言
- //我修改之,能正常运行,测试环境为mysql5.0,xp //关键是设置对字符集,设置gbk,gb2312测试通过,utf8测试未通过 /
- pandas列转换为字典,但将相同第一列(键)的所有值合并为一个键形式一:import pandas as pd # datadata =
- wed的打印方法具我自己懂得知道的有: 1、JQuery插件Jqprint实现 2、JQery打印插件PrintArea实现网页打印 3、C
- 本文实例为大家分享了微信小程序实现扫雷游戏的具体代码,供大家参考,具体内容如下实验小提醒,打开微信小程序模板时,一定要看清楚,要选js模板,