Dale Fraser
Dale Fraser

Reputation: 4758

cfgridcolumn mask to format numbers

According to the docs, I should be able to use the mask attribute to format my column:

<cfgridcolumn name="salary" type="numeric" mask="$999,999">

I have a salary amount I want to show as

$100,000
 $80,000
  $5,000

Any ideas why its not working?

Full code snipit for testing below.

<cfscript>
        rs = QueryNew('salary', 'integer');
        QueryAddRow(rs,3);
        QuerySetCell(rs, 'salary', '100000', 1);
        QuerySetCell(rs, 'salary', '80000', 2);
        QuerySetCell(rs, 'salary', '5000', 3);
</cfscript>

<cfform>
    <cfgrid format="html" name="demo" query="rs">
        <cfgridcolumn name="salary" type="numeric" mask="$999,999">
    </cfgrid>
</cfform>

Upvotes: 1

Views: 1099

Answers (2)

Miguel-F
Miguel-F

Reputation: 13548

I need to give props to @Henry for this answer that I found here and applied to your case. I had not used this before but I tested it and it does work (using CF9). See this other reference that I found as well. Interesting stuff. Anyway...

For your issue try this code:

<html>
    <head><title>Test</title></head>
<body>
<cfsavecontent variable="formatGridInit">
<script language="javaScript">
formatgrid = function() {
    var myFormatter = Ext.util.Format.numberRenderer('$000,000');
    var mygrid = ColdFusion.Grid.getGridObject('demo');
    var cm = mygrid.getColumnModel();
    cm.setRenderer(0, myFormatter);
    mygrid.reconfigure(mygrid.getStore(),cm);
};
</script>
</cfsavecontent>
<cfhtmlhead text="#formatGridInit#">
<cfset ajaxOnLoad("formatgrid")>

<cfscript>
        rs = QueryNew('salary', 'integer');
        QueryAddRow(rs,3);
        QuerySetCell(rs, 'salary', '100000', 1);
        QuerySetCell(rs, 'salary', '80000', 2);
        QuerySetCell(rs, 'salary', '5000', 3);
</cfscript>

<cfform>
    <cfgrid format="html" name="demo" query="rs">
        <cfgridcolumn name="salary" type="numeric">
    </cfgrid>
</cfform>
</body>
</html>

NOTE - Make sure your HTML has <head></head> in order for the <cfhtmlhead> to work.

NOTE - The grid name in this code var mygrid = ColdFusion.Grid.getGridObject('demo'); must match your grid's name.

NOTE - Set the number in this code cm.setRenderer(0, myFormatter); to the column that you want to apply the format to (columns in the grid are zero based).

Upvotes: 2

BKK
BKK

Reputation: 2073

Apparently the "currency" type which would support the mask you used is only available in format="flash|applet" mode only.

The easiest way would be format the data in the query before it hits the CFGRID. Alternatively you could just reformat those cells using ColdFusion:

<cfscript>
        rs = QueryNew('salary', 'varchar');
        QueryAddRow(rs,3);
        QuerySetCell(rs, 'salary', '100000', 1);
        QuerySetCell(rs, 'salary', '80000', 2);
        QuerySetCell(rs, 'salary', '5000', 3);
</cfscript>

<!--- Loop Over Query and Reformat Salary --->
<cfloop query="rs" startrow="1">
    <cfscript>
        QuerySetCell(rs, 'salary', dollarFormat(salary), currentrow);
    </cfscript>
</cfloop>

<cfform>
    <cfgrid format="html" name="demo" query="rs">
        <cfgridcolumn name="salary">
    </cfgrid>
</cfform>

Upvotes: 0

Related Questions