I am trying to update data in my json model with the help of input provided in my view but i am unable to do so, i want to know is it even possible to do so and if yes kindly provide me with the solution
I am trying to load my json model in two different methods in a view but the model is getting loaded in one place and in other place its showing me undefined and data is not present in it when i debug it
var oModel in onSubmit() is undefined and also push has some problem
I want to perform an update operation on my json data and it should get updated in the json model when i press the save button
this is my controller
sap.ui.define([
"sap/ui/core/mvc/Controller"
], function(Controller) {
"use strict";
return Controller.extend("workeLibrary.controller.Details", {
onInit: function() {
// var oModel = new sap.ui.model.json.JSONModel(jQuery.sap.getModulePath("workeLibrary.model", "/books.json"));
// this.getView().setModel(oModel);
var oModel = new sap.ui.model.json.JSONModel("model/books.json");
this.getView().setModel(oModel, "oModelBookdata");
// var oModel = new sap.ui.model.json.JSONModel("model/books.json");
this.getOwnerComponent().getRouter().getRoute("Details").attachPatternMatched(this._onRouteMatched, this);
},
onAfterRendering: function() {
},
_onRouteMatched: function(oEvent) {
var oArgs, oView;
oArgs = oEvent.getParameter("arguments").productId;
oView = this.getView();
var xyz = this.getView().getModel();
oView.bindElement({
path: "/Books/" + oArgs,
model: "oModelBookdata"
});
oView.updateBindings();
},
onSubmit: function() {
var Subm = this.getView().byId("Subm").getText();
var oModel = new sap.ui.model.json.JSONModel("model/books.json");
this.getView().setModel(oModel, "oModeldata");
var eBooks = oModel.getProperty("/Books");
var eName = this.getView().byId("name").getValue();
var eAuth = this.getView().byId("auth").getValue();
var eSeri = this.getView().byId("seri").getValue();
var eSequ = this.getView().byId("sequ").getValue();
var eGenr = this.getView().byId("genr").getValue();
var eInst = this.getView().byId("instk").getValue();
var eCat = this.getView().byId("cat").getValue();
var ePric = this.getView().byId("pric").getValue();
var ePage = this.getView().byId("page").getValue();
if (Subm === "Save") {
var objBook = {
"cat": "",
"name": "",
"author": "",
"series_t": "",
"sequence_i": "",
"genre_s": "",
"inStock": "",
"price": "",
"pages_i": ""
};
objBook.at = eCat;
objBook.name = eName;
objBook.author = eAuth;
objBook.series_t = eSeri;
objBook.sequence_i = eSequ;
objBook.genre_s = eGenr;
objBook.inStock = eInst;
objBook.price = ePric;
objBook.pages_i = ePage;
eBooks.push(objBook);
oModel.setProperty("/Books", eBooks);
} else if (Subm === "Edit") {
this.getView().byId("name").setEditable(true);
this.getView().byId("cat").setEditable(true);
this.getView().byId("auth").setEditable(true);
this.getView().byId("seri").setEditable(true);
this.getView().byId("genr").setEditable(true);
this.getView().byId("sequ").setEditable(true);
this.getView().byId("instk").setEditable(true);
this.getView().byId("pric").setEditable(true);
this.getView().byId("page").setEditable(true);
this.getView().byId("Subm").setText("Save");
}
},
// _on]RouteMatched: function(oEvent) {
// var productId = oEvent.getParameter("arguments").productId;
// this.getView().bindElement("/products/"+productId);
// },
handleNavButtonPress: function() {
this.getOwnerComponent().getRouter().navTo("List");
}
});
});
and this is my view
<mvc:View controllerName="workeLibrary.controller.Details" xmlns:html="http://www.w3.org/1999/xhtml" xmlns:f="sap.ui.layout.form"
xmlns:mvc="sap.ui.core.mvc" displayBlock="true" xmlns="sap.m">
<App>
<pages>
<Page showNavButton="true" navButtonPress="handleNavButtonPress">
<content>
<f:SimpleForm title="Registration Form" editable="true" layout="ResponsiveGridLayout" labelSpanL="3" labelSpanM="3" emptySpanL="4"
emptySpanM="4">
<f:content>
<Label text="Book ID"/>
<Input id="id" value="{oModelBookdata>id}" editable="false"/>
<!--<Text text="{oModelBookdata>id}"/>-->
<Label text="Book Name"/>
<!--<Text text="{oModelBookdata>name}"/>-->
<Input id="name" value="{oModelBookdata>name}" editable="false"/>
<Label text="Book Series"/>
<!--<Text text="{oModelBookdata>name}"/>-->
<Input id="seri" value="{oModelBookdata>series_t}" editable="false"/>
<Label text="Book Genre"/>
<!--<Text text="{oModelBookdata>name}"/>-->
<Input id="genr" value="{oModelBookdata>genre_s}" editable="false"/>
<Label text="Book Category"/>
<!--<Text text="{oModelBookdata>cat}"/>-->
<Input id="cat" value="{oModelBookdata>cat}" editable="false"/>
<Label text="Book Author"/>
<!--<Text text="{oModelBookdata>author}"/>-->
<Input id="auth" value="{oModelBookdata>author}" editable="false"/>
<Label text="Book Sequence"/>
<!--<Text text="{oModelBookdata>sequence_i}"/>-->
<Input id="sequ" value="{oModelBookdata>sequence_i}" editable="false"/>
<Label text="In Stock"/>
<!--<Text text="{oModelBookdata>inStock}"/>-->
<Input id="instk" value="{oModelBookdata>inStock}" editable="false"/>
<Label text="Book Price"/>
<!--<Text text="{oModelBookdata>price}"/>-->
<Input id="pric" value="{oModelBookdata>price}" editable="false"/>
<Label text="Pages"/>
<!--<Text text="{oModelBookdata>pages_i}"/>-->
<Input id="page" value="{oModelBookdata>pages_i}" editable="false"/>
</f:content>
</f:SimpleForm>
</content>
<footer>
<Bar>
<contentRight>
<Button id="Subm" text="Edit" type="Emphasized" press="onSubmit"/>
</contentRight>
</Bar>
</footer>
</Page>
</pages>
</App>
</mvc:View>
Add comment