基于uni-app的动态表单

一、应用场景和意义

可以通过配置字段和校验规则,快速完成页面开发、提升开发效率

二、应用前提

形成ui/业务规范,最好是应用在问卷调查之类的业务

三、动态表单的功能

字段报错、快速滚动定位报错信息、支持字段值和字段规则拆分,便于实现复杂的表单交互

四、动态表单支持的组件类型

1、input-text 文本输入框类型

<template v-if="item.type === 'input-text'">

<view class="formItemBox">

<view class="formItem">

<view class="left">

<view class="num" v-if="num">{{ index + 1 }}.</view>

<view v-if="item.redTip" class="colorRed">{{ item.redTip ? '*' : ''

}}

</view>

<view class="name">{{ item.label }}</view>

</view>

<view class="right">

<u-form-item>

<view v-if="item.readonly || disabledFields.includes(item.id)"

@click="showNoEdit" class="readonlytext">{{ form[item.id] }}

</view>

<u--input v-else style="width: 100%;" v-model="form[item.id]"

input-align="right" :placeholder="item.placeholder"

:placeholderStyle="placeholderStyle"

border="none"></u--input>

</u-form-item>

</view>

</view>

<u-form-item labelWidth="0" :prop="item.id" class="formItemNoBody">

</u-form-item>

<view class="line" v-show="formItem.list.length - 1 > index"></view>

</view>

</template>

支持正常的输入校验 和 不可编辑的样式和提示

2、single-select 单选

<template v-if="item.type === 'single-select'">

<view class="formItemBox">

<view class="formItem">

<view class="left">

<view class="num" v-if="num">{{ index + 1 }}.</view>

<view v-if="item.redTip" class="colorRed">{{ item.redTip ? '*' : ''

}}

</view>

<view class="name">{{ item.label }}</view>

</view>

<view class="right optItemBox">

<view class="optItem" @click.stop="showNoEditOrSelect(item)"></view>

<u-form-item>

<view v-if="item.readonly || disabledFields.includes(item.id)"

class="readonlytext">{{ form[item.id] }}</view>

<view v-else class="select_right">

<view v-if="!form[item.id]" class="placeholderStyle">

{{ item.placeholder }}</view>

<view v-else class="select_right_text">

{{ item.listMap[form[item.id]] &&

item.listMap[form[item.id]].name || '未匹配到' }}

</view>

</view>

<view v-show="false">

<u--input style="width: 100%;" v-model="form[item.id]"

input-align="right" :placeholder="item.placeholder"

:placeholderStyle="placeholderStyle"

border="none"></u--input>

</view>

<view class="arrow_right_icon">

<img src="./icon/arrow.png" alt="">

</view>

</u-form-item>

</view>

</view>

<u-form-item labelWidth="0" :prop="item.id" class="formItemNoBody">

</u-form-item>

<view class="line"></view>

</view>

<!-- <u-action-sheet cancelText="取消" :safeAreaInsetBottom="true" :actions="item.list"

:title="item.selectTitle" safeAreaInsetBottom :show="item.showSelect"

@select="selectConfirm($event, item)" @close="selectClose(item)"></u-action-sheet> --> <u-picker :show="item.showSelect" ref="uPicker" :defaultIndex="defaultIndex" :confirmColor="env.themeColor" :columns="[item.list]" @cancel="selectClose(item)" @close="selectClose(item)" keyName="name" @confirm="selectConfirm($event, item)"></u-picker>

</template>

支持正常的单选和不可编辑以及未匹配到的提示

3、dependent-computing 依赖别的字段计算

<template v-if="item.type === 'dependent-computing'">

<view class="formItemBox">

<view class="formItem">

<view class="left">

<view class="num" v-if="num">{{ index + 1 }}.</view>

<view v-if="item.redTip" class="colorRed">{{ item.redTip ? '*' : ''

}}

</view>

<view class="name">{{ item.label }}</view>

</view>

<view class="right optItemBox">

<view class="optItem" @click.stop="showNoEditOrSelect(item)"></view>

<u-form-item>

<view v-if="item.readonly || disabledFields.includes(item.id)"

class="readonlytext">{{ form[item.id] }}</view>

<view v-else class="select_right">

<u--input style="width: 100%;" v-model="form[item.id]"

input-align="right" :placeholder="item.placeholder"

