[color=darkred][b]最近用Ext做一个小程序,在用到GridPanel的时候遇到了一些问题,但最后都解决了
,得到了很多,写下来与大家分享。[/b][/color]
首先,要在界面显示层用GridPanel来显示数据,具体代码如下:
[b][color=darkred]在body标签里面有相应的div显示:[/color][/b]
[color=darkred][b]在struts.xml配置文件中的代码部分如下:[/b][/color]
[color=darkred][b]同时,还记着有json的插件必须引进来,具体的插件在本文的附件中。[/b][/color]
[color=darkred][b]Java代码部分如下:[/b][/color]
[color=darkred]
好了,这样就可以了。试试吧![/color]
,得到了很多,写下来与大家分享。[/b][/color]
首先,要在界面显示层用GridPanel来显示数据,具体代码如下:
<link rel="stylesheet" type="text/css" href="extjs/resources/css/ext-all.css"
/>
<script type="text/javascript" src="extjs/adapter/ext/ext-base.js"></script>
<script type="text/javascript" src="extjs/ext-all.js"></script>
<script type="text/javascript">
Ext.onReady(function(){
Ext.QuickTips.init();
var sm = new Ext.grid.CheckboxSelectionModel();
var cm = new Ext.grid.ColumnModel([
sm,
{header:'NO.',renderer:function(value, cellmeta, record, rowIndex){
return rowIndex + 1;
}},
{header:'品牌',dataIndex:'name',sortable:true},
{header:'型号',dataIndex:'size',sortable:true},
{header:'价格',dataIndex:'price',sortable:true}
]);
var ds = new Ext.data.Store({
proxy: new Ext.data.HttpProxy({url:'softMachineInit.action'}),
reader: new Ext.data.JsonReader({
totalProperty: 'totalProperty',
root: 'list'},//注意list,totalProperty在相应的action类中得有
//get,set方法。
Ext.data.Record.create(
[{name:'name'},
{name:'size'},
{name:'price'}
]))
});
ds.load({params:{start:0,limit:5}});
var grid = new Ext.grid.GridPanel({
id:'grid',
title:'饮水机信息表',
ds: ds,
height:300,
cm: cm,
sm: sm,
stripeRows:true,//隔行换色
loadMask:true,//在加载数据时的遮罩效果
trackMouseOver:true,
bbar: new Ext.PagingToolbar({
pageSize: 5,
store: ds,
displayInfo: true,
displayMsg: '显示第 {0} 条到 {1} 条记录,一共 {2} 条',
emptyMsg: "没有记录"
})
});
grid.render("grid-example");
});
</script>
[b][color=darkred]在body标签里面有相应的div显示:[/color][/b]
<div id="grid-example" style="margin: 10px;"></div>
[color=darkred][b]在struts.xml配置文件中的代码部分如下:[/b][/color]
<struts>
<package name="My" extends="json-default">
<action name="softMachineInit" class="softMachineAction" method="softMachineInit">
<result type="json">
</result>
</action>
</package>
</struts>
[color=darkred][b]同时,还记着有json的插件必须引进来,具体的插件在本文的附件中。[/b][/color]
[color=darkred][b]Java代码部分如下:[/b][/color]
public class SoftMachineAction extends ActionSupport {
/**
*
*/
private static final long serialVersionUID = 1L;
private SoftMachine softMachine;
private SoftMachineDao softMachineDao;
private String start;
private String limit;
List list;
Long totalProperty;
public Long getTotalProperty() {
return totalProperty;
}
public void setTotalProperty(Long totalProperty) {
this.totalProperty = totalProperty;
}
public List getList() {
return list;
}
public void setList(List list) {
this.list = list;
}
public void setSoftMachine(SoftMachine softMachine) {
this.softMachine = softMachine;
}
public void setSoftMachineDao(SoftMachineDao softMachineDao) {
this.softMachineDao = softMachineDao;
}
@SuppressWarnings("unchecked")
public String softMachineInit() {
this.setTotalProperty(80L);
list = new ArrayList<SoftMachine>();
try {
int index = Integer.parseInt(this.start);
int pageSize = Integer.parseInt(this.limit);
//这段代码挺重要的,通过得到index,pageSize,可以从数据
// 库来执行相应的select操作。下面的for循环是一个模拟过程
//同时,特别注意,在action类中,必须有list和totalProperty
//的get,set方法,这两个属性时与Ext中的代码心对应的。
for(int i=0;i<5;i++){
SoftMachine softMachine = new SoftMachine();
softMachine.setName("name"+i);
softMachine.setSize("size"+i);
softMachine.setPrice(Long.valueOf(77+i));
list.add(softMachine);
}
} catch (Exception ex) {
}
return SUCCESS;
}
public void setStart(String start) {
this.start = start;
}
public void setLimit(String limit) {
this.limit = limit;
}
}
[color=darkred]
好了,这样就可以了。试试吧![/color]