简体   繁体   English

在ExtJS网格中,将NULL值显示为空字符串而不是“null”字符串的最简单方法是什么?

[英]What's the easiest way to display NULL value as empty string instead of “null” string in an ExtJS grid?

I have a server-side script (PHP) returning JSON data to populate an ExtJS Grid. 我有一个服务器端脚本(PHP)返回JSON数据来填充ExtJS网格。

This data comes from a MySQL query, containing some NULL values. 此数据来自MySQL查询,包含一些NULL值。

When PHP encodes it into the JSON notation, it keeps the NULL intact: 当PHP将其编码为JSON表示法时,它保持NULL完整:

{"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. 但是......当这些数据显示在ExtJS网格中时,它会在NULL值的位置显示一个“ null ”字符串。

I don't want it to display " null ". 我不希望它显示“ null ”。 I just want it empty (an empty string). 我只想要它空(一个空字符串)。

What is the easiest way to achieve this? 实现这一目标的最简单方法是什么?

Thanks. 谢谢。

Have you define an Ext.data.Model? 你定义了一个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... 如果你定义和Ext.data模型并在Ext.data.Store中使用它来读取JSON字符串非常容易...

You just need to declare the field type: 'auto' 您只需要声明字段类型:'auto'

try this: 尝试这个:

Ext.define('testModel', {
        extend  : 'Ext.data.Model',
        fields  :[
                     {name: 'Id', type: 'int'},
                     {name: 'name', type: 'string'},
                     {name: 'url', type: 'auto'},
                     {name: 'cls', type: 'string'},                     
                     {name: 'expanded', type: 'boolean'}
                  ]     
    });


var testStore = Ext.create('Ext.data.Store', {
                         model  : 'testModel',
                         proxy  : {
                                      type   : 'memory',
                                      reader : {type:'json'}
                                  }
                });

and use the testStore to fill up the data in your grid. 并使用testStore填充网格中的数据。

I'm not sure if the easiest but the first thing that comes to my mind is creating a custom renderer . 我不确定是否最容易但是我想到的第一件事就是创建一个自定义renderer You can use something like this: 你可以使用这样的东西:

function render(value){
    if(value === null){
        value = '';
    }
    return value;
}

WoLpH's answer is correct. WoLpH的回答是正确的。 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). 实际上,我正在处理一个treegrid (Ext.ux.tree.TreeGrid),而不是一个常见的ExtJS网格(Ext.grid.GridPanel)。

The columns of the treegrid are not of Ext.grid.Column type, as the common grid. treegrid的列不是 Ext.grid.Column类型,作为公共网格。 They are of Ext.list.Column type. 它们是Ext.list.Column类型。

This means that the renderer configuration option does not apply (it exists and works on Ext.grid.Column, but not on Ext.list.Column). 这意味着渲染器配置选项不适用(它存在并且适用于Ext.grid.Column,但不适用于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. 要更改Ext.list.Column的呈现,可以使用tpl配置选项,该选项使用Ext.XTemplate机制。

Below, I share the configuration of my treegrid column that did what I wanted: 下面,我分享了我的treegrid列的配置,它做了我想要的:

tpl: '<tpl if="url === null"></tpl><tpl if="url !== null">{url}</tpl>'

The configuration above made my column render an empty string for NULL values, instead of a " null " string, in the treegrid ! 上面的配置使我的列在treegrid中为NULL值呈现空字符串,而不是“ null ”字符串!


Final note: I voted on WoLpH's answer because it is correct. 最后说明:我投票支持WoLpH的答案,因为它是正确的。 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. 谢谢。

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. 您需要在结果集中检查任何NULL实例,并将它们转换为空字符串,然后再将其返回给客户端。

In ExtJS 3.4 there is a useNull property that can be set for a numeric field (not strings though). 在ExtJS 3.4中,有一个useNull属性可以为数字字段设置(尽管不是字符串)。

http://docs.sencha.com/extjs/3.4.0/#!/api/Ext.data.Field-cfg-useNull http://docs.sencha.com/extjs/3.4.0/#!/api/Ext.data.Field-cfg-useNull

(Optional) Use when converting received data into a Number type (either int or float). (可选)将接收的数据转换为数字类型(int或float)时使用。 If the value cannot be parsed, null will be used if useNull is true, otherwise the value will be 0. Defaults to false 如果无法解析该值,则如果useNull为true,则将使用null,否则该值将为0.默认为false

Defaults to: false 默认为:false

Eg 例如

{
    name: 'myId',
    type: 'int',
    useNull: true
},

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM