如何使用倒计时控件实现单据在一段时间后自动保存原创
金蝶云社区-陈来珍
陈来珍
22人赞赏了该文章 4366次浏览 未经作者许可,禁止转载编辑于2023年12月13日 17:19:40

关键词:单据、定时、自动保存

一、需求背景

为避免网络异常或者页面关闭异常等原因导致用户填的一些单据数据没有及时保存,本案例实现单据在一段时间后实现自动保存。

二、实现方案

为单据添加倒计时控件,在倒计时结束时自动保存单据,并重新开启倒计时。

上传图片

三、实现过程

1、添加倒计时控件

上传图片


2、设置入口参数

指定定时保存的时间,单位/秒,这里为了方便调试,设置成了5秒自动保存

上传图片

3、后台实现自动保存的逻辑

public class AutoSaveBillPlugin extends AbstractBillPlugIn implements CountDownListener {
@Override
public void registerListener(EventObject e) {
//注册倒计时控件的监听
CountDown countdown = this.getView().getControl("sunp_countdownap");
countdown.addCountDownListener(this);
super.registerListener(e);
}
@Override//倒计时结束时,触发该事件
    public void onCountDownEnd(CountDownEvent evt) {
       OperateOption saveOp = OperateOption.create();
       // 取消原有的保存提示
    saveOp.setVariableValue(OperateOptionConst.ISSHOWMESSAGE, "false");
       OperationResult opReslut = this.getView().invokeOperation("save", saveOp);
if (opReslut.isSuccess()) {
           this.getView().showSuccessNotification("自动保存成功,如需取消,在应用菜单的入口参数中删除掉参数:auToSaveTimes");
           FormShowParameter formShowParameter = this.getView().getFormShowParameter();
           String isAuToSave = formShowParameter.getCustomParam("auToSaveTimes");
// 这里不再需要判断字符串是否为数组字符串
           Integer durationTime = Integer.valueOf(isAuToSave);
           CountDown countdown = this.getView().getControl("sunp_countdownap");
           // 设置倒计时时间
           countdown.setDuration(durationTime);
       }
       CountDownListener.super.onCountDownEnd(evt);
    }
    //打开单据时,根据入口参数设置的倒计时时长设置倒计时
    public void afterBindData(EventObject e) {
       FormShowParameter formShowParameter = this.getView().getFormShowParameter();
       String isAuToSave = formShowParameter.getCustomParam("auToSaveTimes");
if (isNumeric(isAuToSave)) {
           Integer durationTime = Integer.valueOf(isAuToSave);
           CountDown countdown = this.getView().getControl("sunp_countdownap");
// 设置倒计时时间
           countdown.setDuration(durationTime); 
// 启动倒计时
           countdown.start();       
}
    }
//判断字符串是否为数字字符串
    public boolean isNumeric(String s) { 
       if (s != null && !"".equals(s.trim()))
           return s.matches("^[0-9]*$");
       else
           return false;
    }
}

四、实现效果

在应用中打开业务单据,进入倒计时,倒计时结束时,单据就会自动保存。

上传图片

实际业务单据中可把倒计时控件设置为不可见,不能设置隐藏,设置隐藏会导致获取倒计时控件时,获取到的控件为空,抛异常。

  • 五、开发环境版本

COSMICV4.0.010.0

  • 六、参考资料

【开发平台】指导手册

学习成长中心


赞 22