当前位置: 首页 > news >正文

Vue3 频率范围输入失焦自动校验实现

在这里插入图片描述
@[toc]

一、背景介绍

在这里插入图片描述

​​需求描述:​​

在Vue3项目中使用Arco Design插件,存在一个包含频率范围输入字段的表单页面。该频率范围输入值需根据所选中转台型号进行边界校验,具体规则如下:

1.系统支持四种中转台型号:

  • V1型号:有效频率范围为136-174兆赫兹

  • U1型号:有效频率范围为400-470兆赫兹

  • U2型号:有效频率范围为450-527兆赫兹

  • U3型号:有效频率范围为350-400兆赫兹

2.实现功能要求:

  • 当用户在频率输入框失焦(blur事件)时,自动执行范围校验

  • 若输入值小于当前型号的最小允许值,则自动修正为最小值

  • 若输入值大于当前型号的最大允许值,则自动修正为最大值

  • 修正操作需同步更新输入框显示值,并提供适当的用户反馈

代码片段分2种:

  • 第一种:固定的单段范围,比如:136-174兆赫兹
  • 第二种:多段范围:比如130-140,155-160

结论:之所以分2种代码,是因为多段范围用单段的带啊吗无法实现,因此实现了2套方法。

二、代码

2.1 针对单段范围

单段范围情况:
V1(136-174兆林兹)
U1(400-470兆赫)
U2(450-527兆赫兹)
U3(350-400兆赫兹)

