Ext object-oriented development practice code_YUI.Ext related
This is a sample program I wrote one by one while learning Ext. It is just for practice and does not have full functions. I will record it in my blog now and hope to share it with my friends who are learning Ext
Brief description of the sample program:
This Demo is to demonstrate how to use GridPanel to display data, add toolbar buttons to GridPanel, and provide pop-up forms to add data.
Ext components used
This demo involves the three components of GridPanel, FormPanel and Window in Ext.
Rendering
Now let’s start explaining the code, first look at the code snippet to create GridPanel
//Define the data list panel class
PersonListGridPanel = Ext.extend(Ext.grid.GridPanel, {
insertWin: null,
updateWin: null,
constructor: function() {
//Add custom event
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); //Trigger custom event
} ,
scope: this
}
}
}),
store: new Ext.data.JsonStore({
data: [{name: "李宗胜", age: 28 , sex: "male"}, {name: "林伊伦", age: 26, sex: "female"}],
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: "Here is the prompt information"},
{header: "Sex", width: 100, sortable: true, dataIndex: "sex", align: "center"}
]),
tbar: [{
text: "Add person",
handler: function() {
//********************** *******************************
//If the Close method of InsertPersonInfoWindow is not overridden
//Before calling Need to check whether its instance insertWin is released
//Usage example:
//if (!this.insertWin) {
// this.insertWin = new InsertPersonInfoWindow();
//}
//this.insertWin.show();
//********************************** ******************
this.insertWin.show();
},
scope: this
}, "-", {
text: "Modifier",
handler: function() {
var r = this.getActiveRecord();
if (!r) return;
//definitely The Show method must be called first, and then the Load method,
//Otherwise the data will not be presented
this.updateWin.show();
this.updateWin.load(r);
},
scope: this
}, "-", {
text: "Delete Person",
handler: function() {
var r = this.getActiveRecord();
if (!r) return;
Ext.MessageBox.confirm("Delete", "Delete current personnel information? ", function(btn) {
if(btn == "yes") {
this.delRecord(r);
}
}, this);
},
scope: this
}]
});
},
getActiveRecord: function() {
var sm = this.getSelectionModel();
//When there is no selected record , does it throw an exception or return 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') ;
}
});
Data maintenance panel code
//Define the data maintenance panel, which will be used in the new and modified forms defined later
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", //Note that the name attribute is used here instead of id, because PersonInfoFormPanel will be used by adding and inserting two forms. There will be a conflict in using the id, resulting in the component not being displayed correctly.
fieldLabel: "Name",
allowBlank: false,
emptyText: "Please enter your name" ,
blankText: "Name cannot be empty"
}, {
name: "age",
fieldLabel: "Age",
vtype: "age"
}, {
hiddenName: "sex",
xtype: "combo",
fieldLabel: "Sex",
store: new Ext.data.SimpleStore({
fields: [
{name: 'Sex'}
],
data:[["male"], ["female"]]
}),
mode: 'local',
displayField:'Sex',
triggerAction: 'all',
emptyText:'Select gender...'
}]
})
},
getValues: function() {
if (this.getForm().isValid()) {
return new Ext.data.Record(this.getForm().getValues());
}
else {
throw Error("Error Message");
}
},
setValues: function(r) {
this.getForm().loadRecord(r);
},
reset: function() {
this.getForm().reset();
}
});
maintenance of data From a design perspective, the two actions of adding and updating require writing two forms to operate them. Careful friends will definitely think that the new and updated actions are for the same data table, so can you just write one form and then reuse it? The answer is yes. Next we will first write a form base class.
//Newly added, modify the form base class
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, //Modal form
onEsc: Ext. emptyFn,
closeAction: "hide",
items: [this.form],
buttons: [{
text: "OK",
handler: this.onSubmitClick,
scope: this
}, {
text: "Cancel",
handler: this.onCancelClick,
scope: this
}]
});
//alert (this.onSubmitClick);
},
close: function() {
//Need to override the CLose method,
//Otherwise the entity will be released when the form is closed
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();
}
});
After the base class is written, we can use the inherited method to write new and updated forms.
//Define the new data form
InsertPersonInfoWindow = Ext.extend(PersonInfoWindow, {
title: "Add"
});
//============================================== ================================
//Define the edit data form
UpdatePersonInfoWindow = Ext.extend(PersonInfoWindow, {
title: "Modify",
load: function(r) {
this.form.setValues(r);
}
}) ;
In order to distinguish between new and updated forms, we specify the Title attribute for them in their respective implementation classes. In addition, we need to add another method for loading the data to be edited in the updated form class. Load.
The script part is complete, let’s see how to use it in HTML. Reference code in HTML
The code is very concise and clear. You only need to create a PersonListGridPanel, because it contains new and modified form objects, and the new and modified forms use the PersonInfoFormPanel responsible for data editing.
VTypes are used in PersonInfoFormPanel for data validation.
New and modified forms are just interfaces, responsible for transferring the data filled in by the user in the PersonInfoFormPanel back to the ListGrid for saving, or transferring the data in the ListGrid to the PersonInfoFormPanel for presentation for the user to edit.
Attach the complete HTML code and JavaScript code files.
Grid.html
PersonListGridPanel.js
//Define the data list panel class
PersonListGridPanel = Ext.extend(Ext.grid.GridPanel, {
insertWin: null,
updateWin: null,
constructor: function( ) {
//Add custom events
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); //Trigger custom events
},
scope: this
}
}
}),
store: new Ext.data.JsonStore({
data: [{name: "李宗胜", age: 28, sex: "male"}, {name: "林伊伦", age: 26, sex: "female"}],
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: "Here is the prompt information"},
{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: "Add",
handler: function() {
//******** ******************************************
//If there is no heavy Write the Close method of InsertPersonInfoWindow
//Before calling, you need to check whether its instance insertWin has been released
//Usage example:
//if (!this.insertWin) {
// this.insertWin = new InsertPersonInfoWindow();
//}
//this.insertWin.show();
//******************** *****************************
this.insertWin.show();
},
scope: this
}, "-", {
text: "Modify",
handler: function() {
var r = this.getActiveRecord();
if (!r ) return;
//How to fill data into the form?
this.updateWin.show();
this.updateWin.load(r);
} ,
scope: this
}, "-", {
text: "Delete",
handler: function() {
var r = this.getActiveRecord();
if (!r) return;
Ext.MessageBox.confirm("Delete", "Delete current personnel information? ", function(btn) {
if(btn == "yes") {
this.delRecord(r);
}
}, this);
},
scope: this
}]
});
},
getActiveRecord: function() {
var sm = this.getSelectionModel();
//When there is no selected record , does it throw an exception or return 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') ;
}
});
//================================ ===============================================
//Define the data maintenance panel, which will be used in the new and modified forms defined later
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", //Note that the name attribute is used here instead of id, because PersonInfoFormPanel will be used by adding and inserting two forms. Using id will conflict, causing the component to not be displayed correctly
fieldLabel: "Name ",
allowBlank: false,
emptyText: "Please enter your name",
blankText: "The name cannot be empty"
}, {
name: "age",
fieldLabel : "Age",
vtype: "age"
}, {
hiddenName: "sex",
xtype: "combo",
fieldLabel: "Sex",
store: new Ext.data.SimpleStore({
fields: [
{name: 'Sex'}
],
data:[["male"], ["female"] ]
}),
mode: 'local',
displayField:'Sex',
triggerAction: 'all',
emptyText: 'Select gender...'
} ]
})
},
getValues: function() {
if (this.getForm().isValid()) {
return new Ext.data.Record(this.getForm ().getValues());
}
else {
throw Error("Incomplete information");
}
},
setValues: function(r) {
//alert(Ext.util.JSON.encode(r.data));
this.getForm().loadRecord(r);
},
reset: function() {
this.getForm().reset();
}
});
//====================== ================================================== =======
//Add and modify the form base class
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, //modal form
onEsc: Ext.emptyFn,
closeAction: "hide",
items: [this.form],
buttons: [{
text: "OK",
handler: this.onSubmitClick,
scope: this
}, {
text: "Cancel",
handler: this.onCancelClick,
scope: this
}]
}) ;
//alert(this.onSubmitClick);
},
close: function() {
//Need to override the CLose method,
//Otherwise the form will be closed The realization is released
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();
}
});
//======================================== ========================================
//Define new Add data form
InsertPersonInfoWindow = Ext.extend(PersonInfoWindow, {
title: "Add"
});
//============ ================================================== ================
//Define the edit data form
UpdatePersonInfoWindow = Ext.extend(PersonInfoWindow, {
title: "Modify",
load: function(r) {
this.form.setValues(r);
}
});

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



