vue3使用tsx渲染复杂逻辑的表单
前置
目前的应用场景是:检查项目是树结构,有的项目还需要动态显示对应的子集
项目用的是uniapp+vue3+ts+vite生成的app
tsx模版
统一渲染入口
<script lang="ts">import uniForms from '@dcloudio/uni-ui/lib/uni-forms/uni-forms.vue'import uniFormsItem from '@dcloudio/uni-ui/lib/uni-forms-item/uni-forms-item.vue'import uniDataCheckbox from '@dcloudio/uni-ui/lib/uni-data-checkbox/uni-data-checkbox.vue'import uniEasyinput from '@dcloudio/uni-ui/lib/uni-easyinput/uni-easyinput.vue'
export default {components:{uniForms, uniFormsItem,uniEasyinput,uniDataCheckbox},props: {params: {type: Object,},render: {type: Function,},},render() {return this.render(this.params)},
}
</script>
<style lang="scss" scope>// .sele-inp{// gap: 20px;// }.cus-column{flex-direction: column;:deep(.checklist-group){gap: 20upx;flex-direction: column!important;}}.renderForm{background: #fff;padding: 20upx;box-sizing: border-box;// margin-top: 40upx;}:deep(.uni-forms-item__label){color: rgba(0, 0, 0, 0.85);}
</style>
动态解析tsx
tsx中用到的组件需要再,统一入口去引入,名字需要保持一致,目前渲染的表单类型,之有两个,input和redio,可根据自己的业务自行扩展
<script setup lang="tsx">import uniDataCheckbox from '@dcloudio/uni-ui/lib/uni-data-checkbox/uni-data-checkbox.vue'import { h, resolveComponent } from 'vue'// import uniForms from '@dcloudio/uni-ui/lib/uni-forms/uni-forms.vue'// import uniFormsItem from '@dcloudio/uni-ui/lib/uni-forms-item/uni-forms-item.vue'// import uniEasyinput from '@dcloudio/uni-ui/lib/uni-easyinput/uni-easyinput.vue'
type RenderType = {temList?: any[]
}const defalutItem = {itemType: '', itemTypeName: '', itemId: '',parentId: '',
itemName: '', itemCode: '', itemRes: '',itemResLabel: ''
}
// 属性
const props = withDefaults(defineProps<RenderType>(),{temList: () => []
})// 表单值
const formDate = reactive({
})// 类型暂无用处
const options = {input: "uniEasyinput",radio: "uniDataCheckbox"
}
const setLabel = (val, id,key,parentItem) => {formDate[id][key] = val.detail.data.textlet item = val.detail.data.dataif(item && item.children && item.children.length > 0){formDate[parentItem.code] ? formDate[parentItem.code] : formDate[parentItem.code] = {}formDate[parentItem.code]['itemType'] = item.typeformDate[parentItem.code]['itemTypeName'] = item.typeNameformDate[parentItem.code]['itemId'] = item.idformDate[parentItem.code]['parentId'] = item.parentIdformDate[parentItem.code]['itemName'] = item.nameformDate[parentItem.code]['itemCode'] = item.code// formDate[parentItem.code]['itemResLabel'] ? formDate[parentItem.code]['itemResLabel'] : ''// formDate[parentItem.code]['itemRes'] ? '' : formDate[parentItem.code]['itemRes'] = ''}else {delete formDate[parentItem.code]}
}
const renderFormRef = ref()
const initDefaultItemForm = (item) => {formDate[item.id] ? formDate[item.id] : formDate[item.id] = {}formDate[item.id]['itemType'] = item.typeformDate[item.id]['itemTypeName'] = item.typeNameformDate[item.id]['itemId'] = item.idformDate[item.id]['parentId'] = item.parentIdformDate[item.id]['itemName'] = item.nameformDate[item.id]['itemCode'] = item.codeformDate[item.id]['itemResLabel'] ? formDate[item.id]['itemResLabel'] : ''formDate[item.id]['itemRes'] ? '' : formDate[item.id]['itemRes'] = ''console.log('initDefaultItemForm')
}
// <view class=""> {formDate[item.id] === ss.parentId ? <view class="theme theme-flex sele-inp"><text>{ ss.name }</text><uniEasyinput v-model={formDate[ss.id]}></uniEasyinput> </view>: '' }</view>
const typeProcessor = (type?: string, item?: any, node?: any[]) => {if(type === 'input'){return <uniFormsItem label={item.name} name={"[" + item.id + ", itemRes ]"}><uniEasyinput v-model={formDate[item.id]['itemRes']} placeholder={'请输入'+item.name}></uniEasyinput></uniFormsItem>}else if(type === 'radio'){const list = [] as any[]// let node = []if(item.children && item.children.length > 0){item.children.forEach((it:any) => {list.push({text: it.name, value: it.code, 'data': it})if(it.children && it.children.length > 0){it.children.forEach((ss:any) => {// 删除对应的值// delete formDate[ss.id]node!.push(ss)})}})}console.log('list',list)return [<view class={node && node.length > 0 ? '' : ''}><uniFormsItem label={item.name} name={"[" + item.id + ", itemRes ]"}><uniDataCheckbox class={item.children.length > 3 ? '' : ''} v-model={formDate[item.id]['itemRes']} localdata={list} placeholder={'请选择'+item.name} onChange={(val) => setLabel(val, item.id, 'itemResLabel',item)}></uniDataCheckbox></uniFormsItem>{node!.map(no => {let parent_code = nulltry{// 匹配父级codelet codes = no.code.split('_');codes = [...codes.slice(0, -1)]parent_code = codes.join('_')}catch(e){}formDate[item.id]['itemRes'] === parent_code ? (initDefaultItemForm(no)) : delete formDate[no.id]return [formDate[item.id]['itemRes'] === parent_code ?<uniFormsItem label={no.name} name={"[" + no.id + ", itemRes ]"}><uniEasyinput v-model={formDate[no.id]['itemRes']} placeholder={'请输入'+ no.name}></uniEasyinput> </uniFormsItem>: '']})}</view>]}
}const renderFn = (formDate: any) => {console.log('formDate', formDate)return [<uniForms class="renderForm" label-width={100} modelValue={formDate} ref={renderFormRef}>{props.temList.map((item) => {initDefaultItemForm(item)let node = [] as any[]return [<view>{typeProcessor(item.renderType, item, node)}</view>]})}</uniForms>]
}
const submit = () => {// reanderTemplateRefconsole.log(formDate)console.log(renderFormRef.value)
}
defineExpose({submit,formDate
})
</script><template><jsxRender :params="formDate" :render="renderFn" />
</template><style lang="scss" scoped>
.jsx-render {color: #41B883;
}
// .sele-inp{
// gap: 20px;
// }
</style>
案例
使用
<template><view class="re-box"><view v-for="(item ,index) in props.tagLists"><view class="box-top"><uni-section class="mb-10" :title="item.typeName" type="line" titleFontSize="20px"></uni-section></view><ReanderTemplate :temList="item.items" ref="reanderTemplateRef" :key="index"/><!-- <uni-forms-item label="医生建议" ><uni-easyinput type="textarea" v-model="'expandParamMap['+item.type +'][docAdvice]'" placeholder="请输入医生建议" /></uni-forms-item> --><view style="padding: 10px;box-sizing: border-box;padding-top: 0;"><uni-forms-item label="医生建议"><uni-easyinput type="textarea" v-model="expandParamMap[item.type]['docAdvice']" placeholder="请输入医生建议" /></uni-forms-item></view></view><view style="padding: 10px;box-sizing: border-box;padding-top: 0;"><uni-forms-item label="签名"><view class="signature" v-if="signatureUrl"><image class="signatureImg" :src=" imgUrlPreFix + signatureUrl" /></view><wd-button plain hairline v-else @click="$tab.navigateTo(`/pages/signature/index`)">点击签名</wd-button></uni-forms-item> </view></view>
</template><script setup lang="ts" name="RenderBox">import { getCurrentInstance, ref } from 'vue'import { useToast } from 'wot-design-uni'import useUpload from '@/hooks/upload'import { upload } from '@/api/system/uploadService'import ReanderTemplate from '@/components/reanderTemplate/index.vue'import { examineFinish } from '@/api/app/speExamineTypeService'import { onLoad } from '@dcloudio/uni-app'const imgUrlPreFix = import.meta.env.VITE_APP_GATEWAY + '/file';const proxy = getCurrentInstance().proxytype EmitType = {(e: 'saveFallback') :void}const signatureUrl = ref()const toast = useToast()const reanderTemplateRef = ref()const emits = defineEmits<EmitType>()const itemAll = inject('itemAll')const expandParamMap = ref<any>({})const props = withDefaults(defineProps<{tagLists?: any[], formDataObj?: any[]}>(),{tagLists: () => [],formDataObj: () => []})watchEffect(() => {props.tagLists.forEach((item) => {expandParamMap.value[item.type] = {itemType: item.type, passed: true, docAdvice: ''}})if(props.formDataObj && props.formDataObj.length > 0){nextTick(() => {for(let i=0; i < reanderTemplateRef.value.length; i++){let item = reanderTemplateRef.value[i]Object.assign(item.formDate,props.formDataObj[i])console.log('item.formDate',item.formDate)}})}})const submit = (params: any, url: string) => {proxy.$pop.showConfirm('','确认保存数据吗',true).then((res: any) => {console.log(res)if(res.confirm){submitFun(params, url)}})}const submitFun = (params: any) => {let itemResList = [] as ExamineTypeParams[]reanderTemplateRef.value.forEach((item: any) => {console.log('item.formDate',item.formDate)itemResList = itemResList.concat(Object.values(item.formDate) as ExamineTypeParams[])})let saveData = {signUrl: signatureUrl.value,expandParamMap: expandParamMap.value,infoId: params.infoId as string,itemResList: itemResList,itemAll: itemAll.value as SpeExamineInfoDocScanCodeDto[]}console.log('saveData',saveData)uni.showLoading({mask: true,title: '保存中...'});examineFinish(saveData).then(res => {console.log(res)proxy?.$pop.toast("保存成功")emits('saveFallback')}).finally(() => (uni.hideLoading()))}const signatureFun = (val: string) => {uni.showLoading({mask: true,title: '签名上传中...'})let ss =new Date().getTime() + '.jpg'let pa = {fileName: ss, localPath: val}useUpload(pa).then((res:any)=>{signatureUrl.value = res.data.fileUrlPath}).finally(() => (uni.hideLoading()))}onLoad(() => {uni.$on('signature', signatureFun)})onUnmounted(() => {uni.$off('signature', signatureFun)})defineExpose({submit})
</script><style lang="scss" scoped>.re-box{margin-top: 20upx;background-color: #fff;}.box-top{// padding: 20upx;font-size: 30upx;background: #fff;}.signature{width: 100px;height: 100px;.signatureImg{display: block;width: 100%;height: 100%;}}
</style>
数据结构及案例效果图
整理完后,后面贴
相关文章:
vue3使用tsx渲染复杂逻辑的表单
前置 目前的应用场景是:检查项目是树结构,有的项目还需要动态显示对应的子集 项目用的是uniappvue3tsvite生成的app tsx模版 统一渲染入口 <script lang"ts">import uniForms from dcloudio/uni-ui/lib/uni-forms/uni-forms.vueimport…...
python助力WRF自动化运行
对大部分人而言,特别是新用户,WRF模式的安装繁琐且不必要,可以作为后续进阶掌握的技能,本学习跳过繁琐的安装步骤,直接聚焦模式的运行部分,通过短平快的教学,快速掌握模式运行。进一步将python语…...
Windows 下 Postgres 安装 TimescaleDB 插件
Windows 下 Postgres 安装 TimescaleDB 插件 一、准备工作 安装 PostgreSQL:首先确保你已经在 Windows 系统中成功安装了 PostgreSQL 数据库。可以从 PostgreSQL 官方网站下载适合你系统的安装包,并按照安装向导进行安装。安装过程中,记住设…...
【Vim Masterclass 笔记21】S09L39:Vim 设置与 vimrc 文件的用法示例(二)
文章目录 S09L39 Vim Settings and the Vimrc File - Part 21 Vim 的配色方案与 color 命令2 map 命令3 示例:用 map 命令快速生成 HTML 代码片段4 Vim 中的 Leader 键5 用 mkvimrc 命令自动生成配置文件 写在前面 本篇为 Vim 自定义配置的第二部分。当中的每个知识…...
【Docker】Supervisor 实现单容器运行多服务进程
本文内容均来自个人笔记并重新梳理,如有错误欢迎指正! 如果对您有帮助,烦请点赞、关注、转发、订阅专栏! 专栏订阅入口 | 精选文章 | Kubernetes | Docker | Linux | 羊毛资源 | 工具推荐 | 往期精彩文章 【Docker】(全…...
【网络协议】【http】【https】ECDHE-TLS1.2
【网络协议】【http】【https】ECDHE-TLS1.2 ECDHE算法 1.客户端和服务器端事先确定好使用哪种椭圆曲线,和曲线上的基点G,这两个参数都是公开的, 双方各自随机生成一个随机数作为私钥d,并与基点 G相乘得到公钥Q(QdG),…...
(十五)WebGL中gl.texImage2D函数使用详解
在 WebGL 中,gl.texImage2D 是一个非常关键的函数,用于将图像数据上传到 WebGL 上下文中并作为纹理对象的一部分。它允许你将图像、视频、画布等作为纹理源。理解如何使用 gl.texImage2D 是在 WebGL 中处理纹理的核心之一。 文章目录 gl.texImage2D 的基…...
CSS 颜色
所有浏览器都支持的颜色名 所有现代浏览器均支持以下 140 种颜色名称(单击颜色名称或十六进制值,可查看将以该颜色为背景颜色以及不同的文本颜色): 颜色名十六进制颜色值颜色AliceBlue#F0F8FFAntiqueWhite#FAEBD7Aqua#00FFFFAqu…...
C#,入门教程(03)——Visual Studio 2022编写彩色Hello World与动画效果
C#,入门教程(01)—— Visual Studio 2022 免费安装的详细图文与动画教程https://blog.csdn.net/beijinghorn/article/details/123350910 C#,入门教程(02)—— Visual Studio 2022开发环境搭建图文教程https://blog.csdn.net/beijinghorn/article/detail…...
杀死安装 CentOS-7-x86_64-DVD-1908
使用 VMware 安装 CentOS-7-x86_64-DVD-1908 CentOS是 reahat 的 免费版本,有了ubutun ,为什么还要使用 CentOS呢? 在linux 服务器实际开发中,大家都用的CentOS,因为两个原因,一个是免费,第二是…...
55.【5】BUUCTF WEB NCTF2019 sqli
进入靶场 输入admin 123 过滤的这么严格??? 过滤很严格,此时要么爆破,要么扫描 直接扫描,得到robots.txt 访问后又得到hint.txt 继续访问 图片内容如下 $black_list "/limit|by|substr|mid|,|admi…...
LeetCode 题目 2545. 根据第 K 场考试的分数排序
在本篇文章中,我们将探讨如何根据第 K 场考试的分数对学生进行排序。这个问题是 LeetCode 上的一个中等难度问题,涉及到排序算法和自定义比较函数的使用。 问题描述 解题思路 理解问题 首先,我们需要理解问题的核心:根据第 K 场…...
算法随笔_12:最短无序子数组
上一篇: 算法随笔_11: 字符串的排列-CSDN博客 题目描述如下: 给你一个整数数组 nums ,你需要找出一个 连续子数组 ,如果对这个子数组进行升序排序,那么整个数组都会变为升序排序。请你找出符合题意的最短子数组,并输出它的长度。…...
计算机毕业设计PySpark+Hadoop+Hive机票预测 飞机票航班数据分析可视化大屏 航班预测系统 机票爬虫 飞机票推荐系统 大数据毕业设计
温馨提示:文末有 CSDN 平台官方提供的学长联系方式的名片! 温馨提示:文末有 CSDN 平台官方提供的学长联系方式的名片! 温馨提示:文末有 CSDN 平台官方提供的学长联系方式的名片! 作者简介:Java领…...
Linux-C/C++--初探linux应用编程概念
对于大多数首次接触 Linux 应用编程的读者来说,可能对应用编程(也可称为系统编程)这个概念并不 太了解,所以在正式学习 Linux 应用编程之前,笔者有必要向大家介绍这些简单基本的概念,从整体上认识 到应用编…...
用sklearn运行分类模型,选择AUC最高的模型保存模型权重并绘制AUCROC曲线(以逻辑回归、随机森林、梯度提升、MLP为例)
诸神缄默不语-个人CSDN博文目录 文章目录 1. 导入包2. 初始化分类模型3. 训练、测试模型,绘图,保存指标 1. 导入包 from sklearn.linear_model import LogisticRegression from sklearn.ensemble import RandomForestClassifier, GradientBoostingClass…...
动手学大数据-3社区开源实践
目录 数据库概览: MaxComput: HAWQ: Hologres: TiDB: Spark: ClickHouse: Apache Calcite 概览 Calcite RBO HepPlanner 优化规则(Rule) 内置有100优化规则 …...
使用Pydantic驾驭大模型
本文介绍Pydantic 库,首先介绍其概念及优势,然后通过基本示例展示如何进行数据验证。后面通过多个示例解释如何在LangChain中通过Pydantic进行数据验证,保证与大模型进行交互过程中数据准确性,并显示清晰的数验证错误信息。 Pydan…...
【HarmonyOS之旅】基于ArkTS开发(二) -> UI开发之常见布局
目录 1 -> 自适应布局 1.1 -> 线性布局 1.1.1 -> 线性布局的排列 1.1.2 -> 自适应拉伸 1.1.3 -> 自适应缩放 1.1.4 -> 定位能力 1.1.5 -> 自适应延伸 1.2 -> 层叠布局 1.2.1 -> 对齐方式 1.2.2 -> Z序控制 1.3 -> 弹性布局 1.3.1…...
【论文投稿】Python 网络爬虫:探秘网页数据抓取的奇妙世界
目录 前言 一、Python—— 网络爬虫的绝佳拍档 二、网络爬虫基础:揭开神秘面纱 (一)工作原理:步步为营的数据狩猎 (二)分类:各显神通的爬虫家族 三、Python 网络爬虫核心库深度剖析 &…...
未来机器人的大脑:如何用神经网络模拟器实现更智能的决策?
编辑:陈萍萍的公主一点人工一点智能 未来机器人的大脑:如何用神经网络模拟器实现更智能的决策?RWM通过双自回归机制有效解决了复合误差、部分可观测性和随机动力学等关键挑战,在不依赖领域特定归纳偏见的条件下实现了卓越的预测准…...
golang循环变量捕获问题
在 Go 语言中,当在循环中启动协程(goroutine)时,如果在协程闭包中直接引用循环变量,可能会遇到一个常见的陷阱 - 循环变量捕获问题。让我详细解释一下: 问题背景 看这个代码片段: fo…...
Appium+python自动化(十六)- ADB命令
简介 Android 调试桥(adb)是多种用途的工具,该工具可以帮助你你管理设备或模拟器 的状态。 adb ( Android Debug Bridge)是一个通用命令行工具,其允许您与模拟器实例或连接的 Android 设备进行通信。它可为各种设备操作提供便利,如安装和调试…...
【力扣数据库知识手册笔记】索引
索引 索引的优缺点 优点1. 通过创建唯一性索引,可以保证数据库表中每一行数据的唯一性。2. 可以加快数据的检索速度(创建索引的主要原因)。3. 可以加速表和表之间的连接,实现数据的参考完整性。4. 可以在查询过程中,…...
FFmpeg 低延迟同屏方案
引言 在实时互动需求激增的当下,无论是在线教育中的师生同屏演示、远程办公的屏幕共享协作,还是游戏直播的画面实时传输,低延迟同屏已成为保障用户体验的核心指标。FFmpeg 作为一款功能强大的多媒体框架,凭借其灵活的编解码、数据…...
1688商品列表API与其他数据源的对接思路
将1688商品列表API与其他数据源对接时,需结合业务场景设计数据流转链路,重点关注数据格式兼容性、接口调用频率控制及数据一致性维护。以下是具体对接思路及关键技术点: 一、核心对接场景与目标 商品数据同步 场景:将1688商品信息…...
基于当前项目通过npm包形式暴露公共组件
1.package.sjon文件配置 其中xh-flowable就是暴露出去的npm包名 2.创建tpyes文件夹,并新增内容 3.创建package文件夹...
JavaScript 数据类型详解
JavaScript 数据类型详解 JavaScript 数据类型分为 原始类型(Primitive) 和 对象类型(Object) 两大类,共 8 种(ES11): 一、原始类型(7种) 1. undefined 定…...
Proxmox Mail Gateway安装指南:从零开始配置高效邮件过滤系统
💝💝💝欢迎莅临我的博客,很高兴能够在这里和您见面!希望您在这里可以感受到一份轻松愉快的氛围,不仅可以获得有趣的内容和知识,也可以畅所欲言、分享您的想法和见解。 推荐:「storms…...
Oracle11g安装包
Oracle 11g安装包 适用于windows系统,64位 下载路径 oracle 11g 安装包...
