向SAPUI5中的JSON模型添加数据

huangapple go评论66阅读模式
英文:

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

&lt;mvc:View xmlns:core=&quot;sap.ui.core&quot;
controllerName=&quot;sap.ui.test.controller.EmployeeForm&quot;
    xmlns=&quot;sap.m&quot;
    xmlns:form=&quot;sap.ui.layout.form&quot;
    xmlns:mvc=&quot;sap.ui.core.mvc&quot;&gt;
    &lt;Page
        showNavButton=&quot;true&quot;
        navButtonPress=&quot;.onNavBack&quot;&gt;
        &lt;Panel headerText=&quot;{i18n&gt;employeeFormHeaderText}&quot;&gt;
            &lt;form:SimpleForm editable=&quot;true&quot; layout=&quot;ColumnLayout&quot;&gt;
                &lt;Label text=&quot;First Name&quot;/&gt;
                &lt;Input value=&quot;{/first_name}&quot; width=&quot;200px&quot; /&gt;
                &lt;Label text=&quot;Last Name&quot;/&gt;
                &lt;Input value=&quot;{/last_name}&quot; width=&quot;200px&quot; /&gt;
                &lt;Label text=&quot;Email&quot;/&gt;
                &lt;Input value=&quot;{/email}&quot; width=&quot;200px&quot;/&gt;
                &lt;Label text=&quot;Gender&quot;/&gt;
                &lt;Select width=&quot;200px&quot;&gt;
                    &lt;items&gt;
                        &lt;core:Item text=&quot;Male&quot; key=&quot;male&quot;&gt;&lt;/core:Item&gt;
                        &lt;core:Item text=&quot;Female&quot; key=&quot;female&quot;&gt;&lt;/core:Item&gt;
                        &lt;core:Item text=&quot;Other&quot; key=&quot;other&quot;&gt;&lt;/core:Item&gt;
                    &lt;/items&gt;
                &lt;/Select&gt;
                &lt;Label text=&quot;IP Address&quot;/&gt;
                &lt;Input value=&quot;{/ip_address}&quot; width=&quot;200px&quot;/&gt;
            &lt;/form:SimpleForm&gt;
            &lt;VBox width=&quot;100%&quot; alignItems=&quot;Center&quot;&gt;
                &lt;Button 
                icon=&quot;sap-icon://save&quot;
                text=&quot;{i18n&gt;employeeFormButtonText}&quot;
                press=&quot;.onSaveEmployee&quot;&gt;&lt;/Button&gt;
            &lt;/VBox&gt;
        &lt;/Panel&gt;
    &lt;/Page&gt;
&lt;/mvc:View&gt;

EmployeeForm.controller.js

sap.ui.define([
    &quot;sap/ui/core/mvc/Controller&quot;,
    &quot;sap/ui/core/routing/History&quot;,
    &quot;sap/ui/model/json/JSONModel&quot;
], function (Controller, History, JSONModel) {
    &quot;use strict&quot;;

    return Controller.extend(&quot;sap.ui.test.controller.EmployeeForm&quot;, {
        onInit: function () {
            var model = this.getOwnerComponent().getModel(&quot;employee&quot;);
            var modelLength = model.oData.Employees.length + 1;
            var oModel = new JSONModel({
                id: modelLength,
                first_name: &quot;&quot;,
                last_name: &quot;&quot;,
                email: &quot;&quot;,
                gender: &quot;&quot;,
                ip_address: &quot;&quot;,
                status: &quot;work&quot;
            });
            this.getView().setModel(oModel);
        },
        onSaveEmployee: function () {
            // get item
            var newItem = this.getView().getModel().oData;
            // get model
            var oModel = this.getOwnerComponent().getModel(&quot;employee&quot;);
            // add item to model
            oModel.setProperty(&quot;/Employees&quot;, oModel.getProperty(&quot;/Employees&quot;).concat(newItem));
        }
    });
});

manifest.json

...
&quot;models&quot;: {
     &quot;employee&quot;: {
         &quot;type&quot;: &quot;sap.ui.model.json.JSONModel&quot;,
         &quot;uri&quot;: &quot;Employees.json&quot;
     }
},
...

答案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.

huangapple
  • 本文由 发表于 2023年5月14日 03:35:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/76244561.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定