Escaping html special chars in a datagrid column

659 Views Asked by At

i have a datagrid (6 columns) which displays strings from the database. I am trying to use a labelfunction to escape html special characters in the string (like &apos,&amp etc).

I found this on the net :

protected function makeSpecialChars(item:Object,dgName:DataGridColumn):String {
            var s:String = agentName.dataField;
            var v:String = item.s;
            return new XMLDocument(v).firstChild.nodeValue;
        }

However when i do this I get the error : Cannot access a property or method of a null object reference.

This works fine when i change the above code like this :

protected function makeSpecialChars(item:Object,agentName:DataGridColumn):String {
            return new XMLDocument(item.computer_name).firstChild.nodeValue;
        }

where computer_name is the datafield of this datagrid column.

Posting Datagrid code :

<components:DoubleClickDataGrid xmlns:fx="http://ns.adobe.com/mxml/2009" 
                            xmlns:s="library://ns.adobe.com/flex/spark" 
                            xmlns:mx="library://ns.adobe.com/flex/mx" 
                            xmlns:components="components.*"
                            width="100%" height="100%"
                            editable="true" 
                            focusColor="#CEDA9B" 
                            rollOverColor="#CEDA9B" 
                            selectionColor="#84A207"
                            dataProvider="{invList}"
                            creationComplete="doubleclickdatagrid1_creationCompleteHandler(event)"
                            itemEditEnd="handleInventoryEdit(event)">
<components:columns>
    <mx:DataGridColumn width="120" dataField="computer_name" headerText="Computer Name" textAlign="center" labelFunction="makeSpecialChars"/>
    <mx:DataGridColumn dataField="path" headerText="Path" textAlign="center" labelFunction="makeSpecialChars"/>
    <mx:DataGridColumn width="110" dataField="computer_type" textAlign="center" headerText="Computer Type" />
    <mx:DataGridColumn width="180" dataField="username" headerText="Username" textAlign="center" />
    <mx:DataGridColumn width="90" dataField="ip_address" headerText="IP Address" textAlign="center" />
    <mx:DataGridColumn width="130" dataField="mac_address" headerText="MAC Address" textAlign="center" />
    <mx:DataGridColumn width="180" dataField="computer_model" headerText="Computer Model" textAlign="center" />
<mx:DataGridColumn width="70" editable="false" headerText="Actions" textAlign="center" itemRenderer="renderers.ActionRenderer" />
</components:columns>

This much is working fine as of now (applied labelfunction to only 2 columns).. But I want to apply the label function to all the columns ...

1

There are 1 best solutions below

7
On

Try to use the following:

protected function makeSpecialChars(item:Object,agentName:DataGridColumn):String {
            var s:String = agentName.dataField;
            var v:String = item[s];
            return new XMLDocument(v).firstChild.nodeValue;
        }