Skip to Content
0

SAPUI5 : Currency formatting JS View List

Apr 02 at 12:13 PM

98

avatar image
Former Member

Hi,

I am using a list within my Java Script view. I wish to prefix the currency symbol to the number(Price). I have come across examples in XML but I could not find any example with JavaScript.

I tried using the following, but does not work, and gives me an Syntax error in the Web IDE.

new sap.m.List("List", {
	items: {
		path: "ES5>/ProductCollection",
		template: new sap.m.ObjectListItem("ProdList",
			{
				title:"{ES5>ProductName}",
				intro:"{ES5>ProductCategory}",
				number:"{parts:[{path:'ES5>UnitPrice'}, 
						{path:'ES5>CurrencyCode'}],
					type:'sap.ui.model.type.Currency',
					formatOptions:{showMeasure:false}
					}",
				numberUnit: "{ES5>CurrencyCode}",
				type: sap.m.ListType.Active,
				press: [oController.onSelProd, oController]
			})
		}
	});

Here ES5 is the model referring to the GWDEMO oData service.

Please help me in identifying what is missing or what I am doing wrong here.

10 |10000 characters needed characters left characters exceeded
* Please Login or Register to Answer, Follow or Comment.

2 Answers

Best Answer
Jamie Cawley
Apr 02 at 03:12 PM
0

Check the example at

https://sapui5.hana.ondemand.com/#/sample/sap.ui.core.sample.TypeCurrency/preview

I would imagine you may be getting an error on trying to set your CurrencyCode, which I would assume is a string, to the type Currency, which is expecting a number. In the future, it would be helpful to include any errors you are seeing.

Regards,

Jamie

Show 3 Share
10 |10000 characters needed characters left characters exceeded
Former Member

Hi Jamie,

The syntax error that I get in the Web IDE is :

error: Syntax Error: ESLint(syntax-parse) : unexpected token ILLEGAL

However I was able to get rid of this by having the entire code for 'number' in a single line as below:

number: "{ parts: [{path: 'ES5>UnitPrice'}, {path: 'ES5>CurrencyCode'}], type: 'sap.ui.model.type.Currency', formatOptions: { showMeasure: false } }",

I do not get any error in the browser console.

The field 'UnitPrice' is of type Decimal, and field 'CurrencyCode' is of type String and contains value as "USD" or "EUR".

I checked the sample in SDK url provided by you. The CurrencyCode is not being used with number and probably is defaulted to $.

Please help.

Regards,

Chetan Mishra

0

Is it working in a browser for you? Are you trying to show the code and not the string? You could do show using the currencyCode property.

formatOptions: {showMeasure: true, currencyCode: false}

Regards,

Jamie

0
Former Member

Hi Jamie,

I apologize for a delayed response, as I was caught up with my Project deliverables.

Thanks for replying and the solution provided by you exactly answers my requirement.

Regards,

Chetan Mishra

0
Nabi Zamani Apr 03 at 02:41 PM
0

This is a copy&paste solution for you (or run it live on jsbin, which uses dependency injection as well):

var oList = new sap.m.List("List", {
    items : {
        path: "ES5>/ProductCollection",
        template: new sap.m.ObjectListItem("ProdList", {
            title:"{ES5>ProductName}",
            intro:"{ES5>ProductCategory}",
            number: {
                parts:[ {path:"ES5>UnitPrice"}, {path: "ES5>CurrencyCode"} ],
                type: new sap.ui.model.type.Currency({
                    showMeasure: false
                }),
            },
            numberUnit: "{ES5>CurrencyCode}",
            type: sap.m.ListType.Active,
            press: [oController.onSelProd, oController]
        })
    }
});
//...
Show 1 Share
10 |10000 characters needed characters left characters exceeded
Former Member

Hi Nabi,

Thanks for replying, the following piece of code suggested by Jamie works for my requirements:

number:"{parts:[{path:'ES5>UnitPrice'}, 
						{path:'ES5>CurrencyCode'}],
					type:'sap.ui.model.type.Currency',
					formatOptions:{showMeasure: true, currencyCode: false}
					}",
0