How to reset controls to their initial values in SAPUI5?

痞子三分冷 提交于 2021-02-11 07:56:32

问题


I have a form with a lot of controls (ComboBoxes, TextAreas, RadioButtons) using OpenUI5. I give the values of the controls on the server side in 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:

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. How can I keep this value and set it back to the control?


回答1:


Use the API of the respective control. sap.m.ComboBox features the method setSelectedKey(sKey). sap.m.TextArea features the method setValue(sValue). sap.m.RadioButton features the method setSelected(bSelected).

Bind a press event to your reset button. 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.




回答2:


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.

  1. Fetch data from odata and assign it to local model.
  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.

Hope this approach helps.



来源:https://stackoverflow.com/questions/52439991/how-to-reset-controls-to-their-initial-values-in-sapui5

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!