把娃扶起来 2009-04-30 10:44
浏览 231
已采纳

gridpanel与后台数据库的交互

我想做这么个功能:在struts的action里调用方法得到数据库内容,然后在girdpanel 里显示. 但是页面只是显示数据,没有控件。不知道原因,我的表里就2个字段,id和name,希望高手能给解决下~谢谢啦。我的代码如下:
test.js:
Ext.onReady(function() {
var reader = new Ext.data.JsonReader({
root : 'list',
totalProperty : 'totalProperty',
fields : [{
name : 'name',
mapping : 'list.name'
}, {
name : 'id',
mapping : 'list.id'
}]
}

);
var store = new Ext.data.JsonStore({
proxy : new Ext.data.HttpProxy({
url : 'person.do',
method : 'GET',
reader : reader
})

});
var cm = new Ext.grid.ColumnModel([{
header : "name",
dataIndex : "name"
}, {
header : "id",
dataIndex : "name"
}]

);
var panel = new Ext.grid.GridPanel({
title : "EXT Grid",
width : 500,
height : 200,
store : store,
cm : cm

});
panel.render(Ext.getBody());
});



Person.Action:
Configuration con = new Configuration().configure();
SessionFactory sf = con.buildSessionFactory();
Session session = sf.openSession();
List l = null;
String hql = "FROM Person";
Query q = session.createQuery(hql);
l = q.list();
int totalProperty=l.size();
Iterator it = l.iterator();
JSONArray jsonArray = new JSONArray();
try {
while (it.hasNext()) {
Person p = (Person) it.next();
JSONObject jsonObject = new JSONObject();
jsonObject.put("id", p.getId());
jsonObject.put("name", p.getName());
jsonArray.put(jsonObject);

}
} catch (JSONException e) {
e.printStackTrace();
}
JSONObject responseJSON = new JSONObject();
try {
responseJSON.put("list", jsonArray);
responseJSON.put("totalProperty", totalProperty);
response.getWriter().write(responseJSON.toString());
} catch (JSONException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}

页面显示如下:{"list":[{"name":"mp ","id":1},{"name":"xjj ","id":2},{"name":"tyk ","id":3},{"name":"fp ","id":4},{"name":"jll ","id":5},{"name":"zb ","id":6},{"name":"zaer ","id":7}],"totalProperty":7}

地址栏:http://localhost:8080/EXT/person.do
问题补充:
不好意思 昨天是我的一点错误 ,现在问题是页面只显示gridpanel,没有数据的现实!!

  • 写回答

4条回答 默认 最新

  • iteye_8781 2009-04-30 15:23
    关注

    可能是这里的原因:
    var reader = new Ext.data.JsonReader({
    root : 'list',
    totalProperty : 'totalProperty',
    [color=red] fields : [{
    name : 'name',
    mapping : 'list.name'
    },
    {
    name : 'id',
    mapping : 'list.id'
    }][/color]
    });

    改为这样试试:
    var record = Ext.data.Record.create([
    {
    name: 'id',
    mapping: 'id'
    },

        {
            name: 'name',
            mapping: 'name'
        },
    

    ]);
    var store = new Ext.data.Store({
    proxy: new Ext.data.HttpProxy({
    url : 'person.do',
    method : 'GET'
    }),
    reader: new Ext.data.JsonReader({
    root: 'list',
    totalProperty: 'totalProperty',
    remoteSort:true
    },record)
    });
    [color=red]store.load();[/color]或者[color=red]autoLoad:true;[/color]这个加在new Ext.data.Store中
    比如:
    var store = new Ext.data.Store({
    [color=red]autoLoad:true,[/color]
    proxy: new Ext.data.HttpProxy({
    url : 'person.do',
    method : 'GET'
    }),
    reader: new Ext.data.JsonReader({
    root: 'list',
    totalProperty: 'totalProperty',
    remoteSort:true
    },record)
    });

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 求学软件的前人们指明方向🥺
  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接
  • ¥15 MCNP里如何定义多个源?
  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services