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;
        };
    }(),

即可解决。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章