cancel
Showing results for 
Search instead for 
Did you mean: 

Binding Input of type Float to JSONModel always returns a string instead of a number

0 Kudos

Hello Everyone,

I want to simply bind a number value to an input field. However, I just cannot get this to work properly.

I have an input field which is bound to a JSONModel, where the value behind is a js number.

<Input value="{path: '/myValue', type : 'sap.ap.ui.model.type.Float' }" type="Number"/>

However, everytime I edit the value of the input field, the JSON Model no longer holds a number, but a string like "1.2" instead of the number 1.2

How can I directly bind the number value to the input field, without having to manually convert afterwards?

Thanks in advance and best regards,

Chris

Accepted Solutions (0)

Answers (1)

Answers (1)

gururajenamate
Explorer
0 Kudos

Hi,

Use StepInput control for number input.

https://sapui5.hana.ondemand.com/#/entity/sap.m.StepInput

Regards,

Gururaj

0 Kudos

Hello Gururaj,

Thank you for your quick response! I was considering this one. However, I do not have a particular step for my input, and it would most like confuse my users as all they want to do is enter a floating point number.

Also, the documentation (StepInput) says I should not use it if I don't have a particular step:

When not to use:

  • To display a value that rarely needs to be adjusted and does not pertain to a particular step. In this case, use the regular sap.m.Input instead.

Thanks and best regards,

Chris

0 Kudos

Another thing is that It acts differently on different browsers. I have IE11 and Chrome, both in German language, but only Chrome displays the correct decimal seperator, IE11 shows a dot.

Any Idea how I can get around that?

BR

Chris

gururajenamate
Explorer
0 Kudos

Hi Chris,

>> You can use Input with Double type https://sapui5.hana.ondemand.com/#/api/sap.ui.model.odata.type.Double

<Input value="{path: 'Items>UnitSize', type: 'sap.ui.model.odata.type.Double'}"/>

IE11 shows a dot: Must be rendering issue.

Regards,

Gururaj

0 Kudos

Hello Gururaj,

the reason your sample works is because you are not setting the type property of the input to "Number". It has unfortunately nothing to do with float or double.

However, if you do not use the type property, the user can enter other characters than numbers as well. And on mobile devices he won't have the numbers keyboard but the normal keyboard.

Do you have any other solutions?

Edit: I prepared an example:

https://jsfiddle.net/93mx0yvt/537/

Regards,

Chris

gururajenamate
Explorer
0 Kudos

We can use liveChange event to handle this.

But "on mobile devices he won't have the numbers keyboard but the normal keyboard." not possible.

Updated https://jsfiddle.net/93mx0yvt/537/ please check.

Regards,

Gururaj

0 Kudos

Hello Gururaj,

I checked your update, however there are 2 problems with it:

1) Now the change event doesn't fire after adding liveChange event.

2) With the format functions, you set the decimal seperator automatically to a '.' dot. However, in other cultures (such as german for example) the decimal seperator is a comma ','. So this solution is not culture aware.

Regards,

Chris

gururajenamate
Explorer
0 Kudos

Hmm:(

Use

<Inputvalue="{path: '/myValue', type : 'sap.ap.ui.model.type.Float' }" type="Number"/>

and use change event to parseFloat the value.

Regards,

Gururaj

0 Kudos

Yes that's what I'm already doing right now. But I think this is not the way it should work, because I then need to either add a different change event to every input, or use parseFloat everytime I want to use one of my values. Both don't seem to be a very clean solution to me.

I think this is more likely to be a bug than an expected behaviour, because for the StepInput the binding works. Can you somehow forward it to the developers?

Regards,

Chris

gururajenamate
Explorer
0 Kudos

Chris, You can raise OSS ticket.

Regards,

Gururaj

0 Kudos

Hello Gururaj,


sorry for responding this late. How can I raise an OSS ticket?

Regards,

Chris