当前位置: 首页 > Web前端 > JavaScript

在SAPBAS中使用SAPUI5应用程序消费OData创建和删除操作

时间:2023-03-27 00:05:47 JavaScript

在SAPBusinessApplicationStudio中创建一个SAPUI5应用程序,它应该具有以下项目结构:打开Home.view.xml,添加一个创建按钮:<按钮id="createButton"icon="sap-icon://add"tooltip="Create"visible="true"press="onOpenAddDialog">点击按钮后,我们期望弹出一个对话框,对话框的实现源码如下:打开和关闭对话框源码:onOpenAddDialog:function(){this.getView().byId("OpenDialog").open();},onCancelDialog:function(oEvent){oEvent.getSource().getParent().close();},对话框中调用的onCreate方法代码如下:onCreate:function(){varoSo=this.getView().byId("idSo").getValue();if(oSo!==""){constoList=this._oTable;constoBinding=oList.getBinding("items");constoContext=oBinding.create({"soNumber":this.byId("idSo").getValue(),"customerName":this.byId("idCustName").getValue(),"customerNumber":this.byId("idCustomer").getValue(),"PoNumber":this.byId("idPo").getValue(),"inquiryNumber":this.byId("idInqNumber").getValue()});oContext.created().then(()=>{//that._focusItem(oList,oContext);this.getView().byId("OpenDialog").close();});}else{MessageToast.show("所以不能为空");}},this._oTable的赋值发生在onInit钩子函数中:this._oTable=this.byId("table0");下面测试点击Create按钮,弹出对话框:维护好必填字段后,点击Create:可以看到创建成功的SalesOrder:下面会执行删除操作的实现。我们设计一个编辑按钮,只有进入编辑模式后,才允许点击删除按钮:Edit按钮的实现逻辑:onEditMode:功能(){这个。byId("editModeButton").setVisible(false);this.byId("saveButton").setVisible(true);this.byId("deleteButton").setVisible(true);this.rebindTable(this.oEditableTemplate,"Edit");}实现onDelete函数:onDelete:function(){varoSelected=this.byId("table0").getSelectedItem();if(oSelected){varoSalesOrder=oSelected.getBindingContext("mainModel").getObject().soNumber;oSelected.getBindingContext("mainModel").delete("$auto").then(function(){MessageToast.show(oSalesOrder+"成功删除");}.bind(this),function(oError){MessageToast.show("删除错误:",oError);});}else{MessageToast.show("PleaseSelectaRowtoDelete");}},ODataV4模型允许开发者指定是否将请求打包并作为批量请求(Batchrequest)发送,以及何时发送请求参数groupId指定了默认的batchgroup,默认为$auto。开发者可以通过参数updateGroupId来设置更新请求的batchgroup,如果不设置该参数,将使用groupId。如下代码实例化一个模型,该模型将批处理组“myAppUpdateGroup”中的所有更新请求捆绑在一起;然后可以使用oModel.submitBatch(“myAppUpdateGroup”)发送批处理请求。sap.ui.define(["sap/ui/model/odata/v4/ODataModel"],function(ODataModel){varoModel=newODataModel({serviceUrl:"/sap/opu/odata4/IWBEP/V4_SAMPLE/default/IWBEP/V4_GW_SAMPLE_BASIC/0001/",synchronizationMode:"None",updateGroupId:"myAppUpdateGroup"});});