The picture shows the destination details for our Exercise:
<mvc:View controllerName="ns.odataui.controller.View1" xmlns:mvc="sap.ui.core.mvc" displayBlock="true" xmlns="sap.m">
<Shell id="shell">
<App id="app">
<pages>
<Page id="page" title="{i18n>title}">
<content>
<List id="list" items="{/Categories}" mode="SingleSelect">
<items>
<StandardListItem title="{ID}" description="{Name}"></StandardListItem>
</items>
</List>
<VBox>
<Input placeholder="Enter the ID for the category" id="idinput"/>
<Input placeholder="Enter the category name" id="nameinput"/>
<Button text="Create" press="createData"/>
<Button text="Update" press="updateData"/>
<Button text="Delete" press="deleteData"/>
</VBox>
</content>
</Page>
</pages>
</App>
</Shell>
</mvc:View>
sap.ui.define([
"sap/ui/core/mvc/Controller",
"sap/m/MessageBox"
],
/**
* @param {typeof sap.ui.core.mvc.Controller} Controller
*/
function (Controller, MessageBox) {
"use strict";
return Controller.extend("ns.odataui.controller.View1", {
onInit: function () {
var that = this;
var odataModel = new sap.ui.model.odata.v2.ODataModel("NorthwindService/V2/(S(jjlmjbf1oszuuecc251trygy))/OData/OData.svc");
odataModel.read("/Products",{
success:function(oData,oResponse){
MessageBox.success("Success");
},
error: function(oError){
MessageBox.error("Error");
}
});
this.getView().setModel(odataModel);
},
updateData: function(){
var list = this.getView().byId("list");
var selItem = list.getSelectedItem();
var title = selItem.getTitle();
var description = selItem.getDescription();
var Name = this.getView().byId("nameinput").getValue();
var payload = {
ID: parseInt(title),
Name: Name
};
var path = "/Categories(" + title + ")";
var odataModel = this.getView().getModel();
// @ts-ignore
odataModel.update(path,payload,{
success: function(data,response){
MessageBox.success("Successfully Updated");
},
error: function(error){
MessageBox.error("Error while updating the data");
}
});
},
createData: function(){
var ID = this.getView().byId("idinput").getValue();
var Name = this.getView().byId("nameinput").getValue();
var data = {
ID: parseInt(ID),
Name: Name
};
var odataModel = this.getView().getModel();
odataModel.create("/Categories", data, {
success: function(data, response){
MessageBox.success("Data successfully created");
},
error: function(error){
MessageBox.error("Error while creating the data");
}
});
},
deleteData: function(){
var list = this.getView().byId("list");
var selItem = list.getSelectedItem();
var title = selItem.getTitle();
var path = "/Categories(" + title + ")"; ///Categories(3);
var odataModel = this.getView().getModel();
odataModel.remove(path,{
success: function(data,response){
MessageBox.success("Deleted data");
},
error: function(error){
MessageBox.error("Deletion failed");
}
})
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
12 | |
9 | |
7 | |
6 | |
6 | |
6 | |
6 | |
5 | |
3 | |
3 |