ExtJS GTGrid simple user management

Source: Internet
Author: User
Tags fam

 




Some source code:
Copy codeThe Code is as follows:
<% @ Page language = "java" pageEncoding = "GBK" %>
<% @ Taglib uri = "http://java.sun.com/jsp/jstl/core" prefix = "c" %>
<C: set var = "ctx" value = "$ {pageContext. request. contextPath}"/>
<Html>
<Head>
<Title> User Management homepage </title>
<Link rel = "stylesheet" type = "text/css" href = "js/ext-2.2/resources/css/ext-all.css"/>
<Script type = "text/javascript" src = "js/ext-2.2/adapter/ext/ext-base.js"> </script>
<Script type = "text/javascript" src = "js/ext-2.2/ext-all.js"> </script>
<Style type = "text/css">
. X-panel-body p {}{
Margin: 5px;
}
. X-column-layout-ct. x-panel {}{
Margin-bottom: 5px;
}
. X-column-layout-ct. x-panel-dd-spacer {}{
Margin-bottom: 5px;
}
. Settings {}{
Background-image: url (js/ext-2.2/shared/icons/fam/folder_wrench.png )! Important;
}
. Nav {}{
Background-image: url (js/ext-2.2/shared/icons/fam/folder_go.png )! Important;
}

. Icon-grid {}{
Background-image: url (JavaScript/ext-2.2/shared/icons/fam/grid.png )! Important;
}

# Button-grid. x-panel-body {}{
Border: 1px solid #99bbe8;
Border-top: 0 none;
}

. Add {}{
Background-image: url (js/ext-2.2/shared/icons/fam/add.gif )! Important;
}

. Option {}{
Background-image: url (js/ext-2.2/shared/icons/fam/plugin.gif )! Important

}

. Remove {}{
Background-image: url (JavaScript/ext-2.2/shared/icons/fam/delete.gif )! Important

}

