Ext 的GridPanel 与Struts2.0的交互显示

[color=darkred][b]最近用Ext做一个小程序,在用到GridPanel的时候遇到了一些问题,但最后都解决了

,得到了很多,写下来与大家分享。[/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]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值