4.         Grid

Think of Ext2.0 (2)

Grid所需要的json字符串

{totalProperty:100,root: [{id:1,name:'二月DD1',descn:'descn1'},{id:2,name:'二月DD2',descn:'descn2'}, {id:3,name:'二月DD3',descn:'descn3'},{id:4,name:'二月DD4',descn:'descn4'}, {id:5,name:'二月DD5',descn:'descn5'},{id:6,name:'二月DD6',descn:'descn6'}, {id:7,name:'二月DD7',descn:'descn7'},{id:8,name:'二月DD8',descn:'descn8'}, {id:9,name:'二月DD9',descn:'descn9'},{id:10,name:'二月 DD10',descn:'descn10'}]}

Grid.jsp 根据起始值和限制数量决定返回json字符串

Think of Ext2.0 (2)<%@ page language="java" contentType="text/html; charset=UTF-8"
Think of Ext2.0 (2)    pageEncoding
="UTF-8"%>
Think of Ext2.0 (2)
<%
Think of Ext2.0 (2)
//response.setCharacterEncoding("UTF-8");
Think of Ext2.0 (2)
String start = request.getParameter("start");
Think of Ext2.0 (2)String limit 
= request.getParameter("limit");
Think of Ext2.0 (2)Think of Ext2.0 (2)
try ...{
Think of Ext2.0 (2)    
int index = Integer.parseInt(start);
Think of Ext2.0 (2)    
int pageSize = Integer.parseInt(limit);
Think of Ext2.0 (2)
Think of Ext2.0 (2)    
//String json = "{totalProperty:100,root:[";
Think of Ext2.0 (2)
    String json = "{totalProperty:100,root:[";
Think of Ext2.0 (2)Think of Ext2.0 (2)    
for (int i = index; i < pageSize + index; i++...{
Think of Ext2.0 (2)        json 
+= "{id:" + i + ",name:'二月DD" + i + "',descn:'descn" + i + "'}";
Think of Ext2.0 (2)Think of Ext2.0 (2)        
if (i != pageSize + index - 1...{
Think of Ext2.0 (2)            json 
+= ",";
Think of Ext2.0 (2)        }

Think of Ext2.0 (2)    }

Think of Ext2.0 (2)    json 
+= "]}";
Think of Ext2.0 (2)    response.getWriter().write(json);
Think of Ext2.0 (2)    
//out.print(json);
Think of Ext2.0 (2)Think of Ext2.0 (2)
}
 catch(Exception ex) ...{
Think of Ext2.0 (2)}

Think of Ext2.0 (2)
%>
Think of Ext2.0 (2)

获取数据时,如此访问grid.jsp文件

grid.jsp?start=1&limit=10

Grid使用 

Grid中字段定制

Think of Ext2.0 (2)    var cm = new Ext.grid.ColumnModel([
Think of Ext2.0 (2)Think of Ext2.0 (2)        
...{header:'描述',dataIndex:'id'},
Think of Ext2.0 (2)Think of Ext2.0 (2)        
...{header:'姓名',width:100, sortable:true,dataIndex:'name'},
Think of Ext2.0 (2)Think of Ext2.0 (2)        
...{header:'描述',dataIndex:'descn'}
Think of Ext2.0 (2)    ]);
Think of Ext2.0 (2)

Header        显示名称

dataIndex     ds查找字段

width         字段宽度

sortable       是否允许排序 

Grid中使用数据源的定义,从grid.jsp中获取数据 

 

Think of Ext2.0 (2)Think of Ext2.0 (2)    var ds = new Ext.data.Store(...{
Think of Ext2.0 (2)Think of Ext2.0 (2)        proxy: 
new Ext.data.HttpProxy(...{url:'grid.jsp'}),
Think of Ext2.0 (2)Think of Ext2.0 (2)        reader: 
new Ext.data.JsonReader(...{
Think of Ext2.0 (2)            totalProperty: 
'totalProperty',
Think of Ext2.0 (2)            root: 
'root'
Think of Ext2.0 (2)        }
, [
Think of Ext2.0 (2)Think of Ext2.0 (2)            
...{name: 'id'},
Think of Ext2.0 (2)Think of Ext2.0 (2)            
...{name: 'name'},
Think of Ext2.0 (2)Think of Ext2.0 (2)            
...{name: 'descn'}
Think of Ext2.0 (2)        ])
Think of Ext2.0 (2)        
Think of Ext2.0 (2)    }
);
Think of Ext2.0 (2)

定义GridPanel创建Grid

Think of Ext2.0 (2)Think of Ext2.0 (2)    var grid = new Ext.grid.GridPanel(...{
Think of Ext2.0 (2)        el: 
'grid',
Think of Ext2.0 (2)        width:
600,
Think of Ext2.0 (2)        ds: ds,
Think of Ext2.0 (2)        cm: cm,
Think of Ext2.0 (2)        
Think of Ext2.0 (2)Think of Ext2.0 (2)        bbar: 
new Ext.PagingToolbar(...{
Think of Ext2.0 (2)        pageSize: 
10,
Think of Ext2.0 (2)        store: ds,
Think of Ext2.0 (2)        displayInfo: 
true,
Think of Ext2.0 (2)        displayMsg: 
'显示第 {0} 条到 {1} 条记录,一共 {2} 条',
Think of Ext2.0 (2)        emptyMsg: 
"你好"
Think of Ext2.0 (2)    }
)
Think of Ext2.0 (2)
Think of Ext2.0 (2)    }
);    
Think of Ext2.0 (2)   
Think of Ext2.0 (2)    grid.render();
Think of Ext2.0 (2)

elgrid.htmlidgriddiv

ds  数据源

cm  grid显示列定义

bbar  bottom toolbal下面的工具栏

       这里使用分页控件   

最后ds导入时候,使用参数进行过滤

ds.load({params:{start:0,limit:10}}); 

扩展一下,将上面的Form放入到grid中来

Grid上添加一个工具栏,通过单击工具栏中Add Something按钮,弹出上面的Form信息

Think of Ext2.0 (2)

修改如下:

Think of Ext2.0 (2)Think of Ext2.0 (2)var grid = new Ext.grid.GridPanel(...{
Think of Ext2.0 (2)        el: 
'grid',
Think of Ext2.0 (2)        width:
600,
Think of Ext2.0 (2)        ds: ds,
Think of Ext2.0 (2)        cm: cm,
Think of Ext2.0 (2)Think of Ext2.0 (2)        tbar:[
...{
Think of Ext2.0 (2)            text:
'Add Something',
Think of Ext2.0 (2)            tooltip:
'Add a new row',
Think of Ext2.0 (2)            iconCls:
'add',
Think of Ext2.0 (2)            handler : onItemClick
Think of Ext2.0 (2)Think of Ext2.0 (2)        }
'-'...{
Think of Ext2.0 (2)            text:
'Options',
Think of Ext2.0 (2)            tooltip:
'Blah blah blah blaht',
Think of Ext2.0 (2)            iconCls:
'option'
Think of Ext2.0 (2)Think of Ext2.0 (2)        }
,'-',...{
Think of Ext2.0 (2)            text:
'Remove Something',
Think of Ext2.0 (2)            tooltip:
'Remove the selected item',
Think of Ext2.0 (2)            iconCls:
'remove'
Think of Ext2.0 (2)        }
],
Think of Ext2.0 (2)Think of Ext2.0 (2)        bbar: 
new Ext.PagingToolbar(...{
Think of Ext2.0 (2)        pageSize: 
10,
Think of Ext2.0 (2)        store: ds,
Think of Ext2.0 (2)        displayInfo: 
true,
Think of Ext2.0 (2)        displayMsg: 
'显示第 {0} 条到 {1} 条记录,一共 {2} 条',
Think of Ext2.0 (2)        emptyMsg: 
"你好"
Think of Ext2.0 (2)    }
)
Think of Ext2.0 (2)
Think of Ext2.0 (2)    }
);    
Think of Ext2.0 (2)

在GridPanel添加在Grid上的Toolbar,Toolbar上添加三个按钮,并为Add Something添加单击事件onItemClick。

单击事件

Think of Ext2.0 (2)var win;
Think of Ext2.0 (2)Think of Ext2.0 (2)    
function onItemClick(item) ...{
Think of Ext2.0 (2)        
//alert(item.text);        
Think of Ext2.0 (2)
            
Think of Ext2.0 (2)Think of Ext2.0 (2)        
if (!win) ...{   
Think of Ext2.0 (2)            
Think of Ext2.0 (2)Think of Ext2.0 (2)        
var ds = new Ext.data.Store(...{
Think of Ext2.0 (2)Think of Ext2.0 (2)        proxy: 
new Ext.data.HttpProxy(...{url:'combo.jsp'}),
Think of Ext2.0 (2)

相关文章: