Extjs 4.2 +Struts2 實現數據動態加載

//自定義一個的js

function commodityQuery() {

    Ext.state.Manager.setProvider(Ext.create('Ext.state.CookieProvider'));

// Set up a model to use in our Store

Ext.define('Commodity', {

   extend: 'Ext.data.Model',

   fields: [

       {name: 'commodityId', type: 'int'},

       {name: 'commodityName',  type: 'string'},

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

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

   ]

});

 var store = Ext.create('Ext.data.Store', {

   model: 'Commodity',

   proxy: {

       type: 'ajax',

       url : 'commodityQuery',

       successProperty:'success',

       reader: {

           type: 'json',

           root: 'results'

       }

   },

   autoLoad: {start:0,limit:4}

});

var gridPanel = Ext.create('Ext.grid.Panel', {

width : 586,

height : 375,

store : store,

columns : [ {

text : "商品編號",

dataIndex : "commodityId",

sortable : true

}, {

text : "商品名稱",

dataIndex : "commodityName",

sortable : true

}, {

text : "商品價格",

dataIndex : "price",

sortable : true,

autoWidth : 50

}, {

text : "商品折扣",

dataIndex : "agio",

sortable : true

} ],

forceFit : true,

bbar : [ Ext.create("Ext.toolbar.Paging", {

store : store,

displayInfo : true,

displayMsg : "顯示 {0} - {1} 條,共計 {2} 條",

emptyMsg : "沒有任何記錄",

width : "100%"


}) ]


});

var commodityQueryWindow = Ext.create("Ext.window.Window", {

width : 600,

height : 400,

title : "商品信息查詢",

resizable : false,

modal : true,

items : gridPanel

});

commodityQueryWindow.show();


}

**********************

Struts.xml 中的配置

       //method 可以寫成DMI方式

<action name="commodityQuery" class="com.rjxy.action.CommodityQueryAction"          method="getCommodity" >

<result type="json">

                <param name="root">responseJson</param>  

            </result>

</action>

**********************

CommodityQueryAction中的變量及方法

private String msg;

private List<Commodity> list;

private Integer totalCount;

private Map responseJson; 

private SaleDaoImpl dao;

private Integer start;

private Integer limit;

/**

* Query Commodity  list.

*/

public String getCommodity(){

Map<String, Object> map = new HashMap<String, Object>();  

try{

conn = GetConnection.getConn();//jdbc連接,網友可以用hibernate

dao = new SaleDaoImpl();

list = dao.queryAllOrder();

if(list != null){

this.setTotalCount(list.size());

map.put("results", list);

       map.put("totalCount", totalCount);

       this.setResponseJson(map);

}

return SUCCESS;

}catch(Exception e){

e.printStackTrace();

return ERROR;

}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章