简体   繁体   English

如何在SAPUI5中将控件重置为初始值?

[英]How to reset controls to their initial values in SAPUI5?

I have a form with a lot of controls ( ComboBoxes , TextAreas , RadioButtons ) using OpenUI5. 我有一个使用OpenUI5的很多控件( ComboBoxesTextAreasRadioButtons )的表单。 I give the values of the controls on the server side in C++. 我在C ++中给出了服务器端控件的值。 What I want is to have a reset button, which will clear the choices of the user and revert the controls back with the default choices. 我想要的是有一个重置按钮,它将清除用户的选择并使用默认选项恢复控件。 Until now, I am able to have the form and the controls as an JS object like this: 到目前为止,我能够将表单和控件作为JS对象,如下所示:

this.byId("MainForm").getModel();

The only think I am able to do until now is to totally clear all the controls like this: 我能够做到的唯一想法是完全清除所有这样的控件:

this.byId("MainForm").getModel().setData(null);

For example I have a ComboBox and the default value from my model is the second choice. 例如,我有一个ComboBox ,我的模型的默认值是第二个选择。 How can I keep this value and set it back to the control? 如何保留此值并将其设置回控件?

Use the API of the respective control. 使用相应控件的API sap.m.ComboBox features the method setSelectedKey(sKey) . sap.m.ComboBox的方法是setSelectedKey(sKey) sap.m.TextArea features the method setValue(sValue) . sap.m.TextArea的方法是setValue(sValue) sap.m.TextArea setValue(sValue) sap.m.RadioButton features the method setSelected(bSelected) . sap.m.RadioButton的方法是setSelected(bSelected)

Bind a press event to your reset button. press事件绑定到重置按钮。 in your press event get the form's controls. 在您的新闻事件中获取表单的控件。 then reset the form's controls to their initial states with their respective methods. 然后使用各自的方法将表单的控件重置为其初始状态。

<!-- in your e.g. xml view -->
<Button type="Reject" text="Reset" press="onPressResetButton"></Button>

// in your js controller
onPressResetButton: function() {
  ...
  var oComboBox = this.byId("your-combo-box-id");
  oComboBox.setSelectedKey("your-initial-item-key");
  ...
}

Get the initial values for the form's controls from your backend or keep them in a local model in the frontend. 从后端获取表单控件的初始值,或将它们保存在前端的本地模型中。

I think the best way to achieve this is to have two models. 我认为实现这一目标的最佳方法是拥有两个模型。 One is your odata model which fetches data from server and second is to have local json model. 一个是从服务器获取数据的odata模型,第二个是具有本地json模型。

  1. Fetch data from odata and assign it to local model. 从odata获取数据并将其分配给本地模型。
  2. Bind the local model to the Form 将本地模型绑定到表单
  3. Doesn't matter user changes data on form. 用户在表单上更改数据无关紧要。 Local model will be affected. 本地模型将受到影响。
  4. When user presses reset button. 当用户按下重置按钮时。 Assign your odata model data to local model again that way the default values will be binded again. 再次将odata模型数据分配给本地模型,以便再次绑定默认值。

Hope this approach helps. 希望这种方法有所帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM