cancel
Showing results for 
Search instead for 
Did you mean: 

How to get the value of the input of a view file from Component.js

kaiumeki
Explorer
0 Kudos

Hello,

I would like to get the values of the inputs of the view file from Component.js.

I was able to get the view file inputs from Controller.js as shown in the image below, but I don't know how to get them from Component.js.

View file:

Controller.js:

I would appreciate it if you could tell me how I can link Component.js to a View file and retrieve the data.

Sincerely,

Kai Umeki

kaiumeki
Explorer
0 Kudos

aaaaaaaaaaaaa

Accepted Solutions (1)

Accepted Solutions (1)

maheshpalavalli
Active Contributor

It would be easier for you to use binding here, try using JSON model binding then bind it's property to the input field. Later you can access that binding data in any other view or in component easily.

https://ui5.sap.com/#/topic/d70e9894c09b4c27a98d4850d4e90f2c

https://ui5.sap.com/#/topic/91f0652b6f4d1014b6dd926db0e91070

Then you can use this.getModel("testmodel").getData(); // you will have your model data here.

maheshpalavalli
Active Contributor
kaiumeki do this.in the component Init method, create the model and set it to the component.
var oModel = new sap.ui.model.json.JSONModel();
oModel.setData({"field1":"","field2":""})
this.setModel(oModel,"testModel")
Now, in the view, use that
<Text text="{/field1}" />
Now in the component, access it
this.getModel("testModel").getData(); // {field1:"",field2:""}
maheshpalavalli
Active Contributor
kaiumeki, I would recommend you to get the basics done before starting else you might face many hurdles and might create apps without following the stanards..
kaiumeki
Explorer
0 Kudos

Dear Mahesh Kumar Palavalli,

I sincerely appreciate your reply.

I don't know why but I couldn't reply to your comment,I posted an answer instead of a reply to your comment, so I hope you will check that out.

Thank you very much.

Kai Umeki

Answers (3)

Answers (3)

lk24
Participant
0 Kudos

hi ,

var data=this.getModel("modelname").getData() will return array of data so to access a particular property value you need to specify the

// var propertyValue=data[index].propertyName;

or

alert(data[index].propertyName)

kaiumeki
Explorer
0 Kudos

Dear lokesh kumar,

Many thanks for your advice.

I did not understand about data types.

Thank you very much for telling me that it is an array type.

I really appreciate it.

Regards,

Kai Umeki

junwu
Active Contributor
0 Kudos

put a debug point at line of alert(***), check data1 in dev tool , you will get better idea.

kaiumeki
Explorer
0 Kudos

Dear Jun Wu,

Thank you for your advise.

I had never used the debug feature before, but it was very useful!

I really appreciate it.

Regards,

Kai Umeki

kaiumeki
Explorer
0 Kudos

Dear Mahesh Kumar Palavalli,

Thank you very much for your replying and advises.

I wrote the code as you taught me, as follows.

Component.js's define section is shown in the figure below.

And the model creation, assignment, and data acquisition parts of component.js are shown below.

I tried to check the contents of the variables retrieved using the alert.

And here is the text part to get.

And here is the result of the execution. I can confirm that I was only able to get the value as an object and not the actual value.

I do not understand why this is happening. If you could please advise me on this, I would appreciate it.

Thank you very much for your advice again and again.

I really appreciate it.

Regards,

Kai Umeki

maheshpalavalli
Active Contributor

you need to know how to access an object in javascript. For this reason I recommended you to do the basics first which will help u to solve this simple issues.

for eg. "{"test":1} -> this is an object/map data in javascript, google about it and learn how to access it.. To access the data, u need to use data1.field1

kaiumeki
Explorer
0 Kudos

Dear Mahesh Kumar Palavalli,

Thank you very much for your advice.

At last I have achieved what I want to do.

I really appreciate it.

Regards,

Kai Umeki

maheshpalavalli
Active Contributor

great to hear that kaiumeki.

I still suggest you to brush up your skills in javascript and HTML(a bit), you will love it for sure.

All the best

Mahesh