Vue + Element UI 实现单选框

发布时间:2026/9/12 6:56:29

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/9/10 10:14:53

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

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

2026/9/8 5:51:01

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

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

2026/9/12 6:55:01

深度学习与蒙特卡洛树搜索:围棋AI Python源码解析

简介:一套基于深度学习和蒙特卡洛树搜索的围棋机器人Python源码,面向计算机相关专业毕业设计、课程设计和期末大作业场景,也适合需要项目实战练习的学习者。项目融合深度神经网络与蒙特卡洛树搜索,代码完整、经过导师指导并获评99…

2026/9/12 6:55:01

CLAUDE.md:AI协作项目的结构化记忆中枢设计

1. 项目概述:CLAUDE.md 如何成为AI项目的"记忆中枢"在多人协作的AI项目开发中,最头疼的问题莫过于"规范失忆"——新加入的开发者总要反复询问"这个参数为什么设0.7?""那段异常处理逻辑是谁加的&#xff1…

2026/9/12 6:50:01

macOS 应用精选集 awesome-macOS:告别盲目找软件的烦恼

macOS 应用精选集 awesome-macOS:告别盲目找软件的烦恼 【免费下载链接】awesome-macOS  A curated list of awesome applications, softwares, tools and shiny things for macOS. 项目地址: https://gitcode.com/GitHub_Trending/aw/awesome-macOS 装个…

2026/9/12 2:05:33

超人会飞不算本事:系统稳定依赖清晰规则与边界设计

开头先不绕弯子。“#斯坦李吐槽dc 所以超人是无缘无故会飞的嘛哈哈哈哈哈哈哈锤哥真是技术人才啊!#雷神 #复联”这类调侃式短标题,第一波冲击力在于它把两个宇宙的角色塞进同一个吐槽箱里,但细想一下就能发现,它真正碰到的根本不是…

2026/9/12 3:55:12

超人VS蜘蛛侠:拆解超级IP的影响力与传播方法论

把“蜘蛛侠 vs 超人”放在 CSDN 上聊,可能很多人第一反应是走错片场了。但如果把这两个角色看成“两个持续运营了 80 多年的文化产品”,你会发现,这场比较本质上是两个不同 IP 策略的长期结果对比:超人赢在定义了整个超级英雄题材…

2026/9/9 16:31:09

基于CNN的调制信号识别:MATLAB实现时频图分类实战

简介:本资源是一套面向通信工程与信号处理方向学习者、研究者的深度学习实践方案,聚焦调制信号自动检测与识别这一典型无线通信任务,解决传统方法依赖人工特征、低信噪比下性能下降等痛点。压缩包共12个文件(10.73MB)&…

2026/9/12 0:04:17

MATLAB仿生优化框架:长鼻浣熊算法多策略融合实现

简介:本资源是一份面向智能优化算法研究者与MATLAB初学者的仿生智能算法实践代码包,聚焦于长鼻浣熊优化算法(COA)的多策略改进与性能验证。针对传统COA易陷局部最优、收敛精度不足等问题,作者融合Circle映射初始化提升…

2026/9/12 0:04:17

【JAVA毕设源码分享】基于 JavaWeb 的校园一卡通管理系统的设计与实现 基于 JavaWeb 的校园卡业务管理系统(程序+文档+代码讲解+一条龙定制)

博主介绍:✌️码农一枚 ,专注于大学生项目实战开发、讲解和毕业🚢文撰写修改等。全栈领域优质创作者,博客之星、掘金/华为云/阿里云/InfoQ等平台优质作者、专注于Java、小程序技术领域和毕业项目实战 ✌️技术范围:&am…

2026/9/12 0:04:17

【JAVA毕设源码分享】基于 Java 的图书馆借阅管理平台的搭建与实现 基于 Java 的图书馆综合管理系统(程序+文档+代码讲解+一条龙定制)

博主介绍:✌️码农一枚 ,专注于大学生项目实战开发、讲解和毕业🚢文撰写修改等。全栈领域优质创作者,博客之星、掘金/华为云/阿里云/InfoQ等平台优质作者、专注于Java、小程序技术领域和毕业项目实战 ✌️技术范围:&am…

2026/9/12 6:29:36

USB Type-C PCB布局分区设计:电源、高速信号与PD协议全攻略

做硬件这行,Type-C接口算是典型的“看着简单,做起来全坑”的东西。光引脚就24个,高低速信号、电源、控制线全部塞在一个小小的连接器里,如果PCB布局不做规划,打样回来基本就是“插上没反应”、“高速掉线”、“静电一打…

2026/9/10 15:19:50

系统编程学习原型如何补齐稳定性边界

系统编程学习原型如何补齐稳定性边界预算有限时&#xff0c;我先优化明显多余的复制&#xff0c;而不是猜测性地换容器。用借用传递只读数据通常就能减少分配&#xff1a; fn parse(line: &str) -> Result<Item, Error> { /* ... */ }用基准确认热点确实在分配&am…

2026/9/12 6:37:43

雨花区哪家财务公司代理记账比较好?

在雨花区&#xff0c;企业处理财税事务常常面临诸多挑战&#xff0c;选择一家靠谱的财务公司至关重要。湖南巨勤财务管理咨询有限公司就是本地正规实体财税服务机构&#xff0c;深耕本地工商财税行业多年&#xff0c;熟悉当地工商局、税务局最新政策与申报流程。主营公司注册、…

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

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

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