Vue + Element UI 实现单选框

发布时间:2026/10/30 3:21:05

Vue + Element UI 实现单选框 目录一. 需求描述需求分析1.1 需求描述1.2 需求分析二. 代码实现2.1 原代码分析2.2 新增代码编写三. 效果展示3.1 需求目标一验证3.2 需求目标二验证3.3 需求目标三验证3.4 需求目标四验证3.5 需求目标五验证3.6 需求目标六验证一. 需求描述需求分析1.1 需求描述如下图所示是一个销售管理后台的修改页面我类似地将各个字段的含义做了简单的标注Producer 大家可以理解为销售人员(中介)假设一件商品是由甲、乙、丙......等非单独一个人推销卖出的产品那么这些人中就有一个一定付出的努力最大所以得到的报酬也应该最多。因此现在要新增一个主附Producer单选框标记出来谁是主Producer方便后台计算员工薪酬奖金。要求一如果只有一个人员则默认为主Producer要求二付出成果占比低的人同样可以成为主ProducerShare Commission与Share Production均可以为0但是在提交的时候需要弹出中断流程的提示框进行提示经确认后才可提交审核要求三同一个团队下各个Producer人员的Share Commission与Share Production值相加应等于100要求四默认情况下只有同一个团队下人员之间可以更换主Producer非同一个团队不能随意更换主Producer要求五选择对应的主Producer时对应的销售人员团队编码和员工编码不能为空要求六拆分的Producer不能相同例如不能第一行是员工A第二行拆分时还是添加的员工A员工不可重复1.2 需求分析1因为不同的团队可能出现相同的员工姓名编码例如A团队001的张三B团队001的张三员工编号可能重复不一定唯一所以我们不能将员工编号直接作为单选框的值但是如果使用团队码值员工码值就可以确定一个唯一的销售人员了不能只我们将字段名称就定义为mainProducer2:label标签采用的是中文文国际版所以字段名称单独定义在其它文件作为Vue组件导入到此因此我们在定义的时候也遵循这种设计理念将添加的主Producer也在对应的文件中进行添加3因为是单选框通过点击来进行选择主Producer所以我们可以给单选框绑定一个 change 函数每当我的单选发生改变时就去执行 change 函数这样一来我们就可以将需求要求的校验写在 change 函数中二. 代码实现2.1 原代码分析如下代码是原本页面上已经存在的代码可以看出一个表格所以我们直接在原有的表格中添加即可。表格中采用了 ElementUI 表格组件:datasplitForm.splitData绑定表格数据源来自splitForm对象的splitData数组 数据来源通常为后端返回slot-scopescope:是 Vue.js 和 ElementUI 中用于表格列模板的一种特殊语法它提供了对当前行数据的访问能力scope 对象内容scope.row当前行的数据对象scope.$index当前行的索引从0开始scope.column当前列的信息scope.store表格的存储状态el-table :datasplitForm.splitData :header-cell-style{text-align:center} :cell-style{text-align:center} stylewidth: 100%; !-- 渠道类型 -- el-table-column :label$t(feeSplit.feeSplit.qudaoleixing) width180px template slot-scopescope el-form-item label-width-120px el-form-item :propsplitData. scope.$index .businessNature :rulespageRules.businessNature code-select v-modelscope.row.businessNature code-typeChannelTypeLv1 :disabled!lookShowBtu || scope.row.dataFlag 1 || scope.row.firstFlag clearable changeclearChild(scope.row, businessNature) / /el-form-item /el-form-item /template /el-table-column !-- producer -- el-table-column :label$t(feeSplit.feeSplit.Procucer) width140px template slot-scopescope el-form-item label-width-120px el-form-item :propsplitData. scope.$index .producerCode :rulespageRules.producerCode code-search v-modelscope.row.producerCode show-code :show-namefalse code-typeProducerInPolicy :other-conditions{ ChannelTypeLv1:scope.row.businessNature, ChannelTypeLv2:scope.row.businessNatureSub, comCode: scope.row.policyComCode, riskCode:scope.row.riskCode,dsProducer:scope.row.dsProducer } :disabled!lookShowBtu || scope.row.dataFlag 1 || scope.row.firstFlag changerefFillProducer(scope.$index, ...arguments), clearChild(scope.row, producerCode) / /el-form-item /el-form-item /template /el-table-column !-- producer name -- el-table-column :label$t(feeSplit.feeSplit.ProcucerName) width180px template slot-scopescope el-input v-modelscope.row.producerName :titlescope.row.producerName disabled / /template /el-table-column !-- 渠道细分 -- el-table-column :label$t(feeSplit.feeSplit.qudaoxifen) width180px template slot-scopescope el-form-item label-width-120px el-form-item :propsplitData. scope.$index .businessNatureSub :rulespageRules.businessNatureSub code-select v-modelscope.row.businessNatureSub code-typeChannelTypeLv2 :parent-codescope.row.businessNature :disabled!lookShowBtu || scope.row.dataFlag 1 || scope.row.firstFlag clearable changeclearChild(scope.row, businessNatureSub) / /el-form-item /el-form-item /template /el-table-column !-- 经办人代码 -- el-table-column :label$t(feeSplit.feeSplit.yewuyuandaima) width120px template slot-scopescope el-form-item label-width-120px el-form-item :propsplitData. scope.$index .producerSubCode :rulespageRules.producerSubCode code-search v-modelscope.row.producerSubCode show-code :show-namefalse code-typeHandlerInPolicyModel :include-codesscope.row.handleCodeList :other-conditions{ ChannelTypeLv1:scope.row.businessNature, ChannelTypeLv2:scope.row.businessNatureSub, Producer:scope.row.producerCode } :disabled!lookShowBtu || scope.row.dataFlag 1 || scope.row.firstFlag || scope.row.subFlag changerefFillHandler(scope.$index, ...arguments), clearChild(scope.row, producerSubCode) / /el-form-item /el-form-item /template /el-table-column !-- 经办人名称 -- el-table-column :label$t(feeSplit.feeSplit.yewuyuanmingcheng) width180px template slot-scopescope el-input v-modelscope.row.producerSubName disabled / /template /el-table-column !-- 销售团队 -- el-table-column :label$t(feeSplit.feeSplit.xiaoshoutuandui) width150px template slot-scopescope code-search v-modelscope.row.teamCode code-typeTeamCodeHis disabled / /template /el-table-column !-- 业绩归属机构 -- el-table-column :label$t(feeSplit.feeSplit.yejiguishujigou) width120px template slot-scopescope code-trans v-modelscope.row.comCode code-typeCompany / !-- code-search v-modelscope.row.comCode code-typeCompany disabled /-- /template /el-table-column !-- share commission -- el-table-column :label$t(feeSplit.feeSplit.ShareCommission) width150px template slot-scopescope el-input-number v-modelscope.row.shareCommission :disabled!lookShowBtu :precision2 :controlsfalse stylewidth: 100px changenumberChange(scope.row.shareCommission,scope.$index,0) / /template /el-table-column !-- share production -- el-table-column :label$t(feeSplit.feeSplit.ShareProduction) width150px template slot-scopescope el-input-number v-modelscope.row.shareProduction :disabled!lookShowBtu :precision2 :controlsfalse stylewidth: 100px changenumberChange(scope.row.shareProduction,scope.$index,1) / /template /el-table-column /el-table2.2 新增代码编写:lable 标签导入的文件如下所示展示部分我们新添加的单选框名称也在这个文件中添加即可export default { mainProducerFlag: 主Producer, // 新增主Producer标识作为表格列名 qudaoleixing: 渠道类型, Procucer: Producer, ProcucerName: Producer Name, ShareCommission: Share Commission, ShareProduction: Share Production ...... }新添加单选框主体代码我们就直接加在表格table代码块最上面即可然后我们在 Vue 的 data 数据中添加 v-mode 绑定的 selectMainProducerCode 属性!-- 主producer标识单选框 -- el-table-column :label$t(feeSplit.feeSplit.mainProducerFlag) width130px aligncenter template slot-scopescope el-radio v-modelselectMainProducerCode :labelgetMainProducerKey(scope.row.producerCode, scope.row.producerSubCode) changeupdateMainAgent(scope.row.producerCode, scope.row.producerSubCode,scope.row.businessNature) /el-radio /template /el-table-column data() { return { selectMainProducerCode: null, // 费用拆分单选主Producer对应的十位码值(producerCode handlerCode) prevSelectMainProducerCode: null, // 上一次单选选择的主Producer对应的十位码值(producerCode handlerCode) selectMainBusinessNature: null, // 主Producer的渠道类型 prevSelectMainBusinessNature: null, // 上一次单选选择的主Producer对应的渠道类型 } }, methods: { // 在 methods 中添加单选标签值计算函数动态的根据每一行的团队编码员工编码得出一个唯一标识 // 用于与 data 中的 selectMainProducerCode匹配相同则被选中不通则不被选中 getMainProducerKey(producerCode,producerSubCode) { return producerCode producerSubCode }, // 编写绑定的 change 函数函数传递三个参数 // 参数一选中行的团队编码用于计算十位唯一值 // 参数二选中行的员工编码用于计算十位唯一值 // 参数三选中行的渠道类型用于判断新选的Producer是否与原Producer渠道相同实现需求的要求四 // 结合上面的getMainProducerKey方法得出的十位唯一值就可以判断当前行对应的员工是否被选中 updateMainAgent(producerCode,handlerCode,businessNature){ // 1. 判空.若为空则提示请选择 Producer, // 此步判断实现需求的要求五对应的销售人员团队编码和员工编码不能为空 if (!producerCode || producerCode || !handlerCode || handlerCode ) { this.$message({ type: warning, message: this.$t(请先选择Producer和经办人代码) }) // 2. 弹出提示框后回滚到上一个选择的 Producer, 然后 return 返回 this.$nextTick(() { this.selectMainProducerCode this.prevSelectMainProducerCode }); return; } // 3. 判断新选择的主Producer渠道类型需与原主Producer渠道类型是否一致 // 此步判断实现需求的要求四非同一个团队不能随意更换主Producer if (this.prevSelectMainBusinessNature ! null this.prevSelectMainBusinessNature ! businessNature){ this.$message({ type: warning, message: this.$t(新选择的主Producer渠道类型需与原主Producer渠道类型一致) }) // 4. 弹出提示框后回滚到上一个选择的 Producer 和 渠道类型值 this.$nextTick(() { this.selectMainBusinessNature this.prevSelectMainBusinessNature this.selectMainProducerCode this.prevSelectMainProducerCode }); return; } // 5. 若选择的 Producer 非空,将此次选择的 Producer 保存用于后续回滚操作使用, 上一次选中的Producer的Id值 this.prevSelectMainProducerCode producerCode handlerCode this.selectMainProducerCode producerCode handlerCode }, // 弹出费用拆分弹窗 feeSplit() { const splitForm { splitData: [{ businessNature: this.selection[0].businessNature, businessNatureSub: this.selection[0].businessNatureSub, producerCode: this.selection[0].producerCode, producerName: this.selection[0].producerName, producerSubCode: this.selection[0].handlerCode, producerSubName: this.selection[0].handlerName, teamCode: this.selection[0].teamCode, teamName: this.selection[0].teamName, comCode: this.selection[0].branchCode, // producer的归属机构 即保单的业绩归属机构对应 shareCommission: 100, shareProduction: 100, permitNo: , permitName: , mobile: , interAddress: , // comCode: this.selection[0].comCode, riskCode: this.selection[0].riskCode, // 产品 firstFlag: true, // 第一行数据 subFlag: false, // 经办人是否可修改 handelCodeList: [] }] } // 下面四行代码用于初始化值实现需求的要求一“如果只有一个人员则默认为主Producer”。 // 未进行拆分说明只有一个人默认选择第一条记录作为主Producer this.selectMainProducerCode this.selection[0].producerCode this.selection[0].handlerCode // 未进行拆分说明只有一个人默认选择第一条记录作为主Producer 记录值用于回滚操作 this.prevSelectMainProducerCode this.selection[0].producerCode this.selection[0].handlerCode // 记录当前的渠道类型值用于判断新选择的Producer渠道类型是否与当前渠道类型值相同 this.selectMainBusinessNature this.selection[0].businessNature // 记录当前的渠道类型值用于回滚操作 this.prevSelectMainBusinessNature this.selection[0].businessNature }, // 保函数 submitButton(passCommpaymentCheck) { this.$refs[splitForm].validate(async valid { // 这里我只编写了新增的逻辑其它原有的表单逻辑已省略...... // Share Commission总和必须为100%供后续作为标识做判断 let comNum 0 // Share Production总和必须为100%供后续作为标识做判断 let proNum 0 // 拆分数据producer相同标识 let proFlag false // 主Producer的shareCommission,shareProduction均为0标识 let mainProducerShareCommissionAndShareProductionIsZeroFlag false // 循环遍历修改后的集合数据计算shareCommissionshareProduction各自的总和 this.splitForm.splitData.forEach(element { if (element.producerCode element.producerSubCode this.selectMainProducerCode){ if (element.shareCommission 0 element.shareProduction 0){ mainProducerShareCommissionAndShareProductionIsZeroFlag true } element.agentFlag 1 }else { element.agentFlag 0 } comNum math.add(comNum, (element.shareCommission undefined || element.shareCommission ) ? 0 : element.shareCommission) proNum math.add(proNum, (element.shareProduction undefined || element.shareProduction ) ? 0 : element.shareProduction) if ((element.shareCommission undefined || element.shareCommission ) (element.shareProduction undefined || element.shareProduction )) { numFlag true return } // 比较所有拆分对象是否有重复 numsIn 0 this.splitForm.splitData.forEach(elementIn { if (nums ! numsIn elementIn.producerCode element.producerCode elementIn.producerSubCode element.producerSubCode) { proFlag true return } numsIn }) nums }) // 若主Producer的shareCommission,shareProduction均为0则弹出提示框 // 这一步判断实现了需求的要求二 if (mainProducerShareCommissionAndShareProductionIsZeroFlag){ try { await this.$confirm( this.$t(请注意主Producer的share Commission和share Production都为0请确认是否提交), this.$t(请确认), { confirmButtonText: this.$t(common.ok), cancelButtonText: this.$t(common.close), type: warning } ); } catch (error) { // 用户点击了取消或关闭弹窗 return; } } // 这一步判断实现了需求的要求六员工不可重复拆分 // 判断拆分数据的producer是否相同相同弹出提示框 if (proFlag) { this.$message({ type: warning, message: this.$t(拆分数据的producer不能相同!) }) return } // 这一步判断实现了需求的要求三Share Commission值相加应等于100 // shareCommission总和必须为100%不为100则弹出提示框 if ((comNum ! 100)) { this.$message({ type: warning, message: this.$t(Share Commission总和必须为100%!) }) return } // 这一步判断实现了需求的要求三Share Production值相加应等于100 // shareProduction总和必须为100%不为100则弹出提示框 if ((proNum ! 100)) { this.$message({ type: warning, message: this.$t(Share Production总和必须为100%!) }) return } }) }, }三. 效果展示3.1 需求目标一验证选中一条数据进入到拆分页面可以发现如果只有一条数据那么会默认选中成为主Producer需求要求一验证成功3.2 需求目标二验证如下图所示我新增一条数据并设置为主Producer将他的Share Commission和Share Production全设置为0点击提交如下弹窗要求我们进一步确认方可提交需求要求二验证成功3.3 需求目标三验证如下我新增两条数据对其Share Commission和Share Production进行设置但相加不为100点击提交触发了 submit 提交校验弹出提示框需求要求三验证成功3.4 需求目标四验证我尝试更换团队编码点击选择另一个团队的销售人员作为主Producer如下图所示在点击的时候因为触发了 change 函数校验团队编码(渠道类型)必须相同否则无法选择需求要求四验证成功。3.5 需求目标五验证如下图所示我新增一条数据但是不选择 Producer点击单选框触发了 change 函数判空校验弹出一条提示信息提示要先选择Producer和经办人代码需求要求五验证成功。3.6 需求目标六验证我新增一条相同的Producer因为默认选择第一条作为主Producer所以新增的第二条也被选中但是没关系我们尝试提交尝试提交触发了 submit 提交函数校验如下图所示。也弹出中断提示框要求拆分的数据Producer不能相同需求要求六验证成功。
延伸阅读

更多相关文章

2026/10/30 11:38:40

浏览器真的能变成你的下一台电脑吗?探索OS.js的Web桌面革命

浏览器真的能变成你的下一台电脑吗?探索OS.js的Web桌面革命 【免费下载链接】OS.js OS.js - JavaScript Web Desktop Platform 项目地址: https://gitcode.com/gh_mirrors/os/OS.js 想象一下,打开浏览器就能拥有一个完整的桌面操作系统——不是远…

2026/10/25 0:11:09

大语言模型多极化时代:技术路线分化与工程实践指南

如果你还在用"GPT-4领先,其他模型追赶"的思维看待大语言模型格局,那么你可能已经落后了。年底这一波密集发布的前沿模型,正在彻底重塑LLM的竞争版图——从单一巨头主导转向真正的多极化时代。过去一年,我们看到的不只是…

2026/10/30 11:38:45

齿轮箱故障诊断:从zip数据解压到特征工程避坑指南

简介:面向齿轮箱故障诊断与预测性维护场景的开放数据集,共十五个文件、约5.91MB,适合设备健康管理、机器学习建模和故障特征提取方向的研究者与工程师使用。包内以mat格式保存振动原始数据,涵盖正常、单齿崩齿、三齿磨损三种典型工…

2026/10/30 11:38:45

声源定位算法MATLAB实现:GCC与MUSIC路线选择与避坑指南

简介:这份资源面向信号处理、声学与通信方向的学习者和研究者,聚焦声源定位算法的MATLAB实现,涵盖广义互相关(GCC)与空间谱估计两条主流技术路线,并引入简单房间回响模型以应对实际环境中的反射与衰减干扰。…

2026/10/30 11:38:45

Agentic AI实践指南:核心组件、构建路径与避坑排查

简介:这份PDF资料面向技术开发者、企业决策者与AI研究者,系统梳理Agentic AI从概念原理到落地实践的知识框架,帮助读者理解其自主规划、记忆与工具调用能力,并掌握在亚马逊云科技上构建应用的方法论。内容涵盖Agentic AI的技术原理…

2026/10/30 11:38:45

UG NX外部开发核心原理:UF_* API与内核直驱建模协议

简介:本资源是一份面向UG NX二次开发初学者的实战入门材料,聚焦外部开发模式下通过C调用Open API创建基础几何体的核心流程。内容以完整可编译的NX9工程代码为核心,涵盖API环境初始化、新建公制零件、原点处创建Block、保存与关闭模型等关键步…

2026/10/30 11:38:45

Ubuntu 24.04内网APT源搭建:apt-mirror同步与Nginx发布避坑指南

简介:本资源是一份面向企业级Linux运维人员与IT系统管理员的内网APT源搭建实战指南,聚焦Ubuntu Server 24.04.1(Noble)环境下离线软件仓库的自主构建与安全发布。文档系统覆盖环境准备、双源配置(阿里云/清华镜像站&am…

2026/10/29 17:05:52

全国计算机二级C语言真题PDF高效刷题指南

简介:这份PDF是全国计算机二级C语言考试的试题与答案解析,专为备考二级C语言的考生准备。内容涵盖C语言特点与设计理念、程序基本组成单位、main函数执行流程、函数嵌套调用规则、标识符命名规范、整型实型字符型数据类型、八进制十六进制数合法形式、运…

2026/10/29 17:06:02

requests+BeautifulSoup:爬虫抓取h1标题的入门实战指南

简介:一份以实战为导向的Python爬虫入门资源,面向具备基本编程基础、希望快速掌握网络数据抓取的初学者,可解决从零编写简单爬虫的常见困惑。实例以requests库发送HTTP请求、BeautifulSoup解析HTML文档,完整演示从网页中提取 标题…

2026/10/29 17:06:13

递推最小二乘参数辨识MATLAB编程:二阶系统与渐消记忆实现

简介:这是一篇面向控制系统、信号处理与机器学习等领域学习者的最小二乘参数辨识程序说明文档。资源共包含一个docx格式文档,压缩包大小七十八KB,内容围绕递推最小二乘算法和渐消记忆最小二乘算法展开:先构造一个极点位于单位圆内…

2026/10/30 0:00:46

NCP1342高频准谐振反激控制器:从设计约束到验收的完整方案

简介:NCP1342 高频准谐振反激控制器应用介绍译文文档,面向电源设计工程师、硬件研发人员及开关电源学习者,帮助读者跨越英文原版资料的语言门槛,系统理解这款高集成度 QR Flyback 控制器的设计要点。文档围绕芯片核心特性展开&…

2026/10/30 0:00:46

NCP1342准谐振反激电源设计:从选型到验收的全流程解析

简介:这份文档面向电源设计工程师与开关电源学习者,系统梳理NCP1342高频准谐振反激控制器的应用要点,帮助读者快速理解这款高集成度离线电源变换器控制芯片的工作机制与设计方法。内容涵盖芯片核心特性、引脚功能定义、电气特性参数表、最大额…

2026/10/30 0:00:46

Web系统自动登出机制设计与实现全解析

1. 系统自动登出机制的设计初衷在各类Web应用和后台管理系统中,自动登出功能早已成为标配。这个看似简单的功能背后,实际上涉及用户安全、资源管理和体验优化的多重考量。作为从业十多年的老开发,我见过太多因为登出逻辑设计不当导致的安全事…

还想了解更多?直接咨询顾问

免费诊断 + 免费方案 + 透明报价。

全国咨询热线400-8866-253
免费获取方案
☎咨询二维码 ☎ ↑