【泛微OA】泛微OA平台实现计算具体的天数
最近在泛微OA上搭建了一套表单,将信息保存进去之后,发现【校准天数】的值是写死的,无法根据【有效期】变化。所以本文实现在泛微OA平台计算【校准天数】的值。具体如下:
一、表单部分显示页面
二、字段:
(1)有效期:field19071
(2)距离检定/校准天数: field19059
三、功能功能:距离检定/校准天数 = 有效期 - 当前时间
四、代码实现
jQuery(document).ready(function(){// 目标字段标识(天数差要赋值的字段)const targetFieldMark = "field19059";// 有效期字段标识const yxqFieldMark = "field19071";// 计算日期差的核心函数(抽离为独立函数,便于复用)function calculateAndUpdateDiff() {// 获取有效期的值var yxq = ModeForm.getFieldValue(yxqFieldMark);console.log("有效期:", yxq);// 获取当前日期并格式化为 "YYYY-MM-DD"const now = new Date();const year = now.getFullYear();const month = String(now.getMonth() + 1).padStart(2, '0');const day = String(now.getDate()).padStart(2, '0');const currentDateStr = `${year}-${month}-${day}`;console.log("当前日期:", currentDateStr);// 处理有效期为空的情况if (!yxq) {console.warn("未获取到有效期值");ModeForm.changeFieldValue(targetFieldMark, { value: "" }); // 清空目标字段return;}// 转换为Date对象(兼容不同格式的日期字符串)const yxqDate = new Date(yxq);const currentDate = new Date(currentDateStr);// 验证日期有效性if (isNaN(yxqDate.getTime()) || isNaN(currentDate.getTime())) {console.error("日期格式错误,无法计算(请确保格式为YYYY-MM-DD)");ModeForm.changeFieldValue(targetFieldMark, { value: "格式错误" });return;}// 计算天数差(使用Math.ceil确保不足一天也按一天计算,可根据需求改为floor/round)const timeDiffMs = yxqDate - currentDate;const daysDiff = Math.ceil(timeDiffMs / (1000 * 60 * 60 * 24));console.log("天数差(有效期 - 当前日期):", daysDiff);// 赋值给目标字段(触发联动)ModeForm.changeFieldValue(targetFieldMark, {value: daysDiff.toString()});}// 页面初始化时计算一次calculateAndUpdateDiff();// 绑定有效期字段变化事件,实现动态更新ModeForm.bindFieldChangeEvent(yxqFieldMark, function(dom, fieldMark, newValue) {console.log(`有效期字段${fieldMark}值已变更为:`, newValue);calculateAndUpdateDiff(); // 重新计算并更新});
});
代码写入:泛微后台—建模引擎—模块—基础—显示布局—插入—代码块。