将 NULL 值显示为空字符串而不是“null”的最简单方法是什么? ExtJS 网格中的字符串?
我有一个服务器端脚本 (PHP) 返回 JSON 数据以填充 ExtJS 网格。
该数据来自 MySQL 查询,包含一些 NULL 值。
当 PHP 将其编码为 JSON 表示法时,它会保持 NULL 不变:
{"id":"33","name":"Test1","url":null,"cls":"x-tree-noicon","expanded":true}
但是...当此数据显示在 ExtJS 网格中时,它会在 NULL 值的位置显示一个“null”字符串。
我不希望它显示“null”。我只想让它为空(一个空字符串)。
实现这一目标的最简单方法是什么?
谢谢。
I have a server-side script (PHP) returning JSON data to populate an ExtJS Grid.
This data comes from a MySQL query, containing some NULL values.
When PHP encodes it into the JSON notation, it keeps the NULL intact:
{"id":"33","name":"Test1","url":null,"cls":"x-tree-noicon","expanded":true}
But... when this data is displayed in the ExtJS grid, it shows a "null" string in the place of the NULL value.
I don't want it to display "null". I just want it empty (an empty string).
What is the easiest way to achieve this?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您的另一个选择是从后端处理它。您需要检查结果集中是否有任何 NULL 实例,并将它们转换为空字符串,然后再将其返回给客户端。
Your other option would be to handle it from the back end. You would need to check in the result set for any instances of NULL and convert them to empty strings before returning it back to the client.
您定义了 Ext.data.Model 吗?如果您定义 Ext.data 模型并在 Ext.data.Store 中使用它来读取 JSON 字符串非常容易...
您只需要声明字段类型:'auto'
试试这个:
并使用 testStore 来填写网格中的数据。
Have you define an Ext.data.Model? if you define and Ext.data Model and the use it in the Ext.data.Store to read the JSON string is very easy...
You just need to declare the field type: 'auto'
try this:
and use the testStore to fill up the data in your grid.
在 ExtJS 3.4 中,有一个 useNull 属性可以为数字字段(但不是字符串)设置。
http://docs.sencha。 com/extjs/3.4.0/#!/api/Ext.data.Field-cfg-useNull
false
In ExtJS 3.4 there is a useNull property that can be set for a numeric field (not strings though).
http://docs.sencha.com/extjs/3.4.0/#!/api/Ext.data.Field-cfg-useNull
E.g.
我不确定是否最简单,但我首先想到的是创建一个自定义渲染器。你可以使用这样的东西:
I'm not sure if the easiest but the first thing that comes to my mind is creating a custom
renderer
. You can use something like this:WoLpH 的答案是正确的。我自己的问题在某种程度上是“不正确的”...... :-)
事实上,我正在处理一个 treegrid (Ext.ux.tree.TreeGrid),不是常见的 ExtJS 网格 (Ext.grid.GridPanel)。
树形网格的列不是 Ext.grid。 Column类型,与常见的网格一样。它们是 Ext.list.Column类型。
这意味着 renderer 配置选项不适用(它存在并适用于 Ext.grid.Column,但不适用于 Ext.list.Column)。
要更改 Ext.list.Column 的呈现,可以使用 tpl 配置选项,该选项使用 Ext.XTemplate 机制。
下面,我分享了我的 treegrid 列的配置,它实现了我想要的功能:
上面的配置使我的列为 NULL 值呈现一个空字符串,而不是“null”字符串,在树形网格中!
最后说明:我对 WoLpH 的答案进行了投票,因为它是正确的。我的问题具有误导性,我宁愿不更改/编辑它,而是分享我自己的发现作为另一个答案。我也感谢他,因为它让我专注于正确的事情。谢谢。
WoLpH's answer is correct. My own question was somehow "incorrect"... :-)
In fact, I am dealing with a treegrid (Ext.ux.tree.TreeGrid), not a common ExtJS grid (Ext.grid.GridPanel).
The columns of the treegrid are not of Ext.grid.Column type, as the common grid. They are of Ext.list.Column type.
This means that the renderer configuration option does not apply (it exists and works on Ext.grid.Column, but not on Ext.list.Column).
To change the rendering of a Ext.list.Column, there is the tpl configuration option available, which uses the Ext.XTemplate mechanism.
Below, I share the configuration of my treegrid column that did what I wanted:
The configuration above made my column render an empty string for NULL values, instead of a "null" string, in the treegrid!
Final note: I voted on WoLpH's answer because it is correct. My question was misleading, and I preferred not to change/edit it, but to share my own findings as another answer. I also thank him because it made me focus on the right things. Thanks.