-1

I have two views. View1 and View2. I am using View1 to get the parameters from the input field, and display data in View2 based on those parameters. Now I cam confused on how to pass data among the views. Can anybody please give me insights towards it, and with working code, and what each line does?

Thanks

Sandra Rossi
  • 11,934
  • 5
  • 22
  • 48
Dibya Ranjan
  • 80
  • 3
  • 18

1 Answers1

1

For instance if you have the Input field in View1.view.xml as this:

 <Input id="id" value="Initial Value" editable="true" />

you can get the field value in the respective Controller say, View1_cont.controller.js and set the model, which can be retrieved in any other controller later.

var oData = {
    input: this.getView().byId("id").getValue()
};
var oModel = new sap.ui.model.json.JSONModel(oData);
sap.ui.getCore().setModel(oModel,"modelName");

Now in the second view's controller, say View2_cont.controller.js, the values can be retrieved and a view element with id "text" is set as shown below:

var oData = sap.ui.getCore().getModel("modelName").getData();
this.getView().byId("text").setValue(oData.input);

The text element in View2.view.xml is:

 <Text id="text"/>
  • What if I have multiple input fields? What changes do I make to var oData, to add them as well? Sorry I am bit weak in JS. And many many thanks for the answer. – Dibya Ranjan Jan 25 '17 at 10:11
  • var oData = { input1: this.getView().byId("id1").getValue(), input2: this.getView().byId("id2").getValue(), input3: this.getView().byId("id3").getValue() }; and so on! Hope this helps. – Harini Gunabalan Jan 25 '17 at 10:13
  • Yeah, had guessed, sorry for being lazy! Thank you very much. :) – Dibya Ranjan Jan 25 '17 at 10:17
  • Also in case I am using a select instead of just Text, I would like to retrieve the selected element. How can I get it's value? – Dibya Ranjan Jan 25 '17 at 10:24
  • this.getView(...).byId(...).getValue is not a Function – Dibya Ranjan Jan 25 '17 at 10:46
  • What UI control are you using? For example, if it is a Label then this would not work as Label does not have the Value property. Refer: http://stackoverflow.com/questions/39128056/sapui5-this-getview-byid-setvalue-is-not-a-function You should be using getText/setText instead. Please refer to the UI explored guide for further details on UI elements: https://sapui5.hana.ondemand.com/explored.html#/ – Harini Gunabalan Jan 25 '17 at 12:43