论坛首页 AJAX版 EXT

用JsonReader读取EXT Grid的问题

浏览 2292 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
最后更新时间:2007-09-06
Ext.onReady(function(){

    // create the Data Store
    var ds = new Ext.data.Store({
        // load using HTTP
        proxy: new Ext.data.HttpProxy({url: 'createXml'}),

        // the return will be Json, so lets set up a Jsonreader
        reader: new Ext.data.JsonReader({
               // root will have an "ItemAttributes" tag
                totalProperty:'results',              
                root: 'ItemAttributes',
                id: 'a_id'
           },[
           {name:'a_id',mapping:'a_id'},
           {name:'Author',mapping:'Author'},   
     {name:'Title',mapping:'Title'},
       {name:'Manufacturer',mapping:'Manufacturer'},
       {name:'ProductGroup',mapping:'ProductGroup'}
           ])
    });
    var cm = new Ext.grid.ColumnModel([
    {header: "a_id", width: 40, dataIndex: 'a_id'},
    {header: "Author", width: 120, dataIndex: 'Author'},
{header:"Title", width: 120, dataIndex: 'Title'},
{header:"Manufacturer", width: 115, dataIndex: 'Manufacturer'},
{header: "ProductGroup", width: 200, dataIndex: 'ProductGroup'}
]);
    cm.defaultSortable = true;

    // create the grid
    var grid = new Ext.grid.Grid('example-grid', {
        ds: ds,
        cm: cm
    });
    grid.render();

    ds.load();
});

后台代码
Vo.PublishDAO dao=new PublishDAO();
Session session = dao.getSession();
Transaction tr = session.beginTransaction();
java.util.List list=dao.getList();
tr.commit();
StringBuilder builder=new StringBuilder();
org.json.JSONObject obj=new JSONObject();
org.json.JSONArray array=new JSONArray();
try {
obj.put("results",list.size());
for(int i=0;i<list.size();i++){
org.json.JSONObject obj1=new JSONObject();
Vo.Publish p=(Vo.Publish)list.get(i);
obj1.put("a_id", p.getId());
obj1.put("Author", p.getAuthor());
obj1.put("Title", p.getTitle());
obj1.put("Manufacturer", p.getManufacturer());
obj1.put("ProductGroup", p.getGroup());
array.put(obj1);
obj.put("ItemAttributes", array);
}
} catch (JSONException e) {
e.printStackTrace();
}
builder.append(obj);
System.out.print(builder.toString());

response.getWriter().print(builder.toString());
session.close();
session.clear();
数据在客户端显示不出来,请问哪里有问题.我的数据在后台已经生成了json的格式了,而且没有错.
{"ItemAttributes":[{"Author":"nmnm","ProductGroup":"nm","Title":"nm","Manufacturer":"nm","a_id":10},{"Author":"dfg","ProductGroup":"fh","Title":"gfh","Manufacturer":"fh","a_id":11}],"results":2}这个是后台打印出来的json字符串.请高手看下有什么问题?
   
最后更新时间:2007-09-06
建议在Firefox下用Firebug检查一下.
   
0 请登录后投票
最后更新时间:2007-10-05
回调函数没有构造问题。在你的代码后给你一段代码,你试试看.
System.out.println(builder.toString());
boolean scriptTag = false;
String cb = request.getParameter("callback");
if (cb != null) {
    scriptTag = true;
    response.setContentType("text/javascript");
} else {
    response.setContentType("application/x-json");
}
if (scriptTag) {
    out.write(cb + "(");
}

out.print(builder.toString());
if (scriptTag) {
    out.write(");");
}

return null;
   
0 请登录后投票
最后更新时间:2007-10-06
在JsonStore中的Config Options里,加上fields:[
{name:'a_id',mapping:'a_id'},
{name:'Author',mapping:'Author'},
{name:'Title',mapping:'Title'},
{name:'Manufacturer',mapping:'Manufacturer'},
{name:'ProductGroup',mapping:'ProductGroup'}
]),试试看行不行,我前段时间遇见相同的问题,是这样解决的,不过我觉得好像是ext的bug,因为在JsonReader中我已经定义好RecordType了,JsonStore的fields应该和RecordType的作用一样。
   
0 请登录后投票
论坛首页 AJAX版 EXT

跳转论坛:
JavaEye推荐