:placeholderStyle="placeholderStyle"

border="none"></u--input>

</view>

</u-form-item>

</view>

</view>

<u-form-item labelWidth="0" :prop="item.id" class="formItemNoBody">

</u-form-item>

<view class="line"></view>

</view>

</template>

依赖计算不可编辑、做显示

4、switch 开关

<template v-if="item.type === 'switch'">

<view class="formItemBox">

<view class="formItem formSwitchItem">

<view class="left">

<view class="num" v-if="num">{{ index + 1 }}.</view>

<view v-if="item.redTip" class="colorRed">{{ item.redTip ? '*' : ''

}}

</view>

<view class="name">{{ item.label }}</view>

</view>

<view class="right optItemBox">

<view v-if="item.readonly || disabledFields.includes(item.id)"

class="optItem" @click.stop="showNoEditOrSelect(item)"></view>

<u-form-item>

<view class="switchBox">

<u-switch v-model="form[item.id]"

inactiveColor="rgba(229, 230, 235, 1)"

:activeColor="env.themeColor"

@change="changeSwitch($event, item)"></u-switch>

</view>

</u-form-item>

</view>

</view>

<u-form-item labelWidth="0" :prop="item.id" class="formItemNoBody">

</u-form-item>

<view class="line"></view>

</view>

</template>

5、单选日期 single-calendar

<template v-if="item.type === 'single-calendar'">

<view class="formItemBox">

<view class="formItem">

<view class="left">

<view class="num" v-if="num">{{ index + 1 }}.</view>

<view v-if="item.redTip" class="colorRed">{{ item.redTip ? '*' : ''

}}

</view>

<view class="name">{{ item.label }}</view>

</view>

<view class="right optItemBox">

<view class="optItem" @click.stop="showNoEditOrCalendar(item)">

</view>

<u-form-item>

<view v-if="item.readonly || disabledFields.includes(item.id)"

class="readonlytext">{{ form[item.id] }}</view>

<view v-else class="select_right">

<view v-if="!form[item.id]" class="placeholderStyle">

{{ item.placeholder }}</view>

<view v-else class="select_right_text">

{{ form[item.id] }}</view>

</view>

<view v-show="false">

<u--input style="width: 100%;" v-model="form[item.id]"

input-align="right" :placeholder="item.placeholder"

:placeholderStyle="placeholderStyle"

border="none"></u--input>

</view>

<view class="arrow_right_icon">

<img src="./icon/arrow.png" alt="">

</view>

</u-form-item>

</view>

</view>

<u-form-item labelWidth="0" :prop="item.id" class="formItemNoBody">

</u-form-item>

<view class="line"></view>

</view>

<!-- <u-calendar :show="item.showSingleCalendar" :color="env.themeColor"

:defaultDate="defaultDateSingle" mode="single"

></u-calendar> -->

<u-datetime-picker :show="item.showSingleCalendar" v-model="defaultDateSingle"

mode="date" @confirm="singleCalendarConfirm($event, item)"

@cancel="closeCalendar(item)"

@close="closeCalendar(item)"></u-datetime-picker>

</template>

支持正常的单选和不可编辑

6、upload-images 上传图片

<template v-if="item.type === 'upload-images'">

<view class="formItemBox">

<view class="formItem">

<view class="left">

<view class="num" v-if="num">{{ index + 1 }}.</view>

<view v-if="item.redTip" class="colorRed">{{ item.redTip ? '*' : ''

}}

</view>

<view class="name">{{ item.label }}</view>

</view>

</view>

<view class='img-wrap'>

<view v-show="false">

<u--input style="width: 100%;" v-model="form[${item.id}filePath]"

input-align="right" :placeholder="item.placeholder"

:placeholderStyle="placeholderStyle" border="none"></u--input>

</view>

<Upload :filesData="form[item.id] || []"

@onFilesChange="(items) => filesDataChange(items, ${item.id})"

:disableEdit="item.readonly || disabledFields.includes(item.id)"

:max='item.imgLen' uploadType='photo' />

</view>

<u-form-item labelWidth="0" :prop="${item.id}filePath"

class="formItemNoBody">

</u-form-item>

<view class="line"></view>

</view>

</template>

这个图片上传耦合了接口上传 和禁止编辑

五、入参

formData 表单校验规则

valueForm 表单字段值

六、使用示例

