UI5 OData:创建Appointment后自动READ调用如何添加过滤?
如何为Planning Calendar新增后的自动READ调用添加过滤条件
可以给新增Appointment后自动触发的READ请求添加过滤条件,以下是几种可行的实现方案:
1. 手动控制创建成功后的刷新逻辑
放弃依赖控件默认的自动刷新,在Appointment创建成功的回调中,主动调用带过滤条件的READ接口,并更新日历数据源。这种方式灵活性最高,能完全复用初始加载时的过滤规则。
示例代码(以SAP UI5为例):
// 假设oModel是你的OData模型,oPlanningCalendar是Planning Calendar控件实例 oModel.create("/AppointmentSet", oNewAppointmentData, { success: function() { // 复用初始加载的过滤条件 var oFilterGroup = new sap.ui.model.Filter({ filters: [ new sap.ui.model.Filter("orgId", sap.ui.model.FilterOperator.Contains, "99999999"), new sap.ui.model.Filter("startDate", sap.ui.model.FilterOperator.LE, new Date("2022-12-01T10:59:14")), new sap.ui.model.Filter("endDate", sap.ui.model.FilterOperator.GE, new Date("2022-12-31T11:59:14")) ], and: true }); // 手动读取带过滤的关联数据 oModel.read("/EmployeeSet('00002889')/EmployeeToAppointment", { filters: [oFilterGroup], top: 1000, success: function(oResponse) { // 更新日历的 appointments 聚合绑定 oPlanningCalendar.bindAggregation("appointments", { path: "/EmployeeSet('00002889')/EmployeeToAppointment", filters: [oFilterGroup], parameters: { $top: 1000 } }); } }); }, error: function(oError) { // 处理创建失败逻辑 } });
2. 配置OData模型的默认过滤规则
给OData模型设置针对EmployeeToAppointment导航属性的默认过滤,这样所有针对该导航的READ请求都会自动带上过滤条件,包括控件触发的自动刷新。
示例代码:
var oODataModel = new sap.ui.model.odata.v2.ODataModel("/your/odata/service/url", { defaultBindingMode: "TwoWay" }); // 定义默认过滤条件 var oDefaultFilter = new sap.ui.model.Filter({ filters: [ new sap.ui.model.Filter("orgId", sap.ui.model.FilterOperator.Contains, "99999999"), new sap.ui.model.Filter("startDate", sap.ui.model.FilterOperator.LE, new Date("2022-12-01T10:59:14")), new sap.ui.model.Filter("endDate", sap.ui.model.FilterOperator.GE, new Date("2022-12-31T11:59:14")) ], and: true }); // 为EmployeeToAppointment导航属性设置默认过滤 oODataModel.setDefaultFilters({ "/EmployeeToAppointment": [oDefaultFilter] }); // 将模型绑定到应用或控件 sap.ui.getCore().setModel(oODataModel);
3. 重写控件的默认刷新行为
如果使用框架自带的Planning Calendar控件(如SAP UI5的sap.m.PlanningCalendar),可以监听其刷新前的事件,在请求发送前注入过滤参数。
示例代码:
oPlanningCalendar.attachBeforeRebindAppointments(function(oEvent) { var oBindingParams = oEvent.getParameter("bindingParams"); // 添加过滤条件到绑定参数 var oFilter = new sap.ui.model.Filter({ filters: [ new sap.ui.model.Filter("orgId", sap.ui.model.FilterOperator.Contains, "99999999"), new sap.ui.model.Filter("startDate", sap.ui.model.FilterOperator.LE, new Date("2022-12-01T10:59:14")), new sap.ui.model.Filter("endDate", sap.ui.model.FilterOperator.GE, new Date("2022-12-31T11:59:14")) ], and: true }); oBindingParams.filters.push(oFilter); });
内容的提问来源于stack exchange,提问作者PeDeGa
相关产品推荐
相关产品推荐