<a-table-columndataIndex="cvTMixedChannelRXFreq":title="$t('MixedChannel_RX_Frequency')":tooltip="true":width="120"
><template #cell="{ rowIndex, record }"><a-input-numberv-model="record.cvTMixedChannelRXFreq"style="width: 120px":precision="6"step="0.000001":min="freqRange.min":max="freqRange.max"></a-input-number></template>
</a-table-column>
<a-table-columndataIndex="cvTMixedChannelTXFreq":title="$t('MixedChannel_TX_Frequency')":tooltip="true":width="120"
><template #cell="{ rowIndex, record }"><a-input-numberv-model="record.cvTMixedChannelTXFreq"style="width: 120px":precision="6"step="0.000001":min="freqRange.min":max="freqRange.max"></a-input-number></template>
</a-table-column>// 新增频率范围响应式变量
const freqRange = reactive({min: 0,max: 1000,
});// 获取设备信息并设置频率范围
const getDeviceInfoMixed = async () => {try {const response = await getParamList({sn: rssiId.value,type: 3,});commonResponse({response,onSuccess: () => {const modelNo = response.data.modelNo || "";// 根据型号设置频率范围if (modelNo.includes("V1")) {Object.assign(freqRange, { min: 136, max: 174 });} else if (modelNo.includes("U1")) {Object.assign(freqRange, { min: 400, max: 470 });} else if (modelNo.includes("U2")) {Object.assign(freqRange, { min: 450, max: 520 });} else if (modelNo.includes("U3")) {Object.assign(freqRange, { min: 350, max: 400 });} else if (modelNo.includes("U5")) {Object.assign(freqRange, { min: 809, max: 941 });} else {// 默认范围Object.assign(freqRange, { min: 0, max: 1000 });}},});} catch (error) {console.error("Failed to get device info:", error);}
};

2.2 针对多段范围

V1(136-174兆林兹)
U1(400-470兆赫)
U2(450-527兆赫兹)
U3(350-400兆赫兹)

U5接收(模拟/数字/混合):851MHz870MHz、935941MHz
U5发射(模拟/数字/混合):806825MHz、851MHz870MHz、896902MHz、935941MHz
当出现不在范围内的机型时,点击写入按钮后弹窗“无效机型!系统仅支持:U1/U2/U3/U5/V1"

<a-table-columndataIndex="cvTMixedChannelRXFreq":title="$t('MixedChannel_RX_Frequency')":tooltip="true":width="120"
><template #cell="{ rowIndex, record }"><a-input-numberv-model="record.cvTMixedChannelRXFreq"style="width: 120px":precision="6"step="0.000001"@blur="(e) => handleFrequencyBlur(e, record, 'receive', 'cvTMixedChannelRX')"></a-input-number></template>
</a-table-column>
<a-table-columndataIndex="cvTMixedChannelTXFreq":title="$t('MixedChannel_TX_Frequency')":tooltip="true":width="120"
><template #cell="{ rowIndex, record }"><a-input-numberv-model="record.cvTMixedChannelTXFreq"style="width: 120px":precision="6"step="0.000001"@blur="(e) => handleFrequencyBlur(e, record, 'transmit', 'cvTMixedChannelTX')"></a-input-number></template>
</a-table-column>
<div class="param-button">
<a-button:disabled="offlineDisabled"v-if="operationAuthorityDisabled"type="primary"class="huge"@click="setMixedChannelModeVal"style="margin-left: 12px">{{ $t("ParamWrite") }}</a-button
>
</div>const errorRangeOfRxtXFreq = ref(false);
const currentModel = ref('');
const currentModelArr = ref(["V1","U1","U2","U3","U5",]);const freqRanges = {V1: {receive: { ranges: [{ min: 136, max: 174 }], defaultValue: 136 },transmit: { ranges: [{ min: 136, max: 174 }], defaultValue: 136 }},U1: {receive: { ranges: [{ min: 400, max: 470 }], defaultValue: 400 },transmit: { ranges: [{ min: 400, max: 470 }], defaultValue: 400 }},U2: {receive: { ranges: [{ min: 450, max: 527 }], defaultValue: 450 },transmit: { ranges: [{ min: 450, max: 527 }], defaultValue: 450 }},U3: {receive: { ranges: [{ min: 350, max: 400 }], defaultValue: 350 },transmit: { ranges: [{ min: 350, max: 400 }], defaultValue: 350 }},U5: {receive: {ranges: [{ min: 851, max: 870 }, { min: 935, max: 941 }],defaultValue: 851},transmit: {ranges: [{ min: 806, max: 825 }, { min: 851, max: 870 }, { min: 896, max: 902 }, { min: 935, max: 941 }],defaultValue: 806}},default: {receive: { ranges: [{ min: 0, max: 1000 }]},transmit: { ranges: [{ min: 0, max: 1000 }]}}
};const handleFrequencyBlur = (e, record, type, label) => {const value = parseFloat(e.target.value);const config = freqRanges[currentModel.value]?.[type] || freqRanges.default[type];const isValid = config.ranges.some(range =>value >= range.min && value <= range.max);if (!isValid) {if (config.defaultValue) {nextTick(() => {record[`${label}Freq`] = config.defaultValue;})}}
};const validateRecord = (record) => {const isReceiveValid = validateFrequency(record.cvTMixedChannelRXFreq, 'receive');const isTransmitValid = validateFrequency(record.cvTMixedChannelTXFreq, 'transmit');return isReceiveValid && isTransmitValid;
};const validateFrequency = (value, type) => {if (!currentModel.value) return true;const config = freqRanges[currentModel.value]?.[type] || freqRanges.default[type];return config.ranges.some(range =>value >= range.min && value <= range.max);
};// 获取设备信息并设置频率范围
const getDeviceInfoMixed = async () => {try {const response = await getParamList({sn: rssiId.value,type: 3,});commonResponse({response,onSuccess: () => {const modelNo = response.data.modelNo || "";// 根据型号设置频率范围if (modelNo.includes("V1")) {currentModel.value = "V1";} else if (modelNo.includes("U1")) {currentModel.value = "U1";} else if (modelNo.includes("U2")) {currentModel.value = "U2";} else if (modelNo.includes("U3")) {currentModel.value = "U3";} else if (modelNo.includes("U5")) {currentModel.value = "U5";} else {errorRangeOfRxtXFreq.value = true;currentModel.value = "default";}},});} catch (error) {console.error("Failed to get device info:", error);}
};const setMixedChannelModeVal = () => {if (currentModelArr.value.includes(currentModel.value)) {mixedChannelList.value.forEach(record =>{if (!validateRecord(record)) {errorRangeOfRxtXFreq.value = true;}})}if (errorRangeOfRxtXFreq.value) {window.Message.error(t('ErrorRangeOfRxtXFreq'))return;}调用更新接口....
}

文章转载自:

http://zHBUf7cM.Lbssg.cn
http://5008AfO5.Lbssg.cn
http://xngtMnDt.Lbssg.cn
http://dn0z8p0U.Lbssg.cn
http://urgNkFSS.Lbssg.cn
http://rHVtV6DI.Lbssg.cn
http://W5Wc2wFY.Lbssg.cn
http://VxFCQGIy.Lbssg.cn
http://iKaLuRp5.Lbssg.cn
http://9DvdKgzQ.Lbssg.cn
http://ggSOmBBD.Lbssg.cn
http://nrYLaiWB.Lbssg.cn
http://xRRX3fG8.Lbssg.cn
http://nV1oT8s7.Lbssg.cn
http://krfZDhjI.Lbssg.cn
http://j5iIT59z.Lbssg.cn
http://IYZxAKYG.Lbssg.cn
http://vE2rRbno.Lbssg.cn
http://E7jz7rxY.Lbssg.cn
http://jbzhvo4q.Lbssg.cn
http://NHz0a4yD.Lbssg.cn
http://pQK2H7LZ.Lbssg.cn
http://ogXuQKIo.Lbssg.cn
http://6eoEd4rP.Lbssg.cn
http://aQKjoCVf.Lbssg.cn
http://SVQHy8Zu.Lbssg.cn
http://Uc1O3AE4.Lbssg.cn
http://exOeUU9b.Lbssg.cn
http://sOBNCqFS.Lbssg.cn
http://kwWjXTr9.Lbssg.cn
http://www.dtcms.com/a/368394.html

相关文章:

  • 删除元素(不是删除而是覆盖)快慢指针 慢指针是覆盖位置,快指针找元素
  • 代码随想录算法训练营第三天| 链表理论基础 203.移除链表元素 707.设计链表 206.反转链表
  • 结合机器学习的Backtrader跨市场交易策略研究
  • 前端开发vscode插件 - live server
  • 码农的“必修课”:深度解析Rust的所有权系统(与C++内存模型对比)
  • 【Python基础】 17 Rust 与 Python 运算符对比学习笔记
  • 云手机可以息屏挂手游吗?
  • 会话管理巅峰对决:Spring Web中Cookie-Session、JWT、Spring Session + Redis深度秘籍
  • 腾讯云大模型训练平台
  • iPhone17全系优缺点分析,加持远程控制让你的手机更好用!
  • 数据泄露危机逼近:五款电脑加密软件为企业筑起安全防线
  • 阿里云vs腾讯云按量付费服务器
  • DocuAI深度测评:自动文档生成工具如何高效产出规范API文档与数据库表结构文档?
  • React JSX 语法讲解
  • 工厂办公环境如何实现一台服务器多人共享办公
  • 从 0 到 1 学 sed 与 awk:Linux 文本处理的两把 “瑞士军刀”
  • VNC连接服务器实现远程桌面-针对官方给的链接已经失效问题
  • 【Web】理解CSS媒体查询
  • 编写前端发布脚本
  • 无密码登录与设备信任:ABP + WebAuthn/FIDO2
  • 消息队列-ubutu22.04环境下安装
  • Vue3源码reactivity响应式篇之EffectScope
  • 从Java全栈到前端框架:一位程序员的实战之路
  • 【Java实战㉖】深入Java单元测试:JUnit 5实战指南
  • 【AI论文】Robix:一种面向机器人交互、推理与规划的统一模型
  • C++(Qt)软件调试---bug排查记录(36)
  • yolov8部署在一台无显卡的电脑上,实时性强方案
  • Alibaba Cloud Linux 3 安装Docker
  • SQL面试题及详细答案150道(61-80) --- 多表连接查询篇
  • 详细解读Docker