"use strict"
var g_device = "PC";
if (typeof (g_env) !== 'undefined') {
if (g_env && g_env.device === "pad") g_device = g_env.device;
else if (g_env && g_env.device === "phone") g_device = g_env.device;
}
function init() {
if(mediForm.fldCtrls["ganRaoZhiLiao"]){
monitCtrl(mediForm.fldCtrls["ganRaoZhiLiao"]);
}
}
// 监控
function monitCtrl(ctrl) {
var rowCount = ctrl.getValue().length;
var ctrlKey=ctrl.fieldInstance.key;
// 绑定已有数据
ctrl.bindRow();
if(ctrlKey=='yiZhiJi'){
ctrl.koValue().result().forEach(function(koItem){
yiZhiSubItem(koItem);
});
// 绑定新增数据
ctrl.koValue().result.subscribe(function(newArr){
var newCount = newArr.length,
idx;
ctrl.bindRow();
for (idx = rowCount; idx < newCount; idx++) {
yiZhiSubItem(newArr[idx]);
}
rowCount = newCount;
});
}
if(ctrlKey=='baXiangYao'){
ctrl.koValue().result().forEach(function(koItem){
baXiangSubItem(koItem);
});
// 绑定新增数据
ctrl.koValue().result.subscribe(function(newArr){
var newCount = newArr.length,
idx;
ctrl.bindRow();
for (idx = rowCount; idx < newCount; idx++) {
baXiangSubItem(newArr[idx]);
}
rowCount = newCount;
});
}
if(ctrlKey=='ganRaoZhiLiao'){
ctrl.koValue().result().forEach(function(koItem){
subscribeSubItem(koItem);
});
// 绑定新增数据
ctrl.koValue().result.subscribe(function(newArr){
var newCount = newArr.length,
idx;
ctrl.bindRow();
for (idx = rowCount; idx < newCount; idx++) {
subscribeSubItem(newArr[idx]);
}
rowCount = newCount;
});
}
// 时间:
function subscribeSubItem(koItem) {
executeLogic(koItem);
koItem["zhouQi"].subscribe(function(){
executeLogic(koItem);
});
koItem["startTime"].subscribe(function(){
executeLogic(koItem);
});
jianGe(koItem)
koItem["jianGeZhouQi"].subscribe(function(){
jianGe(koItem);
});
koItem["jianGeStart"].subscribe(function(){
jianGe(koItem);
});
}
核心代码区:
需求:日期插架选择“开始时间”,输入周期:?月后,自动生成“结束时间”
function executeLogic(koItem) {
var sourceValue = koItem["startTime"]();
var zhouQiVal = parseInt(koItem["zhouQi"]());
if(sourceValue && !isNaN(zhouQiVal)){
// 获取开始时间的月份
var kaiShiYY=parseInt(sourceValue.split('-')[0]);
var kaiShiMM=parseInt(sourceValue.split('-')[1]);
var kaiShiDD=sourceValue.split('-')[2];
var tarMonth=kaiShiMM+zhouQiVal;
if(tarMonth<10 ){
koItem["endTime"](kaiShiYY+"-0" + tarMonth +"-"+kaiShiDD);
if(tarMonth==2 && parseInt(kaiShiDD)>=29){
if(kaiShiYY % 4 == 0 && (kaiShiYY % 100 != 0 || kaiShiYY % 400 == 0)){//闰年29
koItem["endTime"](kaiShiYY+"-0" + tarMonth +"-"+kaiShiDD);
}else{
koItem["endTime"](kaiShiYY+"-0" + (tarMonth+1) +"-"+"0"+(kaiShiDD-28));
}
}
}else if(tarMonth>12){
var years=Math.floor(tarMonth/12);
var months=tarMonth-12*years;
if(months<10){
months="0"+months;
}
koItem["endTime"](kaiShiYY+years +"-" +months+ "-"+kaiShiDD);
}else if(10<=tarMonth<=12 ){
koItem["endTime"](kaiShiYY+"-" + kaiShiMM +"-"+kaiShiDD);
}
}else{
koItem["endTime"]("");
}
}
function jianGe(koItem) {
var sourceValue = koItem["jianGeStart"]();
var zhouQiVal = parseInt(koItem["jianGeZhouQi"]());
if(sourceValue && !isNaN(zhouQiVal)){
// 获取开始时间的月份
var kaiShiYY=parseInt(sourceValue.split('-')[0]);
var kaiShiMM=parseInt(sourceValue.split('-')[1]);
var kaiShiDD=sourceValue.split('-')[2];
var tarMonth=kaiShiMM+zhouQiVal;
if(tarMonth<10 ){
koItem["jianGeEnd"](kaiShiYY+"-0" + tarMonth +"-"+kaiShiDD);
if(tarMonth==2 && parseInt(kaiShiDD)>=29){
if(kaiShiYY % 4 == 0 && (kaiShiYY % 100 != 0 || kaiShiYY % 400 == 0)){//闰年29
koItem["jianGeEnd"](kaiShiYY+"-0" + tarMonth +"-"+kaiShiDD);
}else{
koItem["jianGeEnd"](kaiShiYY+"-0" + (tarMonth+1) +"-"+"0"+(kaiShiDD-28));
}
}
}else if(tarMonth>12){
var years=Math.floor(tarMonth/12);
var months=tarMonth-12*years;
if(months<10){
months="0"+months;
}
koItem["jianGeEnd"](kaiShiYY+years +"-" +months+ "-"+kaiShiDD);
}else if(10<=tarMonth<=12 ){
koItem["jianGeEnd"](kaiShiYY+"-" + kaiShiMM +"-"+kaiShiDD);
}
}else{
koItem["jianGeEnd"]("");
}
}