KingScript轻脚本开发:自定义控件接口开发案例原创
8人赞赏了该文章
262次浏览
编辑于2024年11月28日 17:01:22
# 业务场景
苍穹6.0 及7.0之后更新KingScript脚本开发,使用的jar包范围,以及语法提示都比之前的KDE脚本方便很多
但是在对接自定义控件开发场景的时候,还是存在一些坑,比如自定义控件的类名kd.bos.ext.form.control.CustomControl ,是在ext路径下的,但是KingScript 不能正常import
在此给出KingScript开发对接自定义控件调试的一个简单案例
# KingScript 脚本参考
import { BeforeClosedEvent } from '@cosmic/bos-core/kd/bos/form/events' import { BusinessDataServiceHelper } from '@cosmic/bos-core/kd/bos/servicehelper' import { QFilter, QCP } from '@cosmic/bos-core/kd/bos/orm/query' import { Control } from '@cosmic/bos-core/kd/bos/form/control' /** * @author 蔡嘉盛 * @date 2024-10-29 */ import { AbstractBillPlugIn } from "@cosmic/bos-core/kd/bos/bill"; import { EventObject, ArrayList, HashMap, Calendar } from '@cosmic/bos-script/java/util'; import { PropertyChangedArgs } from "@cosmic/bos-core/kd/bos/entity/datamodel/events"; class MyPlugin extends AbstractBillPlugIn { // Event after data binding afterBindData(e: EventObject) { super.afterBindData(e); //... let calendar = Calendar.getInstance(); calendar.set(Calendar.HOUR_OF_DAY, 0); calendar.set(Calendar.MINUTE, 0); calendar.set(Calendar.SECOND, 0); calendar.set(Calendar.MILLISECOND, 0); let day = calendar.getTime(); this.getModel().setValue("kded_book_date",day) } propertyChanged(e: PropertyChangedArgs): void { super.propertyChanged(e); let key = e.getProperty().getName() if ("kded_book_date" === key){ let day = e.getChangeSet()[0].getNewValue() as Date if (day === null) { return } let meeting = this.getControl("kded_meeting") as CustomControl //过滤会议室,正常的会议期间 const dayFilter = new QFilter("kded_meeting_room.id",QCP.equals,this.getModel().getDataEntity().getPkValue()) dayFilter.and("kded_meeting_status", QCP.not_equals, "2") dayFilter.and("kded_order_start", QCP.large_equals, day).and("kded_order_end", QCP.less_than, new Date(day.getTime() + 24 * 60 * 60 * 1000)); let bookingList = BusinessDataServiceHelper.load("kded_meeting_order", "id,kded_order_start,kded_order_end,kded_meeting_title", dayFilter.toArray()) let bookingData = new ArrayList() for (let booking of bookingList) { let map = new HashMap() map.put("id", booking.getString("id")) map.put("title", booking.getString("kded_meeting_title")) map.put("start", booking.getString("kded_order_start")) map.put("end", booking.getString("kded_order_end")) bookingData.add(map) } let json = new HashMap() json.put("meeting", bookingData) json.put("day", day) json.put("enable", false) meeting.setData(json) } } customEvent(e: CustomEventArgs): void{ let name = e.getEventName() let args = e.getEventArgs() let start = args.split("#")[0] let end = args.split("#")[1] as string let dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm"); let startDay = dateFormat.parse(start) as Date let endDay = dateFormat.parse(end) as Date this.getModel().setValue("kded_order_start", startDay) this.getModel().setValue("kded_order_end", endDay) let diffMs = endDay.getTime() - startDay.getTime(); // 将时间差转换为小时 const diffHours = diffMs / (1000 * 60 * 60); this.getModel().setValue("kded_duration", diffHours) } beforeClosed(e: BeforeClosedEvent):void { e.setCheckDataChange(false) } } declare interface CustomControl extends Control{ getData(): string; getId(): string; setData(arg0: any): void; setId(arg0: string): void; } let plugin = new MyPlugin(); export { plugin };
在上面的代码中有几个重点关注内容
## 需要自己定义CustomControl类名
填写关键的setData() 接口就行,
使用时候直接指定类型 let meeting = this.getControl("kded_meeting") as CustomControl
declare interface CustomControl extends Control{ getData(): string; getId(): string; setData(arg0: any): void; setId(arg0: string): void; }
## 不可以基于TS语法构建JSON对象
CustomControl.setData() 接口一般是是用来向前端返回json对象,
但是经过测试发现,直接new JSON,或者用{} 创建的对象都是基于TS语法的,塞给setData之后会丢失数据,造成数据异常
建议需要使用JSON的地方还是老实的用Java的Array Map实现,参考下面的代码
let bookingData = new ArrayList() for (let booking of bookingList) { let map = new HashMap() map.put("id", booking.getString("id")) map.put("title", booking.getString("kded_meeting_title")) map.put("start", booking.getString("kded_order_start")) map.put("end", booking.getString("kded_order_end")) bookingData.add(map) } let json = new HashMap() json.put("meeting", bookingData) json.put("day", day) json.put("enable", false) meeting.setData(json)
## customEvent
customEvent 和Java开发的基本上是一样的
参考
customEvent(e: CustomEventArgs): void{ let name = e.getEventName() let args = e.getEventArgs() let start = args.split("#")[0] let end = args.split("#")[1] as string let dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm"); let startDay = dateFormat.parse(start) as Date let endDay = dateFormat.parse(end) as Date this.getModel().setValue("kded_order_start", startDay) this.getModel().setValue("kded_order_end", endDay) let diffMs = endDay.getTime() - startDay.getTime(); // 将时间差转换为小时 const diffHours = diffMs / (1000 * 60 * 60); this.getModel().setValue("kded_duration", diffHours) }
KingScript 脚本开发教程 https://vip.kingdee.com/article/474603833033832192?productLineId=29&isKnowledge=2&lang=zh-CN
赞 8
8人点赞
还没有人点赞,快来当第一个点赞的人吧!
打赏
0人打赏
还没有人打赏,快来当第一个打赏的人吧!
推荐阅读