视图逻辑脚本
在视图中,你可以通过脚本代码实现许多复杂的业务逻辑。如视图存在多个多数据部件时,可以在视图挂载时(onMounted)时调用这些部件的加载方法;也可以在视图重新激活(onActivated)时,刷新某一部件数据。视图逻辑支持绑定的事件可参考通用视图。
参数
名称 | 类型 | 描述 |
---|---|---|
document | Document | 当前文档对象 |
selector | (className: string) => HTMLCollectionOf<Element> | 元素选择器 |
env | IEnvironment | 当前环境对象 |
appSession | IApiData | 当前应用会话对象 |
topViewSession | IApiData | 当前顶级视图会话对象 |
viewSession | IApiData | 当前视图会话对象 |
context | IApiContext | 当前视图上下文 |
viewParam | IApiParams | 当前视图参数 |
data | IApiData[] | 当前业务数据 |
app | IApiAppHubController | 当前应用 |
topView | IApiViewController | 当前顶级视图 |
parentView | IApiViewController | undefined | 当前父视图 |
view | IApiViewController | 当前视图 |
parent | IApiViewController | undefined | 当前父视图 |
util | { message: IApiMessageUtil, notification: IApiNotificationUtil,modal: IApiModalUtil,confirm: IApiConfirmUtil,openView: IApiOpenViewUtil} | 工具集 |
ctrl | IApiControlController | undefined | 当前部件 |
调用示例
视图创建完成(onCreated)后监听控制器注册事件并修改控制器参数
typescript
view.ctx.evt.on("onRegister", (name, c) => {
if (name === "searchform") {
const time = new Date();
const preTime = new Date(time.getTime() - 604800000);
const n_create_time_gtandeq =
preTime.getFullYear() +
"-" +
(preTime.getMonth() + 1 < 10
? "0" + (preTime.getMonth() + 1)
: preTime.getMonth() + 1) +
"-" +
(preTime.getDate() < 10 ? "0" + preTime.getDate() : preTime.getDate());
const n_create_time_ltandeq =
time.getFullYear() +
"-" +
(time.getMonth() + 1 < 10
? "0" + (time.getMonth() + 1)
: time.getMonth() + 1) +
"-" +
(time.getDate() < 10 ? "0" + time.getDate() : time.getDate());
const date_range = n_create_time_gtandeq + "," + n_create_time_ltandeq;
Object.assign(c.params, {
n_create_time_gtandeq,
n_create_time_ltandeq,
date_range,
});
}
});
视图创建完成(onCreated)后监听控制器注册并修改控制器上下文
typescript
view.ctx.evt.on("onRegister", (name, c) => {
if (name === "insight_viewcustom_view") {
if (!c.context.dyna_dashboard) {
c.context.dyna_dashboard = view.context.dyna_dashboard;
}
if (!c.context.srfdynadashboardid) {
c.context.srfdynadashboardid = view.context.srfdynadashboardid;
}
}
});
视图挂载后(onMounted)设置快捷搜索的搜索参数
typescript
const searchBar = view.getController("searchbar");
if (searchBar) {
searchBar.state.query = view.params.srfquery ? view.params.srfquery : "";
}
视图挂载后(onMounted)跳转自定义视图
typescript
window.open("http://plm.ibizlab.cn/modeling/start/", "_self");
视图挂载后(onMounted)设置面板项的值
typescript
view.layoutPanel.panelItems.check_type.setDataValue("reader");
视图挂载后(onMounted)判断部件是否存在搜索参数,不存在则不允许加载数据
typescript
const xdataControl = view.getController(view.model.xdataControlName);
if (!xdataControl || xdataControl.enableLoad === undefined) {
return;
}
if (!viewParam.query) {
xdataControl.enableLoad = false;
}
视图挂载后(onMounted)根据表单激活分页控制容器的显隐
typescript
const form = view.getController("form");
if (form) {
form.evt.on("onFormDetailEvent", (event) => {
const panelItems = view.layoutPanel.panelItems;
panelItems.send_comment_container.state.visible = false;
if (!panelItems.comment_container) {
return;
}
if (form.details.tabpanel1) {
const activeTab = form.details.tabpanel1.state.activeTab;
panelItems.comment_container.state.visible = activeTab === "detailpage";
}
});
}
视图关闭前保存表单数据和传递自定义数据
typescript
view.modal.hooks.shouldDismiss.tapPromise(async (context) => {
const form = view.getController("form");
const uiDomain = ibiz.uiDomainManager.get(view.context.srfsessionid);
if (
form.data.title !== null &&
form.data.title !== "" &&
form.data.title !== undefined
) {
const isChange = form.state.modified || uiDomain.dataModification;
if (isChange && context.allowClose == null) {
if (form) {
await form.save({ silent: true });
context.allowClose = true;
}
} else {
if (form) {
await form.remove({ silent: true });
context.allowClose = true;
}
}
}
});
// 关闭前传递是否发布参数
view.modal.hooks.beforeDismiss.tapPromise(async (modalData) => {
modalData.data = [{ is_published: !!view.is_published }];
});