How to use Go language to implement object-oriented event-driven programming Introduction: The object-oriented programming paradigm is widely used in software development, and event-driven programming is a common programming model that realizes the program flow through the triggering and processing of events. control. This article will introduce how to implement object-oriented event-driven programming using Go language and provide code examples. 1. The concept of event-driven programming Event-driven programming is a programming model based on events and messages, which transfers the flow control of the program to the triggering and processing of events. in event driven

The Linuxext2 file system is a file system used on most Linux operating systems. It uses an efficient disk storage structure to manage the storage of files and directories. Before we delve into the physical storage structure of the Linuxext2 file system, we first need to understand some basic concepts. In the ext2 file system, data is stored in data blocks (blocks), which are the smallest allocable units in the file system. Each data block has a fixed size, usually 1KB, 2KB or 4

The @JsonIdentityInfo annotation is used when an object has a parent-child relationship in the Jackson library. The @JsonIdentityInfo annotation is used to indicate object identity during serialization and deserialization. ObjectIdGenerators.PropertyGenerator is an abstract placeholder class used to represent situations where the object identifier to be used comes from a POJO property. Syntax@Target(value={ANNOTATION_TYPE,TYPE,FIELD,METHOD,PARAMETER})@Retention(value=RUNTIME)public

Go language supports object-oriented programming through type definition and method association. It does not support traditional inheritance, but is implemented through composition. Interfaces provide consistency between types and allow abstract methods to be defined. Practical cases show how to use OOP to manage customer information, including creating, obtaining, updating and deleting customer operations.

Analyzing the Flyweight Pattern in PHP Object-Oriented Programming In object-oriented programming, design pattern is a commonly used software design method, which can improve the readability, maintainability and scalability of the code. Flyweight pattern is one of the design patterns that reduces memory overhead by sharing objects. This article will explore how to use flyweight mode in PHP to improve program performance. What is flyweight mode? Flyweight pattern is a structural design pattern whose purpose is to share the same object between different objects.

The Go language supports object-oriented programming, defining objects through structs, defining methods using pointer receivers, and implementing polymorphism through interfaces. The object-oriented features provide code reuse, maintainability and encapsulation in the Go language, but there are also limitations such as the lack of traditional concepts of classes and inheritance and method signature casts.

OOP best practices in PHP include naming conventions, interfaces and abstract classes, inheritance and polymorphism, and dependency injection. Practical cases include: using warehouse mode to manage data and using strategy mode to implement sorting.

There is no concept of a class in the traditional sense in Golang (Go language), but it provides a data type called a structure, through which object-oriented features similar to classes can be achieved. In this article, we'll explain how to use structures to implement object-oriented features and provide concrete code examples. Definition and use of structures First, let's take a look at the definition and use of structures. In Golang, structures can be defined through the type keyword and then used where needed. Structures can contain attributes
