Thursday, June 4, 2009

How to display currency symbol using coldfusion 8, Cfgrid fomat HTML

Question - "how to display the currency $ in cfgrid while using cfgridcolumn, the format is HTML"

There are two solutions to the above question

1. Have the SQL format the data and return the column values with $ symbol added to it.

2. The below code I found on internet, but it is the only solution that I could found if you are using cfgrid and format is HTML, I am posting it so that it is available.

<--- Query --->
<cfscript>
paymentQuery = queryNew("fname,lname,amount", "varchar,varchar,double");
queryAddRow(paymentQuery);
querySetCell(paymentQuery, "fname", "John");
querySetCell(paymentQuery, "lname", "Smith");
querySetCell(paymentQuery, "amount", "103.846");
queryAddRow(paymentQuery);
querySetCell(paymentQuery, "fname", "Mary");
querySetCell(paymentQuery, "lname", "James");
querySetCell(paymentQuery, "amount", "200");
queryAddRow(paymentQuery);
querySetCell(paymentQuery, "fname", "Peter");
querySetCell(paymentQuery, "lname", "Stone");
querySetCell(paymentQuery, "amount", "3.25");
</cfscript>
<--- Cfform and HTML grid --->
<cfform>

<cfgrid name = "paymentGrid" format="html">
<cfgridcolumn name = "fname" header = "First name">
<cfgridcolumn name = "lname" header = "Last name">
<cfgridcolumn name = "amount" header = "amount" dataalign="right">
<cfloop query="paymentQuery">
<cfgridrow data ="#paymentQuery.fname#, #paymentQuery.lname#, #lscurrencyformat(paymentQuery.amount,'local','English (US)')#">
</cfloop>
</cfgrid>

</cfform>

Note -- the above will work with COLDFUSION 8, to work with CF MX 7 you need to change the
lscurrencyformat function(just remove the last two variables).

1 comment:

Prashant said...

hi Brijesh,
i have done similar thing .
please look at the following example:
http://tutorial35.learncf.com/

if you have any query please let me know.