Javascript 如何在 SAPUI5 上检索字段值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38708179/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 21:44:48  来源:igfitidea点击:

How to retrieve a field value on SAPUI5

javascripthtmlmodelsapui5

提问by Mario

I'm starting on OPENUI5/SAPUI5 and I was trying to access the data for the controls i've implemented. For example:

我开始使用 OPENUI5/SAPUI5,我试图访问我已实现的控件的数据。例如:

<Label text="Amount" /> <Input id="inputAmount" value="{Amount}" /> <Text id="lblCurrency" text="USD" >

<Label text="Amount" /> <Input id="inputAmount" value="{Amount}" /> <Text id="lblCurrency" text="USD" >

  1. How to get the programatically values for inputAmount or lblCurrency?
  2. Initially i'm using a mockup-server, how to get access to the odata model programatically? I'm using:
  1. 如何以编程方式获取 inputAmount 或 lblCurrency 的值?
  2. 最初我使用的是模型服务器,如何以编程方式访问 odata 模型?我正在使用:

console.log( this.getView().getModel().getJSON() );

console.log( this.getView().getModel().getJSON() );

I can't get the structure, but i had deployed the data on the objects and actually is showed on screen.

我无法获得结构,但我已将数据部署在对象上并实际显示在屏幕上。

采纳答案by Ran Hassid

you can get the control by it's id and then access it's value so in your code:

您可以通过它的 id 获得控制权,然后在您的代码中访问它的值:

Accessing the Text control in the controller

访问控制器中的 Text 控件

sap.ui.getCore().byId("lblCurrency").getText() 

Accessing the Input value control in the controller

访问控制器中的输入值控件

sap.ui.getCore().byId("inputAmount").getValue()

The code above show how to access to value in the standard way. If you will use binding (binding is when you have a model which updates your view and if it's 2-way binding also your view updates you model) then in order to access to value you need to access access the model and extract it from there

上面的代码显示了如何以标准方式访问值。如果您将使用绑定(绑定是当您有一个更新视图的模型,如果它是 2 向绑定,您的视图也会更新您的模型)那么为了访问值,您需要访问模型并从那里提取它

example of binding and how to use it can be found here:

可以在此处找到绑定示例以及如何使用它:

https://help.sap.com/saphelp_uiaddon10/helpdata/en/91/f0f3cd6f4d1014b6dd926db0e91070/content.htm

https://help.sap.com/saphelp_uiaddon10/helpdata/en/91/f0f3cd6f4d1014b6dd926db0e91070/content.htm

Update

更新

in order to get the data from the model you need to use the getPropertyfunction under the model so your code should look some

为了从模型中获取数据,您需要使用模型下的getProperty函数,因此您的代码应该看起来有些

oModel.getProperty("{PATH_TO_WHERE_THE_PROPERTY_IS_LOCATED",{OPTIONAL_CONTEXT}};