<div class="codetitle"> <span><a style="CURSOR: pointer" data="15734" class="copybut" id="copybut15734" onclick="doCopy('code15734')"><u>复制代码</u></a></span> 代码如下:</div> <div class="codebody" id="code15734"> <br> <br><br> <br> <br> <title>extJs中常用到的增删改查操作的示例代码</title> <br> <!-- CommonJs.jsp 为 ExtJS 引入的路径 --> <br> <br> <script type="text/javascript"><!-- <BR> /** <BR> * 作 者: 花 慧 <BR> * 时 间: 2009年12月22日 <BR> * 内 容: extJs中常用到的增,删,改,查操作 <BR> */ <br><br> //设置每页显示的行数默认为10 <br><br> var QUERY_PAGE_SIZE = 10; <br><br> /** <BR> * SearchQueryForm():绘制查询表单 <BR> */ <BR> function searchQueryForm() <BR> { <BR> //form 使用的时候建议设为局部变量,可以通过Ext.getCmp(“”)去获取form <br><br> var queryForm = null; <BR> queryForm = new Ext.FormPanel({ <BR> id:'queryForm', //指定queryForm的Id <BR> renderTo:'searchPanel', //指向form所在的div层 <BR> labelWidth:70, //label标签的width占页面的百分比 <BR> region:'north', <BR> border:false, //以显示面板的body元素的边界,虚假隐藏起来(默认为true) <BR> badyBorder:false, //显示面板的body元素,假以隐藏它(默认为true的内部边界) <BR> labelAlign:'right', //label标签的对齐方式 <BR> frame:true, //自定义面板的圆形边界,边界宽度1px。默认为false <BR> title:'用户信息查询', //form的标题 <BR> /** <BR> *buttons:在FormPanel中按钮的集合 <BR> */ <BR> buttons:[{handler:addForm,text:'新增'}, <BR> {handler:submitForm,text:'查询'}, <BR> {handler:resetForm,text:'重置'}], <BR> /** <BR> * items: 在FormPanel中不可缺少的部分 <BR> */ <BR> items:[{ <BR> /** <BR> * layout:extJs容器组件,可以设置它的显示风格 <BR> * 它的有效值有absolute,accordion,anchor,border,card,fit,form and table 共9种 <BR> */ <BR> layout:'column', <BR> items:[ <BR> { <BR> columnWidth:.5, <BR> layout:'form', <BR> items:{ <BR> name:'userId', <BR> hiddenName:'userId', <BR> xtype:'textfield', <BR> fieldLabel:'用户编码', <BR> maxLength:'50', <BR> vtype:'specialChar', <BR> anchor:'80%' <BR> } <BR> },{ <BR> columnWidth:.5, <BR> layout:'form', <BR> items:{ <BR> name:'userName', <BR> hiddenName:'userName', <BR> xtype:'textfield', <BR> fieldLabel:'用户名称', <BR> maxLength:'100', <BR> vtype:'specialChar', <BR> anchor:'80%' <BR> } <BR> } <BR> ] <BR> }] <BR> }); <BR> } <BR> /** <BR> * showUserForm():绘制添加表单 <BR> */ <BR> function showUserForm() <BR> { <BR> //将变量定义成局部变量,避免每次都生成一个新对象 <br><br> var userForm = null; <BR> userForm = new Ext.FormPanel({ <BR> id:'conditionForm', <BR> labelWidth:'80', <BR> labelAlign:'right', <BR> border:false, <BR> bodyBorder:false, <BR> frame:true, <BR> items:[ <BR> layout:'column', <BR> items:[ <BR> { <BR> columnWidth:'.8', <BR> items:{ <BR> name:'userInfo.userId', <BR> hiddenName:'userInfo.userId', //hiddenName动态的绑定数据库中对应的字段 <BR> xtype:'textField', //xtype可以分为三类,textField为表单域的控件 <BR> fieldLabel:'用户编码<font color=red>*',//控件前的文本说明 <BR> labelSeparator:'', <BR> blankText : '填写用户编码', //为空的文本框提示信息 <BR> allowBlank:false, //不允许为空 <BR> maxLength:'50', //文本框允许输入的最大的长度,最小的minLength <BR> vtype:'specialChar', <BR> anchor:'80%' <BR> } <BR> },{ <BR> columnWidth:'.8', <BR> items:{ <BR> name:'userInfo.userName', <BR> hiddenName:'userInfo.userName', <BR> xtype:'textField', <BR> fieldLabel:'用户姓名<font color=red>*', <BR> labelSeparator:'', <BR> blankText:'填写用户姓名', <BR> allowBlank:false, <BR> maxLength:'100', <BR> vtype:'specialChar', <BR> anchor:'100%' <BR> } <BR> },{ <BR> columnWidth:'.8', <BR> items:{ <BR> name:'userInfo.pwd', <BR> hiddenName:'userInfo.pwd', <BR> xtype:'textField', <BR> inputType:'password', <BR> fieldLabel:'用户密码<font color=red>*', <BR> labelSeparator:'', <BR> blankText:'填写用户密码', <BR> allowBlank:false, <BR> maxLength:'12', <BR> minLength:'6', <BR> value:'123456', //用户默认的秘密 <BR> anchor:'100%' <BR> } <BR> },{ <BR> columnWidth:'.8', <BR> items:{ <BR> name:'rPwd', <BR> hiddenName:'rPwd', <BR> xtype:'textField', <BR> inputType:'password', <BR> fieldLabel:'确认密码<font color=red>*', <BR> labelSeparator:'', <BR> blankText:'二次输入的秘密要相同', <BR> allowBlank:false, <BR> vtype:'pwdRange', <BR> pwdRange:{begin:'userInfo.pwd',end:'rPwd'}, <BR> maxLength:'12', <BR> anchor:'100%' <BR> } <BR> }] <BR> ] <BR> }); <BR> } <BR> /** <BR> * editUserForm():绘制修改表单 <BR> */ <BR> function editUserForm(){ <BR> //将变量定义成局部变量,避免每次都生成一个新对象 <br><br> var userForm = null; <BR> userForm = new Ext.FormPanel({ <BR> id:'editForm', <BR> labelWidth:'80', <BR> labelAlign:'right', <BR> border:false, <BR> bodyBorder:false, <BR> frame:true, <BR> items:[ <BR> layout:'column', <BR> items:[ <BR> { <BR> columnWidth:'.8', <BR> items:{ <BR> name:'userInfo.userId', <BR> hiddenName:'userInfo.userId', //hiddenName动态的绑定数据库中对应的字段 <BR> xtype:'textField', //xtype可以分为三类,textField为表单域的控件 <BR> fieldLabel:'用户编码', //控件前的文本说明 <BR> labelSeparator:':', <BR> readOnly:true, //文本框只读 <BR> disabled:true, //文本框灰色,区别与其他的文本框颜色 <BR> blankText : '填写用户编码', //为空的文本框提示信息 <BR> allowBlank:false, //不允许为空 <BR> maxLength:'50', //文本框允许输入的最大的长度,最小的minLength <BR> //字母开头,且只能存在字母与数字长度为2到12位 <br><br> regex : /^[a-zA-Z]{1}([a-zA-Z0-9]|[_]){1,11}$/, <BR> regexText : '用户编码必须以字母开头,长度2-12位!', <BR> anchor:'90%' <BR> } <BR> },{ <BR> columnWidth:'.8', <BR> items:{ <BR> name:'userInfo.userName', <BR> hiddenName:'userInfo.userName', <BR> xtype:'textField', <BR> fieldLabel:'用户姓名', <BR> labelSeparator:':', <BR> blankText:'填写用户姓名', <BR> allowBlank:false, <BR> maxLength:'100', <BR> //只含有汉字、数字、字母、下划线不能以下划线开头和结尾 <br><br> regex : /^(?!_)(?!.*?_$)[a-zA-Z0-9_\u4e00-\u9fa5]+$/, <BR> regexText : '只含有汉字、数字、字母、下划线不能以下划线开头和结尾!', <BR> anchor:'90%' <BR> } <BR> },{ <BR> columnWidth:'.2', <BR> items:{ <BR> hiddenName:"infoFill", <BR> name:"infoFill", <BR> xtype:'label', <BR> html:'<font color=red>*', <BR> labelSeparator:'', <BR> anchor:'100%' <BR> } <BR> },{ <BR> columnWidth:'.8', <BR> items:{ <BR> name:'userInfo.pwd', <BR> hiddenName:'userInfo.pwd', <BR> xtype:'textField', <BR> inputType:'password', <BR> fieldLabel:'用户密码<font color=red>*', <BR> labelSeparator:':', <BR> blankText:'填写用户密码', <BR> allowBlank:false, <BR> maxLength:'12', <BR> minLength:'6', <BR> anchor:'90%' <BR> } <BR> },{ <BR> columnWidth:'.2', <BR> items:{ <BR> hiddenName:"infoFill", <BR> name:"infoFill", <BR> xtype:'label', <BR> html:'<font color=red>*', <BR> labelSeparator:'', <BR> anchor:'100%' <BR> } <BR> },{ <BR> columnWidth:'.8', <BR> items:{ <BR> name:'rPwd', <BR> hiddenName:'rPwd', <BR> xtype:'textField', <BR> inputType:'password', <BR> fieldLabel:'确认密码<font color=red>*', <BR> labelSeparator:':', <BR> blankText:'二次输入的秘密要相同', <BR> allowBlank:false, <BR> //在extCommon.js文件中定义二次输入的密码相同验证pwdRange <br><br> // vtype为验证的方法,如果是通用的验证,请在方法中定义,如果是特例,可以使用regex <BR> vtype:'pwdRange', <BR> pwdRange:{begin:'userInfo.pwd',end:'rPwd'}, <BR> maxLength:'12', <BR> anchor:'90%' <BR> } <BR> },{ <BR> columnWidth:'.2', <BR> items:{ <BR> hiddenName:"infoFill", <BR> name:"infoFill", <BR> xtype:'label', <BR> html:'<font color=red>*', <BR> labelSeparator:'', <BR> anchor:'100%' <BR> } <BR> }] <BR> ] <BR> }); <BR> } <BR> /** <BR> * onReady:该文件准备好(在onload和图像加载之前) <BR> */ <BR> Ext.onReady(function(){ <br><br> searchQueryForm(); <BR> //获取查询form <br><br> var queryForm = Ext.getCmp("queryForm").getForm(); <BR> /** <BR> * layout设置为border表示页面将划分为东南西北中五个部分 <BR> * 这里表示centerPanel放在中间 <BR> */ <BR> var layout = new Ext.Viewport({ <BR> layout:'border', <BR> defaluts:{border:false,bodyBorder:false,activeTab:0}, <BR> items:[queryForm,{id:'centerPanel',region:'center',height:document.body.clientHeight,contentEl:'mainDiv'}] <BR> }); <BR> //页面加载的时候,默认数据查询页面显示用户信息列表 <br><br> submitForm(); <BR> }); <br><br> //查询信息 <BR> var store = new Ext.data.Store({ <BR> url:'../user/doGetPageList.action', //action的路径 <BR> reader:new Ext.data.JsonReader({ <BR> root:'userList', //从struts2.0里面传递过来的参数:用户的集合 <BR> totalProperty:'rowTotal', //从struts2.0里面传递过来的参数:总共的信息的行数 <BR> id:'userId', <BR> successPropery:'success'}, <BR> ['userId','userName','pwd'] <BR> ) <BR> }); <br><br> /** <BR> * callback:调用的函数 <BR> */ <BR> function getMsg() <BR> { <br><br> } <BR> /** <BR> * 模糊查询 <BR> */ <BR> function submitForm() <BR> { <BR> //初始化grid <BR> var grid = null; <BR> //复选框 <BR> var sm = new Ext.grid.CheckboxSelectionModel({ <BR> dataIndex:'id', <BR> width:'20' <BR> }); <BR> /** <BR> *  sortabel:(可选)如果真要排序允许在此列 <BR> *  renderer:(可选)用于生成给定数据值单元格的HTML标记的功能。如果没有指定,默认渲染器使用的原始数据值。 <BR> * 在renderer:function createButton(参数)这里的参数可以没有或多个 <BR> *  鼠标移动图片上变成"手"是:style="cursor:hand" <BR> */ <BR> var colM = new Ext.grid.ColumnModel( <BR> [sm,{header:'用户账号',dataIndex:'userId',align:'center',sortable:true}, <BR> {header:'用户姓名',dataIndex:'userName',align:'center',sortabel:true}, <BR> {header:'删除',dataIndex:'id',align:'center',renderer:function createButton(){ <BR> return '<img alt="删除" style="max-width:90%" style="cursor:hand" src="../images/hh_framework/ico_delete.gif" src="images/hh_framework/ico_delete.gif" />';}}, <BR> {header:'编辑',dataIndex:'userId',align:'center',renderer:function createButton(userId, metadata, record){ <BR> return '<a style="cursor:hand" style="cursor:hand" onclick=updateForm('+userId+') >'+record.get('userName')+'的信息修改'+'';}}] <BR> ); <br><br> //获取查询表单 <br><br> var form = Ext.getCmp("queryForm").getForm(); <br><br> //判断是否通过验证,如果没有请直接关闭 <BR> if(!form.isValid()) <BR> { <BR> Ext.Msg.alert("系统提示","查询数据不正确,请确认输入!"); <BR> return ; <BR> } <BR> //差选queryform中查询的数据参数 <br><br> store.baseParams = form.getValues(); <BR> /** <BR> * getLimitCount():获取分页每页行数,如果不传值,则会取默认值 <BR> * Start表示读取数据的起始位置、limit表示每次读取多少条数据 <BR> * callback:getMsg 表示回调时,执行函数 getMsg。可省略 <BR> */ <br><br> store.load({params:{start:0,limit:getLimitCount()}, callback:getMsg}); <BR> if(grid == null) <BR> { <BR> grid = new Ext.grid.EditorGridPanel({ <BR> renderTo:"mainDiv", //grid查询结果指向显示的div层 <BR> title:"用户查询结果", //grid标题 <BR> width:document.body.clientWidth, //设置grid的width的值 <BR> hight:document.doby.clientHight-100,//设置hight的值 <BR> viewConfig:{forceFit:true}, //设置列数的充满窗口 <BR> loadMask:true, //在加载数据时的遮罩效果 <BR> stripeRows:true, //隔行换色 <BR> region:'center', //这个是设置在ViewPort中显示的位置 <BR> cm:colM, //定义的列 <BR> ds:store, //定义的数据源 <BR> border:false, <BR> bodyBorder:false, <BR> sm:sm, //定义的复选框 <br><br> //listeners:包含一个或多个事件处理程序被添加到这个对象初始化过程中 <br><br> listeners:{cellclick:renderPage}, <BR> /** <BR> * bbar: new Ext.PagingToolbar部分是定义分页工具栏, <BR> * 这里的分页控件栏还用到了1个自己定义的插件,就是可以选择每页显示多少条的 <BR> * plugins : [new Ext.ux.PageSizePlugin()],参考Ext的API <BR> * 要实现分页,后台必须有total属性,表示共多少条数据 <BR> */ <br><br> bbar:new Ext.PagingToolbar({ <BR> items:[{ <BR> xtype:'button', <BR> text:'删除所选', <BR> handler:delUserAll, //自定义执行动 <BR> pressed:true <BR> }], <BR> id:'pageToolBar', <BR> pageSize:QUERY_PAGE_SIZE, //每页的行数默认为:QUERY_PAGE_SIZE <BR> store:store, <BR> displayInfo:true, <BR> displayMsg:'显示第{0}条到{1}条记录,共{2}条记录', <BR> emptMsg:'没有记录', <BR> plugins:[new Ext.ux.PageSizePlugin()] <BR> }) <BR> }); <BR> } <BR> grid.render(); <BR> } <BR> /** <BR> * 增加用户信息 <BR> */ <BR> function addForm() <BR> { <BR> showUserForm(); <BR> //获取绘制用户窗口的form <br><br> var userForm = Ext.getCmp("conditionForm").getForm(); <BR> //初始化用户添加的窗口的Id <br><br> var addUserWin = Ext.getCmp("addWin"); <BR> if(addUserWin == null) <BR> { <BR> addUserWin = new Ext.Window({ <BR> width:500, //初始窗口的width的值 <BR> x:100, //窗口的初始化x方向的位置 <BR> y:100, //窗口的初始化y方向的位置 <BR> plain:true, <BR> modal:true, //模式窗口,默认为false <BR> closeAction:"hide", //默认窗口隐藏 <BR> resizable:false, //窗口的大小不允许拖动,默认为true <BR> id:"addWin", //指定用户添加窗口的Id <BR> items:[userForm], <BR> buttons:[ <BR> {text:'保存',handler:function(){ <BR> if(userForm.form.isValid()){ <BR> userForm.form.doAction('submit',{ <BR> url:'../user/addUser.action', <BR> params:{roleId:userForm.form.findField('userId').getValue()}, <BR> method:'post', //数据提交的方式:有两种get,post <BR> waitTitle:'提示信息', //数据提交等待的滚动条 <BR> waitMsg:'保存数据,请稍候...', //滚动条提示的内容 <BR> success:function(form,action){ <BR> var message = action.result.message; <BR> if(message == null){ <BR> Ext.Msg.alert("提示信息","用户信息添加成功!"); <BR> store.reload(); <BR> addUserWin.hide(); <BR> }else{ <BR> Ext.Msg.alert("提示信息",message); <BR> } <BR> }, <BR> failure:function(form,action){ <BR> Ext.Msg.alert('提示信息',"新用户添加失败!"); <BR> return; <BR> } <BR> }); <BR> }else { <BR> Ext.Msg.alert("提示信息","表单有错误,请正确填写!"); <BR> } <BR> }}, <BR> {handler:function(){userForm.form.reset();},text:'重置'}, <BR> {handler:function(){addUserWin.hide();},text:'关闭'}] <BR> }); <BR> } <BR> addUserWin.show(); <BR> } <BR> /** <BR> * 删除用户信息 <BR> */ <BR> function delForm(userId) <BR> { <BR> Ext.Msg.confirm('提示信息','你确定要执行删除操作吗?',function(btn){ <BR> if(btn == 'yes') <BR> { <BR> /** <BR> * 数据提交的一种方式:Ext.Ajax.request({}); <BR> */ <BR> Ext.Ajax.request({ <BR> url:'../user/delUser.action', <BR> params:{userId:userId}, <BR> method:'post', <BR> success:function(o) <BR> { <BR> var info = Ext.decode(o.responseText); <BR> Ext.Msg.alert("提示信息",info.message); <BR> store.reload(); <BR> return ; <BR> }, <BR> failure:function(form,action) <BR> { <BR> Ext.Msg.alert("提示信息","用户信息删除失败!"); <BR> return ; <BR> } <BR> }); <BR> } <BR> }); <BR> } <BR> /** <BR> &nbs</script> </div>