extjs 2.3 修复 jsonstore 嵌套多级引用数据NULL的BUG

Ext版本:2.3.0

搜索到一篇有用的链接是:

JsonStore and nested/multi level data in a GridPanel


我在使用Ext.grid.GridPanel时,用到了多级JSON。

如:

{
"data": [
{
    "id" : 1,"amount" : 100,"costumer" : {
        "id" : 123,"name" : "Luis Valdés"
    }
},{
    "id" : 2,"amount" : 100
}
]
}

在定义列时有 {header:'id',dataIndex:'id'},
{header:'amount',dataIndex:'amount'},
{header:'cutomerid','dataIndex:'customer.id'},
{header:'customerName',dataIndex:'customer.name'}

当customer不为空时都正常,空的时候就显示不出来。解决办法如下,将Ext.data.JsonReader的getJsonAccessor现有方法

    getJsonAccessor: function(){
        var re = /[\[\.]/;
        return function(expr) {
            try {
                return(re.test(expr))
                    ? new Function("obj","return obj." + expr)
                    : function(obj){
                        return obj[expr];
                    };
            } catch(e){}
            return Ext.emptyFn;
        };
    }(),

替换为:
    getJsonAccessor: function(){
        var re = /[\[\.]/;
        return function(expr) {
            try {
                return(re.test(expr))
                    ? new Function("obj","try{return obj." + expr+"}catch(e){}return '';")
                    : function(obj){
                        return obj[expr];
                    };
            } catch(e){}
            return Ext.emptyFn;
        };
    }(),
即可解决

相关文章

AJAX是一种基于JavaScript和XML的技术,能够使网页实现异步交...
在网页开发中,我们常常需要通过Ajax从后端获取数据并在页面...
在前端开发中,经常需要循环JSON对象数组进行数据操作。使用...
AJAX(Asynchronous JavaScript and XML)是一种用于创建 We...
AJAX技术被广泛应用于现代Web开发,它可以在无需重新加载页面...
Ajax是一种通过JavaScript和HTTP请求交互的技术,可以实现无...