form: {

ownerName: '',

idNumbe: '',

phoneNumber: "",

proAddres: "",

projectDetailedAddress: "",

},

allFormData: [

{

title: '客户信息',

showTitle: true,

list: [{

id: "ownerName",

placeholder: "请输入",

label: "姓名",

type: "input-text",

redTip: false,

readonly: true,

rules: [

{

type: 'string',

required: true,

message: '请输入',

trigger: ['change', 'blur']

}

]

},

{

id: "idNumbe",

placeholder: "请输入",

label: "证件号码",

type: "input-text",

redTip: false,

readonly: true,

rules: [

{

type: 'string',

required: true,

message: '请输入',

trigger: ['change', 'blur']

}

]

},

{

id: "phoneNumber",

placeholder: "请输入",

label: "联系电话",

type: "input-text",

redTip: false,

readonly: true,

rules: [

{

type: 'string',

required: true,

message: '请输入',

trigger: ['change', 'blur']

}

]

},

]

},

{

title: '项目地址',

showTitle: true,

list: [{

id: "proAddres",

placeholder: "请输入",

label: "项目地址",

type: "input-text",

redTip: false,

readonly: true,

rules: [

{

type: 'string',

required: true,

message: '请输入',

trigger: ['change', 'blur']

}

]

}, {

id: "projectDetailedAddress",

placeholder: "请输入",

label: "详细地址",

type: "input-text",

redTip: false,

readonly: true,

rules: [

{

type: 'string',

required: true,

message: '请输入',

trigger: ['change', 'blur']

}

]

}]

},

{

title: '电站基本情况',

showTitle: true,

list: [{

id: "zuKuai",

placeholder: "请输入",

label: "板块数",

type: "input-text",

redTip: false,

readonly: false,

rules: [

{

type: 'string',

required: true,

message: '请输入',

trigger: ['change', 'blur']

}, {

validator: (rule, value, callback) => {

const pattern = /^[1-9]\d{0,3}$|^[1-9]\d{0,3}$|^\d{1,4}$/

if (!pattern.test(value)) {

callback(new Error('请输入大于0小于10000的正整数'));

} else {

callback();

}

},

trigger: ['change', 'blur'],

}

]

},

{

id: "zuPower",

placeholder: "请选择",

label: "功率",

type: "single-select",

redTip: false,

readonly: false,

showSelect: false,

selectTitle: "请选择",

list: [],

listMap: {},

rules: [

{

type: 'string',

required: true,

message: '请选择',

trigger: ['change', 'blur']

}

]

},

{

id: "totalZuPower",

placeholder: "计算带出,不得修改",

label: "系统容量",

type: "dependent-computing",

redTip: false,

readonly: false,

rules: [{

type: 'string',

required: true,

message: '请完善版块数和功率',

trigger: ['change', 'blur']

}, {

validator: (rule, value, callback) => {

if (Number(value) > 120000) {

callback(new Error('系统容量不能大于120000'));

} else {

callback();

}

},

trigger: ['change', 'blur'],

}]

},]

},

{

title: '付款信息',

showTitle: true,

list: [

{

id: "priceType",

placeholder: "请选择",

label: "价格类型",

type: "single-select",

redTip: false,

readonly: false,

showSelect: false,

selectTitle: "请选择",

list: [],

listMap: {},

rules: [

{

type: 'string',

required: true,

message: '请选择',

trigger: ['change', 'blur']

}

],

beferConfirmFun: (confirmFun) => {

uni.showModal({

title: '提示',

content: '修改价格类型后需重新填写付款信息',

success: async (res) => {

if (res.confirm) {

confirmFun()

}

},

});

}

},

{

id: "assemblyAttribute",

placeholder: "请选择",

label: "组件属性",

type: "single-select",

redTip: false,

readonly: false,

showSelect: false,

selectTitle: "请选择",

list: [],

listMap: {},

rules: [

{

type: 'string',

required: true,

message: '请选择',

trigger: ['change', 'blur']

}

]

},

{

id: "applySituation",

placeholder: "请选择",

label: "应用场景",

type: "single-select",

redTip: false,

readonly: false,

showSelect: false,

selectTitle: "请选择",

list: [],

listMap: {},

rules: [

{

type: 'string',

required: true,

message: '请选择',

trigger: ['change', 'blur']

}

]

},

{

id: "singleDoubleSide",

placeholder: "请选择",

label: "单双面",

type: "single-select",

redTip: false,

readonly: false,

showSelect: false,

selectTitle: "请选择",

list: [],

listMap: {},

rules: [

{

type: 'string',

required: true,

message: '请选择',

trigger: ['change', 'blur']

}

]

},

]

},

],

七、案例显示

可以进行复杂的交互和可视区域定位校验

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/571632.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

Linux安装Matlab运行时

一般而言&#xff0c;安装Matlab的linux系统是带桌面版的&#xff0c;如果没带&#xff0c;不在本教程范围内。 一、下载Matlab 下载地址&#xff1a;MATLAB Runtime - MATLAB Compiler - MATLAB 本教程使用R2020b(9.9) 二、linux系统中进行解压 将zip传入linux系统&#xf…

微电子领域常见概念(八)靶材

微电子领域常见概念&#xff08;八&#xff09;靶材 靶材是用于物理气相沉积&#xff08;PVD&#xff09;技术中的一种关键材料&#xff0c;它在制备薄膜的过程中起到至关重要的作用。PVD技术包括多种不同的工艺&#xff0c;如磁控溅射、离子束溅射、分子束外延&#xff08;MBE…

Vue:vue的工程化

Vue前端工程化 前后端分离开发 即前端人员开发前端工程,将开发好的前端工程打包部署在前端服务器上 后端开发人员开发后端工程,再将后端工程打包部署在后端服务器上,这种模式称为前后端分离开发 而前后端要顺利对接的关键就是要遵循一定的开发规范 开发规范 这种开发规范定…

CCF区块链会议--Middleware 2024 截止5.24 附录用率

会议名称&#xff1a;Middleware CCF等级&#xff1a;CCF B类会议 类别&#xff1a;软件工程/系统软件/程序设计语言 录用率&#xff1a;2022年录用率38%&#xff08;8/21&#xff09; Topics of Interest The Middleware conference seeks original submissions of resear…

LAMP(Linux+Apache+MySQL+PHP)环境介绍、配置、搭建

LAMP(LinuxApacheMySQLPHP)环境介绍、配置、搭建 LAMP介绍 LAMP是由Linux&#xff0c; Apache&#xff0c; MySQL&#xff0c; PHP组成的&#xff0c;即把Apache、MySQL以及PHP安装在Linux系统上&#xff0c;组成一个环境来运行PHP的脚本语言。Apache是最常用的Web服务软件&a…

科技赋能无人零售

科技赋能无人零售&#xff0c;使其具备以下独特优势&#xff1a; 1. 全天候无缝服务 &#xff1a;无人零售店依托科技&#xff0c;实现24小时不间断运营&#xff0c;不受人力限制&#xff0c;满足消费者随时购物需求&#xff0c;尤其惠及夜间工作者、夜猫子及急需购物者&…

微前端是如何实现作用域隔离的?

微前端是如何实现作用域隔离的&#xff1f; 一、前言 沙箱&#xff08;Sandbox&#xff09;是一种安全机制&#xff0c;目的是让程序运行在一个相对独立的隔离环境&#xff0c;使其不对外界的程序造成影响&#xff0c;保障系统的安全。作为开发人员&#xff0c;我们经常会同沙…

03-JAVA设计模式-访问者模式

访问者模式 什么是访问者模式 访问者模式&#xff08;Visitor Pattern&#xff09;是软件设计模式中的一种行为模式&#xff0c;它用于将数据结构中的元素与操作这些元素的操作解耦。这种模式使得可以在不修改数据结构的情况下添加新的操作。 在访问者模式中&#xff0c;我们…

PHP+MYSQL多条件选一通用搜索系统功能单文件7KB

通用功能: 快速填写参数用于自己的mysql数据表搜索,ajax载入数据 <?php header("content-Type: text/html; charsetUTF-8"); //error_reporting(0);$dbhost "localhost"; //数据库地址本地localhost $dbuser "chalidecom"; //数据库账号 …

Tkinter是什么?

Tkinter是Python标准库中的一个模块&#xff0c;用于创建图形用户界面&#xff08;GUI&#xff09;应用程序。它提供了一组工具和组件&#xff0c;使开发者能够在Python中创建窗口、按钮、标签、文本框、菜单等各种界面元素&#xff0c;并通过这些元素构建交互式的用户界面。 T…

