If you ever used cfgrid, you know that it accepts a query as its data. Each cfgridcolumn of the grid has to be a column of the query. You cannot combine two or more columns or add something to the column. That means that any formatting of the data needs to be done before giving the query to the grid (unless you loop over it and use
If you ever used cfgrid, you know that it accepts a query as its data. Each cfgridcolumn of the grid has to be a column of the query. You cannot combine two or more columns or add something to the column. That means that any formatting of the data needs to be done before giving the query to the grid (unless you loop over it and use
Most likely, you would do this in sql, by creating a new column that is a combination of other columns or by adding strings to a given column. The same problem arises if you wanted to format the data using something different than the provided types (currency for example) and the mask attribute is known to be unpredictable sometimes. The requirement of having to provide only a column, and nothing else, to the
So let me explain how to do data formatting on the client as opposed as in the query. We will use the onload function available in the 7.01 updater to change the "labelFunction" of the column we want to format. In this example, we will use three different columns to show an address in one column. This can also be used in combination with variableRowHeight, as shown in the second example, to have a multiple-line row (you can also set the height of the rows if it is fixed).
Note that the query columns are: id, address, city, state
function formOnLoad(){
myGrid.getColumnAt(1).labelFunction = setLabel;
}
function setLabel(item:Object, columnName:String): String {
return item.address + ', ' + item.city + ', ' + item.state;
};
Example with multiple lines (adding "/n") and having variableRowHeight = true
function formOnLoad(){
myGrid.variableRowHeight = true;
myGrid.getColumnAt(1).labelFunction = setLabel;
myGrid.getColumnAt(1).wordWrap = true;
}
function setLabel(item:Object, columnName:String): String {
return item.address + '\n' + item.city + ', ' + item.state;
};
Suppose your database contain the filename of the image you want to show in the grid using
function formOnLoad(){
myGrid.getColumnAt(1).labelFunction = setLabel;
}
function setLabel(item:Object, columnName:String): String {
if ( item[columnName] != undefined) {
return "images/" + item[columnName];
}
else {
return "";
}
};
From what I've seen so far since I've started using this technique, it has the nice effect of fixing the "mysteriously disappearing image" bug experienced in Firefox when using
You can use this technique to implement formatting other than the built-in types (I just couldn't think of a quick example). You can also do the same with cfselect, but you won't have all the query columns available unless you loaded the data via remoting, so you'll have label and data as the only sources.