接續Ext Layout(一)功能,於Tab Panel上再增加一個Grid。
此作法是先建立一個Grid(注意請不要指定renderTo屬性值),再建立一個viewport(Border Layout)後,再於viewport之中間區域之Tab panel上加入先前建立之Grid。作法與Ext Layout(二)功能,於Tab Panel上再增加一個Grid不同。
開發環境:
IDE:MyEclipse6.6GA
Web Server:Tomcat 6.0.20
Database:MySql 5.1.36
MVC:Struts2.1.8
FrameWork:Spring2.0,Hibernate3.2
Client UI:Ext2.3.0
一、Layout頁面(layout3.jsp):
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'combox.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<link rel="stylesheet" type="text/css" href="extjs/resources/css/ext-all.css">
<link rel="stylesheet" type="text/css" href="css/onReady.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" src="extjs/ext-lang-zh_TW.js"></script>
<script type="text/javascript">
<!--
Ext.onReady(function(){
Ext.BLANK_IMAGE_URL='extjs/resources/images/default/s.gif';
/* normal store
var store = new Ext.data.Store({
url: 'SysMgrJson!getMovies.action',
reader: new Ext.data.JsonReader({root:'results',totalProperty:'totals'}, [
'id',
'coverthumb',
'title',
'director',
{name: 'released', type: 'date', dateFormat: 'Y-m-d'},
'genre',
'tagline',
{name: 'price', type: 'float'},
{name: 'active'}
])
});
*/
//Grouping store
var store = new Ext.data.GroupingStore({
url: 'SysMgrJson!getMovies.action',
sortInfo: {
field: 'genre',
direction: "ASC"
},
groupField: 'genre',
reader: new Ext.data.JsonReader({root:'results',totalProperty:'totals'}, [
'id',
'coverthumb',
'title',
'director',
{name: 'released', type: 'date', dateFormat: 'Y-m-d'},
'genre',
'tagline',
{name: 'price', type: 'float'},
{name: 'active'}
])
});
var genres = new Ext.data.Store({
data: [
[1,"Comedy"],
[2,"Drama"],
[3,"Action"],
[4,"Mystery"]
],
reader: new Ext.data.ArrayReader({id:'id'}, [
'id',
'genre'])
});
//Combining two columns function
function title_tagline(val, x, store){
return '<b>'+val+'</b><br>'+store.data.tagline;
}
//Generating HTML and graphics
function cover_image(val){
return '<img src="images/'+val+'.jpg" width="50%" height="50%">';
}
//for edit field of grid
var title_edit = new Ext.form.TextField();
var director_edit = new Ext.form.TextField();
var tagline_edit = new Ext.form.TextField({
maxLength: 45
});
var price_edit = new Ext.form.TextField();
var coverthumb_edit = new Ext.form.TextField();
var released_edit = new Ext.form.DateField();
var genre_edit = new Ext.form.ComboBox({
typeAhead: true,
triggerAction: 'all',
mode: 'local',
store: genres,
displayField:'genre',
valueField: 'genre'
});
//for add row to grid
var ds_model = Ext.data.Record.create([
'id',
'title',
'director',
{name: 'released', type: 'date', dateFormat: 'Y-m-d'},
'genre',
'tagline',
'coverthumb',
{name: 'price', type: 'float'},
{name: 'active'}
]);
// add your data store here
var grid = new Ext.grid.EditorGridPanel({
//renderTo: document.body,
//renderTo:'example-grid',
frame:true,
title: 'Movie Database',
height:300,
width:550,
stripeRows:true,
store: store,
//matching Grouping store
view: new Ext.grid.GroupingView(),
//增加grid event for Row Selection mode
sm: new Ext.grid.RowSelectionModel({
//singleSelect: true,
listeners: {
rowselect: {
fn: function(sm,index,record) {
index++;
Ext.Msg.alert('提示','您選擇了,'+record.data.title+',第 '+index+'筆資料');
}
}
}
}),
//Altering the grid at the click of a button
tbar: [
//modify title column of grid
{
text: '修改 Title',
handler: function(){
var sm = grid.getSelectionModel();
if (sm.hasSelection()){
var sel = sm.getSelected();
Ext.Msg.show({
title: '修改 Title',
prompt: true,
buttons: Ext.MessageBox.OKCANCEL,
value: sel.data.title,
fn: function(btn,text){
if (btn == 'ok'){
sel.set('title', text);
}
}
});
}else{
Ext.Msg.alert('提示','請選擇欲修改Title之列');
}
}
},
//hidden price column of grid
{
text: 'Hide Price',
handler: function(btn){
var cm = grid.getColumnModel();
var pi = cm.getIndexById('price');
//var pi=5;
if (cm.isHidden(pi)){
cm.setHidden(pi,false);
btn.setText('Hide Price');
}else{
cm.setHidden(pi,true);
btn.setText('Show Price');
}
btn.render();
}
},
//add first row to grid
{
text: 'Add Movie(First)',
icon: 'images/RowInsert.png',
cls: 'x-btn-text-icon',
handler: function() {
grid.getStore().insert(0,new ds_model({
id:'',
title:'New Movie',
director:'',
genre:'Comedy',
tagline:'',
active:'1'
})
);
grid.startEditing(0,0);
}
},
//add last row to grid
{
text: 'Add Movie(Last)',
icon: 'images/RowInsert.png',
cls: 'x-btn-text-icon',
handler: function() {
grid.getStore().insert(grid.getStore().getCount(),new ds_model({
id:'',
title:'New Movie',
director:'',
genre:'Comedy',
tagline:'',
active:'1'
})
);
grid.startEditing(grid.getStore().getCount()-1,0);
}
},
//Deleting data from the server
{
text: 'Remove Movie',
icon: 'images/RowDelete.png',
cls: 'x-btn-text-icon',
handler: function() {
var sm = grid.getSelectionModel();
if (sm.hasSelection()){
//var sel = sm.getSelected();
var sels = sm.getSelections();
var len = sels.length;
var ids = new Array(len);
for(var i=0;i<len;i++){
ids[i] = sels[i].data.id;
if(i==0){
var msgs = '刪除: '+sels[i].data.title;
}else{
msgs = msgs +',\n'+sels[i].data.title;
}
};
Ext.Msg.show({
title: '刪除電影',
buttons: Ext.MessageBox.YESNOCANCEL,
msg: msgs,
fn: function(btn){
if (btn == 'yes'){
var conn = new Ext.data.Connection();
conn.request({
url: 'SysMgrJson!updateMovies.action',
method:'post',
params: {
action: 'delete',
id: ids
},
success: function(resp,opt) {
//grid.getStore().remove(sel);
grid.getStore().reload();
},
failure: function(resp,opt) {
Ext.Msg.alert('Error',
'Unable to delete movie');
}
});
}
}
});
}else{
Ext.Msg.alert('提示','請選擇欲刪除(多)列之資料');
}
}
},
//Saving data to the server
{
text: 'Save Movie',
icon: 'images/save-icon.png',
cls: 'x-btn-text-icon',
handler: function() {
var sm = grid.getSelectionModel();
if (sm.hasSelection()){
var sels = sm.getSelections();
var len = sels.length;
var values = new Array(len);
for(var i=0;i<len;i++){
values[i] = new Array(9);
values[i][0] = sels[i].data.id;
values[i][1] = sels[i].data.title;
values[i][2] = sels[i].data.director;
values[i][3] = (sels[i].data.released).format('Y-m-d');
values[i][4] = sels[i].data.genre;
values[i][5] = sels[i].data.tagline;
values[i][6] = sels[i].data.coverthumb;
values[i][7] = sels[i].data.price;
values[i][8] = sels[i].data.active;
if(i==0){
var msgs = '儲存: '+sels[i].data.title;
}else{
msgs = msgs +',\n'+sels[i].data.title;
}
};
Ext.Msg.show({
title: '儲存電影',
buttons: Ext.MessageBox.YESNOCANCEL,
msg: msgs,
fn: function(btn){
if (btn == 'yes'){
var conn = new Ext.data.Connection();
conn.request({
url: 'SysMgrJson!updateMovies.action',
method:'post',
params: {
action: 'update',
values: values
},
success: function(resp,opt) {
//grid.getStore().remove(sel);
grid.getStore().reload();
},
failure: function(resp,opt) {
Ext.Msg.alert('Error',
'Unable to save movie');
}
});
}
}
});
}else{
Ext.Msg.alert('提示','請選擇欲儲存(多)列之資料');
}
}
}
],
bbar: new Ext.PagingToolbar({
pageSize: 5,
store: store,
displayInfo: true,
displayMsg: '第 {0} 筆 - {1} 筆 of {2} 筆',
emptyMsg: "沒有資料"
}),
//Defining a Grids column model and want to display column
columns: [
{header: "Cover", dataIndex: 'coverthumb',renderer:cover_image},
{header: "CoverThumb", dataIndex: 'coverthumb',editor:coverthumb_edit},
//Combining two columns
{header: "Title", dataIndex: 'title',renderer:title_tagline,editor:title_edit},
{header: "Director", dataIndex: 'director',editor:director_edit},
//Client-side sorting
{header: "Released", dataIndex: 'released',sortable:true,
renderer: Ext.util.Format.dateRenderer('m/d/Y'),editor:released_edit},
{header: "Genre", dataIndex: 'genre',editor:genre_edit},
{header: "Price",dataIndex: 'price',id:'price',editor:price_edit},
//setup hidden column
{header: "Tagline", dataIndex: 'tagline',hidden:true,editor:tagline_edit}
]
});
var viewport = new Ext.Viewport({
layout: 'border',
renderTo: Ext.getBody(),
items: [{region: 'north',
xtype: 'panel',
html: 'North'},
{region: 'west',
xtype: 'panel',
split: true,
//open/close panel
collapsible: true,
collapseMode: 'mini',
title: 'Some Info',
bodyStyle:'padding:5px;',
width: 200,
minSize: 200,
html: 'West'},
{region: 'center',
//Adding a tab panel
xtype: 'tabpanel',
activeTab: 0,
items: [grid,{
title: 'Movie Grid'},
{title: 'Movie Descriptions',
html: 'Movie Info'},
{title: 'Movie Grid2',
html:'Center2'}]
},
{region: 'east',
xtype: 'panel',
split: true,
title: 'genre information form',
//open/close panel
collapsible: true,
width: 200,
html: 'East'
},
{region: 'south',
xtype: 'panel',
html: 'South'}]
});
//需在gridPanel建立後才可以Load資料
store.load({params:{start:0, limit:5}});
});
//-->
</script>
</head>
<body>
</body>
</html>
二、測試結果:
1.http://localhost:8080/ExtStudy/Basic/layout3.jsp
沒有留言:
張貼留言