Friday, May 04, 2012

Conditionally Change the Color Of a Cell Text In cfgrid

This topic consists of three parts

1. Grid display page.
2. ColdFusion component and function to bind the grid.
3. JavaScript for changing the color of specific grid Cell.

1. cfgridDemo.cfm(Grid Display Page)

        <script type="text/javascript" src="./cfgridDemo.js"></script>
            <!-- This is grid display Order information of a online Shop. orderID, Order Date, Customer Name, Order Status
                   The order status can be complete, pending, Shipped, etc.Depending on the order status the color of the status text will change
        <cfform name="displayGridForm" format="html">
            <cfgrid format="html" name="displayGrid" autoWidth="true" pagesize="50" selectmode="row" bind="cfc:testCode.cfgridDemo.getGridData({cfgridpagesize},{cfgridpage},{cfgridsortcolumn},{cfgridsortdirection})" sort="true">
                <cfgridcolumn name="ORDERID"            width="150"        header="Order ID"    headerbold="true" />
                <cfgridcolumn name="ORDERDATE"        width="100"        header="Order Date"    headerbold="true"  type="date" />
                <cfgridcolumn name="CUSTOMERFIRSTNAME"    width="150"        header="Customer First Name"    headerbold="true" />
                <cfgridcolumn name="CUSTOMERLASTNAME"        width="150"     header="Customer Last Name"    headerbold="true" />
                <cfgridcolumn name="ORDERSTATUSID"    width="100"        header="Order Status ID"        headerbold="true" />
                <cfgridcolumn name="STATUS"        width="100"        header="Status"        headerbold="true" />
        <cfset ajaxOnLoad("init") />

2. cfgridDemo.cfc(ColdFusion component and function to bind the grid)

    <cffunction name="getGridData" access="remote" returntype="Struct" output="false">
        <cfargument name="pageSize" required="false" default="30" hint="No of records to display in each page of cfgrid" />
        <cfargument name="pageNo" required="false" default="1" hint="Current page no of cfgrid which you want to view" />
        <cfargument name="sortColumnName" required="false" default="ORDERID" hint="Default sort column for query sorting" />
        <cfargument name="sortDirection" required="false" default="ASC" hint="Default query sort direction" />
        <cfset variables.qryGetMedia = queryNew("") />
        <!--- Default Sort Coulmn name if the sortColumn contains empty strings --->
        <cfif len(arguments.sortColumnName)>
            <cfset variables.sortColumnName = arguments.sortColumnName />
            <cfset variables.sortColumnName = "ORDERID" />
        <!--- Default Sort direction if the sort direction contains empty string --->
        <cfif len(arguments.sortDirection)>
            <cfset variables.sortDirection = arguments.sortDirection />
            <cfset variables.sortDirection = "ASC" />
        <!--- Here I have used the dafualt datasource created during ColdFusion installation.
            So you can run the code directly. --->
        <cfquery datasource="cfartgallery" name="variables.qryGetMedia">
            FROM ORDERS
            ORDER BY #variables.sortColumnName# #variables.sortDirection#
        <!--- Convert the query Object into cfgrid compatible structure --->
        <cfreturn queryConvertForGrid(variables.qryGetMedia, arguments.pageNo, arguments.pageSize) />

3. cfgridDemo.js(JavaScript for changing the color of specific grid Cell.)

var init = function(){
    //get the grid Object
    grid = ColdFusion.Grid.getGridObject('displayGrid');
    //get grid Column Model
    var gridCM = grid.getColumnModel();
    //Call the color renderer function to render the color
    gridCM.setRenderer(gridCM.getIndexById('STATUS'), colorRenderer);

//Function to color renderer
function colorRenderer(value)
    if( > -1)
        return '<b style="color:#00FF00;">' + value + '</b>';
    else if( > -1)
        return '<b style="color:#FF0000;">' + value + '</b>';
    else if( > -1) 
        return '<b style="color:#00FFFF;">' + value + '</b>';
    else if( > -1) 
        return '<b style="color:#0000FF;">' + value + '</b>';
        return value;

After all these steps if we will run cfgridDemo.cfm we will get the following out put. We can also change the display style by changing the JavaScript as I have done.

Other Grid related topics:
1. Starting with CFGRID( part - 1 )
2. Starting with CFGRID( part - 2 )(Auto Refreshing CFGRID)
3. Export cfgrid Data or Table Data in Excel, Pdf and CSV Format(ColdFusion - 9)
4. Search Functionality in CFGRID

No comments:

Post a Comment