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