There are no secrets to success. It is the result of preparation, hard work, and learning from failure.

Sunday, 29 April 2018

Custom Currency Formatter

Click here and follow my SAP UI5/FIORI snippets and information Page
I have made custom currency formatter example,below I have shown Application Structure, View Part, Controller Part, Formatter, JSON and Result.Go through it and please let me know in case of any issue/doubt.

Application Structure


View Part

<Page title="{i18n>title}">
<content>
<f:SimpleForm editable="true" layout="ResponsiveGridLayout" labelSpanXL="3" labelSpanL="3" labelSpanM="3" labelSpanS="12"
adjustLabelSpan="false" emptySpanXL="4" emptySpanL="4" emptySpanM="4" emptySpanS="0" columnsXL="1" columnsL="1" columnsM="1"
singleContainerFullSize="false">
<f:content>
<Label text="Amount"/>
<Input value="{parts:[{path:'amo'}],formatter:'currency1.util.formatter.currencyMethod'}"  id="inId"></Input>
</f:content>
</f:SimpleForm>
</content>
</Page>

Controller Part

onInit:function(){
// Loading formatter at the time of application initialization....
jQuery.sap.require("currency1.util.formatter");
// Loading json file....
var json = new sap.ui.model.json.JSONModel("model/record.json");
// Binding form with json value....
this.getView().bindElement("/0");
// Setting json to current view....
this.getView().setModel(json);
}

formatter.js

// Declaring formatter file....
jQuery.sap.declare("currency1.util.formatter");
currency1.util.formatter = {
currencyMethod: function(val) {
var amount = parseFloat(val);
var finalResult = amount.toLocaleString('en-US', {
style: 'currency',
currency: 'USD'
});
return finalResult.replace("$", '');
}
}

record.json

[{
"amo":"2344000"
}]

Result



No comments:

Post a Comment