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;
};
}(),
即可解決。