根据官方的例子改了一个从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]