英文:
Add data to a JSON model in SAPUI5
问题
我目前正在学习SAPUI5,并希望创建一个简单的表单作为练习。不幸的是,我无法在我的JSON模型中创建一个新条目。当我尝试执行我的代码时,我没有收到错误消息,并且条目未复制到JSON文件中。
我尝试了以下内容:
EmployeeForm.view.xml
<mvc:View xmlns:core="sap.ui.core"
controllerName="sap.ui.test.controller.EmployeeForm"
xmlns="sap.m"
xmlns:form="sap.ui.layout.form"
xmlns:mvc="sap.ui.core.mvc">
<Page
showNavButton="true"
navButtonPress=".onNavBack">
<Panel headerText="{i18n>employeeFormHeaderText}">
<form:SimpleForm editable="true" layout="ColumnLayout">
<Label text="First Name"/>
<Input value="{/first_name}" width="200px" />
<Label text="Last Name"/>
<Input value="{/last_name}" width="200px" />
<Label text="Email"/>
<Input value="{/email}" width="200px"/>
<Label text="Gender"/>
<Select width="200px">
<items>
<core:Item text="Male" key="male"></core:Item>
<core:Item text="Female" key="female"></core:Item>
<core:Item text="Other" key="other"></core:Item>
</items>
</Select>
<Label text="IP Address"/>
<Input value="{/ip_address}" width="200px"/>
</form:SimpleForm>
<VBox width="100%" alignItems="Center">
<Button
icon="sap-icon://save"
text="{i18n>employeeFormButtonText}"
press=".onSaveEmployee"></Button>
</VBox>
</Panel>
</Page>
</mvc:View>
EmployeeForm.controller.js
sap.ui.define([
"sap/ui/core/mvc/Controller",
"sap/ui/core/routing/History",
"sap/ui/model/json/JSONModel"
], function (Controller, History, JSONModel) {
return Controller.extend("sap.ui.test.controller.EmployeeForm", {
onInit: function () {
var model = this.getOwnerComponent().getModel("employee");
var modelLength = model.oData.Employees.length + 1;
var oModel = new JSONModel({
id: modelLength,
first_name: "",
last_name: "",
email: "",
gender: "",
ip_address: "",
status: "work"
});
this.getView().setModel(oModel);
},
onSaveEmployee: function () {
var newItem = this.getView().getModel().oData;
var oModel = this.getOwnerComponent().getModel("employee");
oModel.setProperty("/Employees", oModel.getProperty("/Employees").concat(newItem));
}
});
});
manifest.json
...
"models": {
"employee": {
"type": "sap.ui.model.json.JSONModel",
"uri": "Employees.json"
}
},
...
英文:
I am currently learning SAPUI5 and wanted to create a simple form as an exercise. Unfortunately I can't manage to create a new entry in my JSON model. When I try to execute my code, I don't get an error message and the entry is not copied to the JSON file.
I have tried the following:
EmployeeForm.view.xml
<mvc:View xmlns:core="sap.ui.core"
controllerName="sap.ui.test.controller.EmployeeForm"
xmlns="sap.m"
xmlns:form="sap.ui.layout.form"
xmlns:mvc="sap.ui.core.mvc">
<Page
showNavButton="true"
navButtonPress=".onNavBack">
<Panel headerText="{i18n>employeeFormHeaderText}">
<form:SimpleForm editable="true" layout="ColumnLayout">
<Label text="First Name"/>
<Input value="{/first_name}" width="200px" />
<Label text="Last Name"/>
<Input value="{/last_name}" width="200px" />
<Label text="Email"/>
<Input value="{/email}" width="200px"/>
<Label text="Gender"/>
<Select width="200px">
<items>
<core:Item text="Male" key="male"></core:Item>
<core:Item text="Female" key="female"></core:Item>
<core:Item text="Other" key="other"></core:Item>
</items>
</Select>
<Label text="IP Address"/>
<Input value="{/ip_address}" width="200px"/>
</form:SimpleForm>
<VBox width="100%" alignItems="Center">
<Button
icon="sap-icon://save"
text="{i18n>employeeFormButtonText}"
press=".onSaveEmployee"></Button>
</VBox>
</Panel>
</Page>
</mvc:View>
EmployeeForm.controller.js
sap.ui.define([
"sap/ui/core/mvc/Controller",
"sap/ui/core/routing/History",
"sap/ui/model/json/JSONModel"
], function (Controller, History, JSONModel) {
"use strict";
return Controller.extend("sap.ui.test.controller.EmployeeForm", {
onInit: function () {
var model = this.getOwnerComponent().getModel("employee");
var modelLength = model.oData.Employees.length + 1;
var oModel = new JSONModel({
id: modelLength,
first_name: "",
last_name: "",
email: "",
gender: "",
ip_address: "",
status: "work"
});
this.getView().setModel(oModel);
},
onSaveEmployee: function () {
// get item
var newItem = this.getView().getModel().oData;
// get model
var oModel = this.getOwnerComponent().getModel("employee");
// add item to model
oModel.setProperty("/Employees", oModel.getProperty("/Employees").concat(newItem));
}
});
});
manifest.json
...
"models": {
"employee": {
"type": "sap.ui.model.json.JSONModel",
"uri": "Employees.json"
}
},
...
答案1
得分: 0
JSONModel只使用JSON文件中的数据作为初始数据,它不会在初始加载后写回任何更改。
在应用程序使用过程中进行的所有更改(如果是双向绑定)应该在JSONModel对象上可见,但在刷新时将丢失,因为不会直接在JSON文件上进行写入以使这些更改永久保存。
英文:
The JSONModel only uses the data from the JSON file as initial data, it will never write back any changes made after the initial load.
All changes done (if in two way binding) during the use of the application should be visible on the JSONModel object but they will be lost when refreshing since no writing directly on the JSON file will be done to make those changes permanent.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论