在数据网格列中转义 html 特殊字符

发布于 2024-11-14 19:27:13 字数 2570 浏览 4 评论 0原文

我有一个数据网格(6 列),它显示数据库中的字符串。我正在尝试使用 labelfunction 来转义字符串中的 html 特殊字符(如 &apos、&amp 等)。

我在网上找到了这个:

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

但是,当我这样做时,我收到错误:无法访问空对象引用的属性或方法。

当我像这样更改上面的代码时,效果很好:

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

其中computer_name是此数据网格列的数据字段。

发布数据网格代码:

<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>

到目前为止,这一切工作正常(仅将标签函数应用于 2 列)..但我想将标签函数应用于所有列......

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,& 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 ...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

哀由 2024-11-21 19:27:13

尝试使用以下内容:

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

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;
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文