Ext面向?qū)ο箝_發(fā)實踐代碼
更新時間:2008年11月18日 00:37:01 作者:
這是自己在學習Ext過程中一個寫的一個示例程序,僅為練習,功能并不全,現(xiàn)將其記錄在我的博客中,希望可以和學習Ext的朋友一起分享
附上完整的HTML代碼和JavaScript代碼文件。
Grid.html
復制代碼 代碼如下:
<html>
<head>
<title>Ext Grid</title>
<link rel="stylesheet" type="text/css" href="http://localhost:8080/ext-2.2/resources/css/ext-all.css"/>
<script type="text/javascript" src="http://localhost:8080/ext-2.2/adapter/ext/ext-base.js"></script>
<script type="text/javascript" src="http://localhost:8080/ext-2.2/ext-all.js"></script>
<script type="text/javascript" src="PersonListGridPanel.js"></script>
<script type="text/javascript">
Ext.QuickTips.init();
Ext.form.Field.prototype.msgTarget = "side";
Ext.BLANK_IMAGE_URL = "http://localhost:8080/ext-2.2/resources/images/default/s.gif";
Ext.apply(Ext.form.VTypes, {
"age": function(_v) {
if (/^\d+$/.test(_v)) {
var _age = parseInt(_v);
if ((_age > 0) && (_age < 200)) return true;
}
return false;
},
"ageText": "年齡必須在0到200之間",
"ageMask": /[0-9]/i
});
Ext.onReady(function() {
new PersonListGridPanel();
});
</script>
</head>
<body>
</body>
</html>
PersonListGridPanel.js
復制代碼 代碼如下:
//定義數(shù)據(jù)列表面板類
PersonListGridPanel = Ext.extend(Ext.grid.GridPanel, {
insertWin: null,
updateWin: null,
constructor: function() {
//添加自定義事件
this.addEvents("rowSelect");
this.insertWin = new InsertPersonInfoWindow();
this.insertWin.on("submit", this.onInsertWinSubmit, this);
this.updateWin = new UpdatePersonInfoWindow();
this.updateWin.on("submit", this.onUpdateWinSubmit, this);
PersonListGridPanel.superclass.constructor.call(this, {
renderTo: Ext.getBody(),
width: 360,
height: 300,
frame:true,
sm: new Ext.grid.RowSelectionModel({
singleSelect:true,
listeners: {
"rowselect": {
fn: function(sm, rowIndex, r) {
this.fireEvent("rowSelect", r); //觸發(fā)自定義事件
},
scope: this
}
}
}),
store: new Ext.data.JsonStore({
data: [{name: "李宗盛", age: 28, sex: "男"}, {name: "林憶蓮", age: 26, sex: "女"}],
fields: ["name", "sex", "age"]
}),
draggable: false,
enableColumnMove: false,
title: "First Grid",
//iconCls:'icon-grid',
colModel: new Ext.grid.ColumnModel([
{header: "Staff Name", width: 100, menuDisabled: true},
{header: "Age", width: 100, sortable: true, dataIndex: "age", align: "right", tooltip: "這里是提示信息"},
{header: "Sex", width: 100, sortable: true, dataIndex: "sex", align: "center"}
]),
tbar: [{
name: "btnFirst",
//text: "First",
iconCls: "x-tbar-page-first",
handler: function () {
this.getSelectionModel().selectFirstRow();
},
scope: this
}, {
name: "btnPrev",
//text: "Prev",
iconCls: "x-tbar-page-prev",
handler: function () {
this.getSelectionModel().selectPrevious();
},
scope: this
}, {
name: "btnNext",
//text: "Next",
iconCls: "x-tbar-page-next",
handler: function () {
this.getSelectionModel().selectNext();
},
scope: this
}, {
name: "btnLast",
//text: "Last",
iconCls: "x-tbar-page-last",
handler: function () {
this.getSelectionModel().selectLastRow();
},
scope: this
}, "-", {
text: "添加",
handler: function() {
//***************************************************
//如果沒有重寫InsertPersonInfoWindow的Close方法
//在調(diào)用之前需要檢查其實例insertWin是否被釋放
//使用示例:
//if (!this.insertWin) {
// this.insertWin = new InsertPersonInfoWindow();
//}
//this.insertWin.show();
//***************************************************
this.insertWin.show();
},
scope: this
}, "-", {
text: "修改",
handler: function() {
var r = this.getActiveRecord();
if (!r) return;
//如何將數(shù)據(jù)填充到窗體中?
this.updateWin.show();
this.updateWin.load(r);
},
scope: this
}, "-", {
text: "刪除",
handler: function() {
var r = this.getActiveRecord();
if (!r) return;
Ext.MessageBox.confirm("刪除", "刪除當前人員信息?", function(btn) {
if(btn == "yes") {
this.delRecord(r);
}
}, this);
},
scope: this
}]
});
},
getActiveRecord: function() {
var sm = this.getSelectionModel();
//沒有選中的記錄時,是拋出異常還是返回null???????
return (sm.getCount() === 0) ? null : sm.getSelected();
},
insert: function(r) {
this.getStore().add(r);
},
delRecord: function(r) {
this.getStore().remove(r);
},
onInsertWinSubmit: function(win, r) {
this.insert(r);
},
onUpdateWinSubmit: function(win, r) {
alert('onUpdateWinSubmit');
}
});
//==============================================================================
//定義數(shù)據(jù)維護面板,在后面定義的新增和修改窗體中都會使用到該面板
PersonInfoFormPanel = Ext.extend(Ext.form.FormPanel, {
constructor: function() {
PersonInfoFormPanel.superclass.constructor.call(this, {
//title: "Person Info",
frame: true,
width: 360,
labelWidth: 40,
defaultType: "textfield",
defaults: { anchor: "92%" },
items: [{
name: "name", //注意,這里使用name屬性而不是id,因為PersonInfoFormPanel會被添加和插入兩個窗體使用,使用id會有沖突,導致組件不能被正確顯示
fieldLabel: "Name",
allowBlank: false,
emptyText: "請輸入姓名",
blankText: "姓名不能為空"
}, {
name: "age",
fieldLabel: "Age",
vtype: "age"
}, {
hiddenName: "sex",
xtype: "combo",
fieldLabel: "Sex",
store: new Ext.data.SimpleStore({
fields: [
{name: 'Sex'}
],
data:[["男"], ["女"]]
}),
mode: 'local',
displayField:'Sex',
triggerAction: 'all',
emptyText:'選擇性別...'
}]
})
},
getValues: function() {
if (this.getForm().isValid()) {
return new Ext.data.Record(this.getForm().getValues());
}
else {
throw Error("信息不完整");
}
},
setValues: function(r) {
//alert(Ext.util.JSON.encode(r.data));
this.getForm().loadRecord(r);
},
reset: function() {
this.getForm().reset();
}
});
//==============================================================================
//新增,修改窗體基類
PersonInfoWindow = Ext.extend(Ext.Window, {
form: null,
constructor: function() {
this.addEvents("submit");
this.form = new PersonInfoFormPanel();
//Ext.apply(this.form, {baseCls: "x-plain"});
PersonInfoWindow.superclass.constructor.call(this, {
plain: true,
width: 360,
modal: true, //模式窗體
onEsc: Ext.emptyFn,
closeAction: "hide",
items: [this.form],
buttons: [{
text: "確 定",
handler: this.onSubmitClick,
scope: this
}, {
text: "取 消",
handler: this.onCancelClick,
scope: this
}]
});
//alert(this.onSubmitClick);
},
close: function() {
//需要重寫CLose方法,
//否則在窗體被關(guān)閉其實體會被釋放
this.hide();
this.form.reset();
},
onSubmitClick: function() {
//alert(Ext.util.JSON.encode(this.form.getValues().data));
try {
this.fireEvent("submit", this, this.form.getValues());
this.close();
}
catch(_err) {
return;
}
},
onCancelClick: function() {
this.close();
}
});
//==============================================================================
//定義新增數(shù)據(jù)窗體
InsertPersonInfoWindow = Ext.extend(PersonInfoWindow, {
title: "添加"
});
//==============================================================================
//定義編輯數(shù)據(jù)窗體
UpdatePersonInfoWindow = Ext.extend(PersonInfoWindow, {
title: "修改",
load: function(r) {
this.form.setValues(r);
}
});
相關(guān)文章
Javascript YUI 讀碼日記之 YAHOO.util.Dom - Part.2 0
繼續(xù)在 YAHOO.util.Dom 中徘徊。由于 YAHOO.util.Dom 多次調(diào)用 batch 方法,所以先看看這個函數(shù)是怎么寫的。有關(guān) batch 的用法,可以參見這里,相關(guān)的代碼如下2008-03-03解決extjs在firefox中關(guān)閉窗口再打開后iframe中js函數(shù)訪問不到的問題
最近做ext時遇到一個問題,在firefox中瀏覽ext應用,加載后第一次打開一個嵌入iframe的Window時,可以直接通過js代碼來執(zhí)行 iframe中的js函數(shù),但是如果將窗口關(guān)閉后重新再打開,將會拋出異常,說是funcName is not a function2008-11-11javascript YUI 讀碼日記之 YAHOO.util.Dom - Part.4
YAHOO.util.Dom 中的 getXY 函數(shù)讓開發(fā)者充分體驗到不同瀏覽器的 Hack 的樂趣。IE8 即將破殼而出,但愿下面的函數(shù)不會又多個 if 判斷。getXY 函數(shù)使用 匿名函數(shù)執(zhí)行返回 函數(shù)(聽起來有點拗口,可以參考 圓心 Blog 上的一篇文章)。2008-03-03學習YUI.Ext第七日-View&JSONView Part Two-一個畫室網(wǎng)站的案例
這篇文章主要介紹了學習YUI.Ext第七日-View&JSONView Part Two-一個畫室網(wǎng)站的案例2007-03-03使用EXT實現(xiàn)無刷新動態(tài)調(diào)用股票信息
最近開始對ExtJS感興趣了,今天正好有空,花了點時間,寫了個基于Ext的例子。2008-11-11