picora 2009-04-22 09:43
浏览 136
已采纳

extJS grid取数据遇到的问题

根据官方的例子改了一个从jsp读取数据的grid,firebug里response有响应的数据,但grid里显示不出来。
以下是我的文件,一个html,一个js,一个jsp,请帮忙看一下问题出在哪里。

[code="java"]


content="text/html; charset=iso-8859-1">
Array Grid Example
href="ext/resources/css/ext-all.css" />







Array Grid Example



This example shows how to create a grid from Array data.




[/code]

[code="java"]
Ext.onReady(function(){

Ext.BLANK_IMAGE_URL='extjs/resources/images/default/s.gif';

var store = new Ext.data.JsonStore({
    proxy: new Ext.data.HttpProxy({
        //url:'http://localhost/extjs/jsonAction.jsp'
        url: 'jsonAction.jsp'
    }),
    reader: new Ext.data.JsonReader({
        totalProperty: 'totalProperty',
        root: 'root'
    }),
    fields: [
       {name: 'company'},
       {name: 'price', type: 'float'},
       {name: 'change', type: 'float'},
       {name: 'pctChange', type: 'float'},
       {name: 'lastChange', type: 'date', dateFormat: 'n/j h:ia'}
    ]
}); 

store.load();

var grid = new Ext.grid.GridPanel({
    store: store,
    columns: [
        {id:'company',header: "Company", width: 160, sortable: true, dataIndex: 'company'},
        {header: "Price", width: 75, sortable: true, renderer: 'usMoney', dataIndex: 'price'},
        {header: "Change", width: 75, sortable: true, dataIndex: 'change'},
        {header: "% Change", width: 75, sortable: true, dataIndex: 'pctChange'},
        {header: "Last Updated", width: 85, sortable: true, renderer: Ext.util.Format.dateRenderer('m/d/Y'), dataIndex: 'lastChange'}
    ],
    stripeRows: true,
    autoExpandColumn: 'company',
    height:350,
    width:600,
    title:'Array Grid'
});

grid.render('grid-example');

});
[/code]

[code="java"]
<%
String json = "({totalProperty:2,root:[";

json += "{company:'3m Co',price:71.72,change:0.02,pctChange:0.03,lastChange:'9/1 12:00am'},";
json += "{company:'Alcoa Inc',price:29.01,change:0.42,pctChange:1.47,lastChange:'9/1 12:00am'}";
json += "]});";
System.out.println(json);
response.getWriter().write(json);

%>

[/code]

[b]问题补充:[/b]
回一楼的,我去掉分号或去掉分号和两边的“()”,结果还是一样,显示不出来数据
[b]问题补充:[/b]
谢谢两位,问题解决了。总结一下,希望后面遇到同样问题的朋友少走弯路。
第一个是分号的问题,以我测试情况看来,括号其实可以有。
第二个是Store的问题,yourgame提到的,换成Store就可以了。
然后我去查了一下API,发现JsonStore不需要配置reader这个属性。所以我换了另一种写法,也是可以的。

[code="java"]
var store = new Ext.data.JsonStore({
url: 'jsonAction.jsp',
// 把reader里的东西拿出来
totalProperty : 'totalProperty',
root : 'root',
fields: [

{name: 'company'},

{name: 'price', type: 'float'},

{name: 'change', type: 'float'},

{name: 'pctChange', type: 'float'},

{name: 'lastChange', type: 'date', dateFormat: 'n/j h:ia'}

]
});

[/code]

另,其实不用去配proxy,直接写url属性就可以。
[quote]
url : String If passed, an HttpProxy is created for the passed URL
[/quote]

  • 写回答

3条回答 默认 最新

  • zhoujuan520 2009-04-22 11:57
    关注

    总的来说有两个地方错误
    [code="java"]
    <%
    String json = "{totalProperty:2,root:[";
    json += "{company:'3m Co',price:71.72,change:0.02,pctChange:0.03,lastChange:'9/1 12:00am'},";
    json += "{company:'Alcoa Inc',price:29.01,change:0.42,pctChange:1.47,lastChange:'9/1 12:00am'}";
    json += "]}";//没有"()" 和";"号
    System.out.println(json);
    response.getWriter().write(json);
    %>
    [/code]

    [code="js"]
    Ext.onReady(function(){

    Ext.BLANK_IMAGE_URL='ext-2.2/resources/images/default/s.gif';
    
    var store = new Ext.data.Store({//这里换成Store即可!!
        url: 'jsonAction.jsp',
        reader : new Ext.data.JsonReader(
        {totalProperty : 'totalProperty',root : 'root'}, 
        [
           {name: 'company'},
           {name: 'price', type: 'float'},
           {name: 'change', type: 'float'},
           {name: 'pctChange', type: 'float'},
           {name: 'lastChange', type: 'date', dateFormat: 'n/j h:ia'}
        ])
    }); 
    
    store.load();
    
    var grid = new Ext.grid.GridPanel({
        store: store,
        columns: [
            {header: "Company", width: 160, sortable: true, dataIndex: 'company'},
            {header: "Price", width: 75, sortable: true, renderer: 'usMoney', dataIndex: 'price'},
            {header: "Change", width: 75, sortable: true, dataIndex: 'change'},
            {header: "% Change", width: 75, sortable: true, dataIndex: 'pctChange'},
            {header: "Last Updated", width: 85, sortable: true, renderer: Ext.util.Format.dateRenderer('m/d/Y'), dataIndex: 'lastChange'}
        ],
        height:350,
        width:600,
        title:'Array Grid'
    });
    
    grid.render('grid-example');
    

    });

    [/code]

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

报告相同问题?

悬赏问题

  • ¥15 写论文,需要数据支撑
  • ¥15 identifier of an instance of 类 was altered from xx to xx错误
  • ¥100 反编译微信小游戏求指导
  • ¥15 docker模式webrtc-streamer 无法播放公网rtsp
  • ¥15 学不会递归,理解不了汉诺塔参数变化
  • ¥15 基于图神经网络的COVID-19药物筛选研究
  • ¥30 软件自定义无线电该怎样使用
  • ¥15 R语言mediation包做中介分析,直接效应和间接效应都很小,为什么?
  • ¥15 Jenkins+k8s部署slave节点offline
  • ¥15 如何实现从tello无人机上获取实时传输的视频流,然后将获取的视频通过yolov5进行检测