. Save {}{
Background-image: url (js/ext-2.2/shared/icons/save.gif )! Important;
}
</Style>
<Script>
<! --
Ext. onReady (function ()
{
/** // ** Grid related **/
Ext. QuickTips. init ();
Ext. form. Field. prototype. msgTarget = 'day ';
Var newFormWin; // form window container
Var form1; // Add the user's form
Var form2; // modify the user's form

// Record the sidebar status
Ext. state. SessionProvider = Ext. extend (Ext. state. CookieProvider ,{
ReadCookies: function (){
If (this. state ){
For (var k in this. state ){
If (typeof this. state [k] = 'string '){
This. state [k] = this. decodeValue (this. state [k]);
}
}
}
Return Ext. apply (this. state | |{}, Ext. state. SessionProvider. superclass. readCookies. call (this ));
}
});

Var xg = Ext. grid;

Var jsonReader = new Ext. data. JsonReader ({
Root: 'LIST', // returned data set
TotalProperty: 'totalcount', // total number of records
SuccessProperty: '@ success' // marked successfully
},[{
Name: 'id', // dataIndex in grid
Mapping: 'id', // field name in the returned data
Type: 'int' // type. The default value is string.
},{
Name: 'username ',
Mapping: 'username'
},{
Name: 'age ',
Mapping: 'age ',
Type: 'int'
},
{
Name: 'ramark ',
Mapping: 'remark'
}]);

// Store
Var ds = new Ext. data. Store ({
Proxy: new Ext. data. HttpProxy ({
Url: '$ {ctx}/UserServlet? Method = getall'
}),
Reader: jsonReader
});
Ds. setDefaultSort ('id', 'asc ');


/**//**
* ** CRUD Grid
****/

// Select a custom checkbox Column
Var sm = new xg. CheckboxSelectionModel ({
Listeners: // Add a listener
{
// Row selection event
Rowselect: function (sm, rowIndex, keep, rec) // row selection event
{
// Obtain the ext component using Ext. getCmp ('id ')
Var btn = Ext. getCmp ('tbar1 ');
// If the selected quantity is greater than 2, disable the modify button.
If (sm. getCount ()! = 1)
{
Btn. disable ();
}
Else
{
Btn. enable ();
}
},
// Cancel the selection event
Rowdeselect: function (sm, rowIndex, keep, rec) // row selection event
{
// Obtain the ext component using Ext. getCmp ('id ')
Var btn = Ext. getCmp ('tbar1 ');
// The number is equal to 1. Start the modify button.
If (sm. getCount () = 1)
{
Btn. enable ();
}
Else
{
Btn. disable ();
}
}
}


});

// Initialize the Grid
Var grid = new xg. GridPanel ({
Id: 'user-grid ',
Width: 780,
Height: 450,
Frame: true,
Title: 'simple user management ',
IconCls: 'icon-grid ',
Hidden: true,
Store: ds, // Data Warehouse
RenderTo: document. body,
// Column
Cm: new xg. ColumnModel ([
Sm,
{Id: 'id', header: "Index", width: 40, sortable: true, dataIndex: 'id '},
{Header: "username", width: 20, sortable: true, dataIndex: 'username '},
{Header: "age", width: 20, sortable: true, dataIndex: 'age '},
{Header: "Remarks", width: 20, sortable: true, dataIndex: 'remark '}
]),

Sm: sm,

ViewConfig :{
ForceFit: true
},
// Pagination Toolbar
Bbar: new Ext. PagingToolbar ({
PageSize: 10,
Store: ds,
DisplayInfo: true,
DisplayMsg: 'Show {0}-{1} items/total {2} items ',
EmptyMsg: "No data. "
}),

// Upload embedded toolbar (button)
Tbar :[{
Text: 'add ',
Tooltip: 'Add a row of New data ',
IconCls: 'add ',
Handler: function ()
{
Add ();
}
},'-',{
Text: 'modify ',
Tooltip: 'modify A data ',
IconCls: 'option ',
Id: 'tbar1 ',
Handler: function ()
{
Modify ();
}
},'-',{
Text: 'delete ',
Tooltip: 'delete data ',
IconCls: 'delete ',
Handler: function ()
{
Remove ();
}
}]

});

// Load data
Ds. load ({params: {start: 0, limit: 10 }});
// Render the Grid
Grid. render ();

// Add a user's Function
Var add = function ()
{
NewFormWin = new Ext. Window ({
Layout: 'fit ',
Width: 400,
Height: 300,
CloseAction: 'hide ',
Plain: true,
Title: 'user management ',
Items: form1

});
NewFormWin. show ();
}

// Modify the user's Function
Var modify = function ()
{
Var _ record = grid. getSelectionModel (). getSelected ();
If (! _ Record)
{
Ext. Msg. alert ('select the item to be modified! ');
}
Else
{
MyFormWin ();
Form2.form. load
({
Url: '$ {ctx}/UserServlet? Method = getById & id = '+ _ record. get ('id '),
WaitMsg: 'loading Data ',

Failure: function (){
Ext. Msg. alert ('loading failed ');
},
Success: function (){
// Ext. Msg. alert ('Load successful! ');
}
});
}
}
// Modify the user's form
Var myFormWin = function (){
NewFormWin = new Ext. Window ({
Layout: 'fit ',
Width: 400,
Height: 300,
CloseAction: 'hide ',
Plain: true,
Title: 'modify user ',
Items: form2
});
NewFormWin. show ('');
}

/** // * Note that JsonReader should be placed on the Form, and the data loading sequence is incorrect */
Var jsonFormReader = new Ext. data. JsonReader ({
Root: 'LIST ',
TotalProperty: 'totalcount ',
SuccessProperty: '@ success'
},[
{
Name: 'id ',
Mapping: 'id ',
Type: 'int ',
},
{
Name: 'username ',
Mapping: 'username'
},{
Name: 'age ',
Mapping: 'age ',
Type: 'int'
},{
Name: 'remark ',
Mapping: 'remark'
}]);

// Add the user's Form
Form1 = new Ext. FormPanel ({
LabelWidth: 75,
Frame: true,
Title: 'add user ',
BodyStyle: 'padding: 5px 5px 0 ',
Width: 350,
WaitMsgTarget: true,
Url: '$ {ctx}/UserServlet? Method = save ',
Defaults:
{
Width: 230
},
DefaultType: 'textfield ',
Items :[
{
FieldLabel: 'username ',
Name: 'username', // used to obtain data in the background
AllowBlank: false,
BlankText: 'user name cannot be blank'
},{
Xtype: 'numberfield ',
MaxValue: 100,
MaxText: 'Age cannot exceed 100 age ',
MinValue: 1,
MinText: 'Age cannot be less than 1 ',
FieldLabel: 'age ',
Name: 'age ',
AllowBlank: false,
BlankText: 'Age cannot be blank'
}, New Ext. form. TextArea ({
FieldLabel: 'note ',
Name: 'remark ',
GrowMin: 234,
MaxLength: 50,
MaxLengthText: 'the maximum length cannot exceed 50 characters! '
})],

Buttons :[{
Text: 'save ',
Disabled: false,
Handler: function ()
{

If (form1.form. isValid ())
{
Form1.form. submit (
{
Url: '$ {ctx}/UserServlet? Method = save ',
Success: function (from, action)
{
Ext. Msg. alert ('user added successfully! ');
Ds. load ({
Params :{
Start: 0,
Limit: 10
}
});
},
Failure: function (form, action ){
Ext. Msg. alert ('failed to add user! ');
},
WaitMsg: 'saving the data. Later'
});
NewFormWin. hide ();
}
Else
{
Ext. Msg. alert ('Check whether your information has been filled in successfully! ');
}
}
},{
Text: 'cancel ',
Handler: function ()
{
Form1.form. reset ();
}
}]
});

// Modify the user's Form
Form2 = new Ext. FormPanel ({
LabelWidth: 75,
Frame: true,
Title: 'modify user ',
BodyStyle: 'padding: 5px 5px 0 ',
Width: 350,
WaitMsgTarget: true,
Url: '$ {ctx}/UserServlet? Method = Update ',
Reader: jsonFormReader, // specify reader for Form.
Defaults:
{
Width: 230
},
DefaultType: 'textfield ',
Items :[
{
Xtype: 'ddy ',
Name: 'id'
},
{
FieldLabel: 'username ',
Name: 'username', // used to obtain data in the background
AllowBlank: false,
BlankText: 'user name cannot be blank'
},{
Xtype: 'numberfield ',
MaxValue: 100,
MaxText: 'Age cannot exceed 100 age ',
MinValue: 1,
MinText: 'Age cannot be less than 1 ',
FieldLabel: 'age ',
Name: 'age ',
AllowBlank: false,
BlankText: 'Age cannot be blank'
}, New Ext. form. TextArea ({
FieldLabel: 'note ',
Name: 'remark ',
GrowMin: 234,
MaxLength: 50,
MaxLengthText: 'the maximum length cannot exceed 50 characters! '
})],

Buttons :[{
Text: 'modify ',
Disabled: false,
Handler: function ()
{

If (form2.form. isValid ())
{
Form2.form. submit (
{
Success: function (from, action)
{
Ext. Msg. alert ('user modified successfully! ');
Ds. load ({
Params :{
Start: 0,
Limit: 10
}
});
},
Failure: function (form, action ){
Ext. Msg. alert ('failed to modify the user! ');
},
WaitMsg: 'saving the data. Later'
});
NewFormWin. hide ();
}
Else
{
Ext. Msg. alert ('Check whether your information has been filled in successfully! ');
}
}
},{
Text: 'cancel ',
Handler: function ()
{
Form2.form. reset ();
}
}]
});

// Delete the event
Var remove = function ()
{

Var _ record = grid. getSelectionModel (). getSelected ();
If (_ record)
{
Ext. MessageBox. confirm ('Confirm delete', 'Are you sure you want to delete the selected data? ', Function (btn)
{
If (btn = "yes "){
Var m = grid. getSelections ();
Var jsonData = "";
For (var I = 0, len = m. length; I <len; I ++)
{
Var ss = m [I]. get ("id"); // user id, "id" field name
If (I = 0 ){
JsonData = jsonData + ss;
} Else {
JsonData = jsonData + "," + ss;
}
// Delete the data source
Ds. remove (m [I]);
// Delete the corresponding database record
Ext. Ajax. request ({
Url: '$ {ctx}/UserServlet? Method = remove & id = '+ ss
});
}

Ds. load ({
Params :{
Start: 0,
Limit: 10,
DelData: jsonData
}
});


}
});
}
Else
{
Ext. Msg. alert ('select the data item to delete! ');
}
};
/**//***/
Ext. state. Manager. setProvider
(New Ext. state. SessionProvider ({state: Ext. appState }));

// Tabs for the center
Var tabs = new Ext. TabPanel ({
Region: 'center ',
Margins: '3 3 3 0 ',
ActiveTab: 0,
Defaults :{
AutoScroll: true
},
Items :[{
Title: 'extjs version ',
ContentEl: 'user-grid' // html id to be filled
},{
Title: 'gtgrid version ',
Html: 'gtgrid does not support integration with Extjs (window) at the moment. <a href = "$ {ctx}/gt. jsp"> click here to show my example </>'
},{
Title: 'More reference ',
Html: 'learn more <a href = "http://www.blogjava.net/supercrsky"> my blog </a>'
}]
});

// Panel for the west
Var nav = new Ext. Panel ({
Title: 'menu directory ',
Region: 'west ',
Split: true,
Width: 200,
Collapsible: true,
Margins: '3 0 3 3 ',
Cmargins: '3 3 3 3 ',
Layout: 'accordion ',
LayoutConfig :{
Animate: true
},
Items :[{
Html: Ext. example. Invalid bogusmarkup,
Title: 'user management ',
AutoScroll: true,
Border: false,
IconCls: 'nav'
},{
Title: 'user configuration ',
Html: Ext. example. shortBogusConfig,
Border: false,
AutoScroll: true,
IconCls: 'settings'
}]
});

Var win = new Ext. Window ({
Title: 'user management microsystem ',
Closable: true,
Maximizable: true,
Minimizable: true,
Width: '200 ',
Height: '20140901 ',
Plain: true,
Layout: 'border ',
Closable: false,
Items: [nav, tabs]
});

Win. show ();
Win. maximize ();

});
-->
</Script>
</Head>
<Body>
<! -- Js used in the sidebar -->
<Script type = "text/javascript" src = "js/ext-2.2/shared/examples. js"> </script>
</Body>
</Html>

Complete source code download click here to download

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.