稀碎从零算法笔记Day59-LeetCode: 感染二叉树需要的总时间

题型&#xff1a;树、图、BFS、DFS 链接&#xff1a;2385. 感染二叉树需要的总时间 - 力扣&#xff08;LeetCode&#xff09; 来源&#xff1a;LeetCode 题目描述 给你一棵二叉树的根节点 root &#xff0c;二叉树中节点的值 互不相同 。另给你一个整数 start 。在第 0 分钟…

Three.js入门学习笔记

学习资料&#xff1a; 【Three.js】Three.js快速上手教程_three.module.js-CSDN博客 2024年了&#xff0c;是该学学Three.js了_three.js 2024-CSDN博客 一、three.js简介 three.js是JavaScript编写的WebGL第三方库。 three.js&#xff0c;webGL&#xff0c;openGL三者的关…

微信小程序4~6章总结

目录 第四章 页面组件总结 4.1 组件的定义及属性 4.2 容器视图组件 4.2.1 view 4.2.2 scroll-view 4.2.3 swiper 4.3 基础内容组件 4.3.1 icon ​编辑 4.3.2 text 4.3.3 progress ​编辑 4.4 表单组件 4.4.1 button 4.4.2 radio 4.4.3 checkbox 4.4.4 switch …

第27天:安全开发-PHP应用TP框架路由访问对象操作内置过滤绕过核心漏洞

第二十七天 一、TP框架-开发-路由访问&数据库&文件上传&MVC模型 1.TP框架-开发-配置架构&路由&MVC模型 参考&#xff1a;https://www.kancloud.cn/manual/thinkphp5_1 配置架构-导入使用路由访问-URL访问数据库操作-应用对象文件上传操作-应用对象前端页…

51.HarmonyOS鸿蒙系统 App(ArkUI)通知

普通文本通知测试 长文本通知测试 多行文本通知测试 图片通知测试 进度条通知测试 通知简介 应用可以通过通知接口发送通知消息&#xff0c;终端用户可以通过通知栏查看通知内容&#xff0c;也可以点击通知来打开应用。 通知常见的使用场景&#xff1a; 显示接收到的短消息、…

L1-099 帮助色盲 - java

L1-099 帮助色盲 代码长度限制 16 KB 时间限制 400 ms 内存限制 64 MB 栈限制 8192 KB 题目描述&#xff1a; 在古老的红绿灯面前&#xff0c;红绿色盲患者无法分辨当前亮起的灯是红色还是绿色&#xff0c;有些聪明人通过路口的策略是这样的&#xff1a;当红灯或绿灯亮起时&am…

VMware-Linux切换桥接模式上网教程(超详细)

这里写目录标题 1. 虚拟机关机2. VMware 虚拟网络配置2.1 检查是否存在 VMnet02.2 修改桥接模式2.3 修改Linux虚拟机网络适配器 3. Linux 系统配置3.1 修改系统网卡配置3.1.1 配置项含义解释3.1.2 查看物理机网络信息3.3.3 修改配置 3.2 重启服务 4. 测试网络连接情况5. 注意事…

数据可视化-ECharts Html项目实战(14)

在之前的文章中&#xff0c;我们深入学习ECharts鼠标左键触发。想了解的朋友可以查看这篇文章。同时&#xff0c;希望我的文章能帮助到你&#xff0c;如果觉得我的文章写的不错&#xff0c;请留下你宝贵的点赞&#xff0c;谢谢。 数据可视化-ECharts Html项目实战&#xff08;…

目标检测——YOLOv7算法解读

论文&#xff1a;YOLOv7: Trainable bag-of-freebies sets new state-of-the-art for real-time object detectors (2022.7.6) 作者&#xff1a;Chien-Yao Wang, Alexey Bochkovskiy, Hong-Yuan Mark Liao 链接&#xff1a;https://arxiv.org/abs/2207.02696 代码&#xff1a;h…

Bayes判别示例数据:鸢尾花数据集

使用Bayes判别的R语言实例通常涉及使用朴素贝叶斯分类器。朴素贝叶斯分类器是一种简单的概率分类器&#xff0c;基于贝叶斯定理和特征之间的独立性假设。在R中&#xff0c;我们可以使用e1071包中的naiveBayes函数来实现这一算法。下面&#xff0c;我将通过一个简单的示例展示如…