Reputation: 493
I have a input field (sap.m.Input) for which I need the type as Float. I tried using the sap.ui.model.type.Float() but it did not work.
How can I use a custom type for my input field. I don't have any binding, just need to set the type of the input field as float. An example would be really helpful.
Thanks in Advance, Deepak
Upvotes: 1
Views: 8857
Reputation: 626
I have the same problem, usage of new sap.ui.model.type.Float
seems to convert the string from OData service (Edm.Decimal) into a real float number. This will work for showing the number correct, but not if you try to write a changed value back (OData two-way-binding).
Therefore I implemented an own type like this:
jQuery.sap.declare("my.package.MyFloat");
sap.ui.model.SimpleType.extend("my.package.MyFloat", {
formatValue : function(oValue) {
return oValue;
},
parseValue : function(oValue) {
return oValue;
},
validateValue : function(oValue) {
if (oValue != null && oValue != "" && oValue != undefined) {
if (isNaN(Number(oValue))) {
var messageString = sap.ui.getCore().getModel("i18n").getResourceBundle().getText("KEY_TO_ERROR_MESSAGE");
throw new sap.ui.model.ValidateException(messageString);
}
}
}
})
I detected one SAP example here: http://help.sap.com/saphelp_nw74/helpdata/de/91/f0652b6f4d1014b6dd926db0e91070/content.htm search for PLZ in the file.
Currently I am searching for a way to add some parameters during the construction of MyFloat
.
You can use such a type this way:
new sap.m.Input({
value: {
path : "BindingPathToAttribute",
type : new my.package.MyFloat({})
}
}),
Upvotes: 1
Reputation: 19
You can create custom type like this way :
<script type="text/javascript">
(function(){
"use strict";
jQuery.sap.declare('Float');
jQuery.sap.require('sap.ui.base.DataType');
Float = sap.ui.base.DataType.createType( "Float",
{ isValid :
function(sValue) {
return ((sValue % 1) != 0);
}
}, sap.ui.base.DataType.getType('number')
);
})();
</script>
Now you can use type as Float
Upvotes: 2
Reputation: 432
var oInp = new sap.m.Input({
liveChange : function(oEvent){
debugger;
var value = parseFloat(oEvent.getSource().getProperty('value'));
if(value % 1 === 0 || isNaN(value))
valueState = "Error";
else
valueState = "Success";
oEvent.getSource().setValueState(valueState);
}
});
oInp.setType(sap.m.InputType.Number);
Upvotes: 1