Extjs3.2自定义组件的书写

阅读更多

最近参与的项目中用到Extjs,研究了几天, 发现实际开发中很有必要将各种布局的组件一起作为自定义组件使用,以提高代码重用性。

项目中的‘添加用户’功能中, 由于用户信息比较多,还同时可能要注册其账号信息,所以想在添加的时候分多步填写最后提交。

翻阅Extjs3.2的example发现没有满足要求的组件。于是试着书写一个。我暂时给它取名叫StepForm。

 

1.Extjs中可以模拟java中的集成关系,从而可以让自定义的‘类’集成自Extjs中的‘类’,比如Ext.Panel,Ext.Window等等。

于是,要做的第一步就是新建一个StemForm.js文件,然后用Ext.extend(Ext.Panel,{})集成Ext中的Panel。

 

view plain copy to clipboard print ?
  1. Ext.ns("Util");  
  2. Util.StepForm = Ext.extend(Ext.Panel,{  
  3.     //重写构造函数,conf为自定义的参数   
  4.     constructor:function(conf){},  
  5.   
  6.    //自定义函数   
  7.     register:function(){}  
  8.   
  9. ))  

 

 

2.重写Panel的构造函数constructor,在该函数中书写Util.StepForm.superclass.constructor.apply(this,[{...}])来提交自定义的参数。

 

view plain copy to clipboard print ?
  1. constructor:function(conf){  
  2.         var items = conf.items;  //点击左边连接可以切换的不同的表单,   
  3.     var number = conf.number; //列表是否显示数字   
  4.     //...   
  5.   
  6.          //若干操作   
  7.          Util.StepForm.superclass.constructor.apply(  
  8.          this,[{  
  9.                 //这里开始就是你要自定义的布局或者元素了,因为该类本身继承自Ext.Panel,所以它也可以添加跟多子组件并布局。   
  10.         width:conf.width,  
  11.         height:conf.height,  
  12.         layout:'border',  
  13.         items:[  
  14.                          
  15.             new Ext.Panel({  
  16.                 id:'nav',  
  17.                 title:ltitle,  
  18.                 region:'west',  
  19.                 width:navWidth,  
  20.                 html:link  
  21.             }),  
  22.             //...省略   
  23.         ]  
  24.         }]  
  25.         );  
  26.           
  27.         //若干操作省略   
  28. }  

 

 

3.当自定义StepForm类写好后,可以与使用Ext本身组件一样的方式使用 var stepForm = new Util.StepForm({});

 

最后我把完整代码输出:

StepForm.js:

 

view plain copy to clipboard print ?
  1. Ext.ns("Util");  
  2. Util.StepForm = Ext.extend(Ext.Panel,{  
  3.     constructor:function(conf){  
  4.         var items = conf.items;  //点击左边连接可以切换的不同的表单,   
  5.         var number = conf.number; //列表是否显示数字   
  6.         var activeTab = conf.activeTab;    //加载时的默认活动标签   
  7.         var ltitle = conf.ltitle;   //z左边标题   
  8.         var rtitle = conf.rtitle;   //右边标题   
  9.         var navWidth = conf.navWidth; //导航栏的宽度   
  10.         var count = activeTab;  //记录当前活动项的索引   
  11.         if(activeTab>items.length-1) {  
  12.             alert("activeTab值大于items的数量");  
  13.             return;  
  14.         }  
  15.         var link = "";     //显示在左边的列表html内容   
  16.         var bodys = new Array();  
  17.         for(var i=0; i
  18.             var item = items[i];  
  19.             if(number){  
  20.                 if(activeTab == i){  
  21.                     link += "+i+"' href="javascript:void(0)" mce_href="javascript:void(0)" style="display:block;cursor:pointer;padding:5px 10px;text-decoration:none;font-size:14px;font-weight:bold" mce_style="display:block;cursor:pointer;padding:5px 10px;text-decoration:none;font-size:14px;font-weight:bold">"+(i+1)+"."+item.title+"
    "
    ;  
  22.                 }else{  
  23.                     link += "+i+"' href="javascript:void(0)" mce_href="javascript:void(0)" style="display:block;cursor:pointer;padding:5px 10px;text-decoration:none;font-size:12px" mce_style="display:block;cursor:pointer;padding:5px 10px;text-decoration:none;font-size:12px">"+(i+1)+"."+item.title+"
    "
    ;  
  24.                 }  
  25.             }else{  
  26.                 if(activeTab == i){  
  27.                     link += "+i+"' href="javascript:void(0)" mce_href="javascript:void(0)" style="display:block;cursor:pointer;padding:5px 10px;text-decoration:none;font-size:14px;font-weight:bold" mce_style="display:block;cursor:pointer;padding:5px 10px;text-decoration:none;font-size:14px;font-weight:bold">"+item.title+"
    "
    ;  
  28.                 }else{  
  29.                     link += "+i+"' href="javascript:void(0)" mce_href="javascript:void(0)" style="display:block;cursor:pointer;padding:5px 10px;text-decoration:none;font-size:12px" mce_style="display:block;cursor:pointer;padding:5px 10px;text-decoration:none;font-size:12px">"+item.title+"
    "
    ;  
  30.                 }  
  31.             }  
  32.             //将表单收集到bodys数组   
  33.             var forms = item.items;  
  34.             if(!forms){  
  35.                 alert("缺少items属性");  
  36.                 return;  
  37.             }  
  38.             var form = item.items[0];  
  39.             form.id = "form"+i;  
  40.             if(activeTab != i){  
  41.                 form.hidden = true;  
  42.             }else{  
  43.                 form.hidden = false;  
  44.             }  
  45.             bodys.push(form);  
  46.         }  
  47.           
  48.         Util.StepForm.superclass.constructor.apply(  
  49.             this,[{  
  50.                 width:conf.width,  
  51.                 height:conf.height,  
  52.                 layout:'border',  
  53.                 items:[  
  54.                     new Ext.Panel({  
  55.                         id:'nav',  
  56.                         title:ltitle,  
  57.                         region:'west',  
  58.                         width:navWidth,  
  59.                         html:link  
  60.                     }),  
  61.                     new Ext.Panel({  
  62.                         title:rtitle,  
  63.                         height:conf.height,  
  64.                         layout:'fit',  
  65.                         region:'center',  
  66.                         items:bodys  
  67.                     }),  
  68.                     new Ext.Panel({  
  69.                         autoHeight:true,  
  70.                         region:'south',  
  71.                         bbar:new Ext.Toolbar([  
  72.                             {xtype:'tbfill'},  
  73.                             {xtype:'button',id:'prev',text:'上一步',handler:function(){  
  74.                                 navFun(count-1);  
  75.                             }},  
  76.                             {xtype:'button',id:'next',text:'下一步',handler:function(){  
  77.                                 navFun(count+1);  
  78.                             }}  
  79.                         ])  
  80.                     })  
  81.                 ]  
  82.             }]  
  83.         );  
  84.         var navFun = function(index){  
  85.             if(index < 0) {  
  86.                 index = 0;  
  87.                 count = 0;  
  88.             }  
  89.             if(index > bodys.length-1){  
  90.                 index = bodys.length - 1;  
  91.                 count = bodys.length - 1;  
  92.             }  
  93.             for(var i=0; i
  94.                 if(i == index){  
  95.                     Ext.getCmp("form"+i).show(true);  
  96.                     Ext.get("alink"+i).dom.style.fontSize = "14px";  
  97.                     Ext.get("alink"+i).dom.style.fontWeight = "bold";  
  98.                 }else{  
  99.                     Ext.getCmp("form"+i).hide(true);  
  100.                     Ext.get("alink"+i).dom.style.fontSize = "12px";  
  101.                     Ext.get("alink"+i).dom.style.fontWeight = "normal";  
  102.                 }  
  103.             }  
  104.             count = index;  
  105.         };  
  106.         this.bodys = bodys;  
  107.         this.activeTab = activeTab;  
  108.         this.navigate = navFun;  
  109.     },  
  110.     register:function(){  
  111.         var activeItem = Ext.get("alink"+this.activeTab).dom.innerHTML;  
  112.         var delegate = this.navigate;  
  113.         //给左边导航添加单击事件   
  114.         for(var i=0; i<this.bodys.length; i++){  
  115.             Ext.get("alink"+i).on("click",function(e){  
  116.                 var i = e.target.id.substring(5);  
  117.                 delegate(i);  
  118.             });  
  119.         }  
  120.           
  121.     }  
  122. });  
  123. Ext.reg('stepform', Util.StepForm);  

 

 

index.jsp

 

view plain copy to clipboard print ?
  1. "-//W3C//DTD HTML 4.01 Transitional//EN">  
  2.   
  3.     
  4.     "<%=basePath%>">  
  5.     My JSP </span><span class="string"><span style="color: #0000ff;">'index.jsp'</span></span><span> starting page  
  6.     "pragma" content="no-cache">  
  7.     "cache-control" content="no-cache">  
  8.     "expires" content="0">      
  9.     "keywords" content="keyword1,keyword2,keyword3">  
  10.     "description" content="This is my page">  
  11.     "stylesheet" type="text/css" href="js/extjs/resources/css/ext-all.css" mce_href="js/extjs/resources/css/ext-all.css" />  
  12.     "js/extjs/adapter/ext/ext-base.js" mce_src="js/extjs/adapter/ext/ext-base.js">  
  13.     "js/extjs/ext-all-debug.js" mce_src="js/extjs/ext-all-debug.js">  
  14.     "js/StepForm.js" mce_src="js/StepForm.js">  
  15.     "text/javascript">   
  16.     
  17.     
  18.     
  19.     
  20.   

 

 

效果图:

 Extjs3.2自定义组件的书写_第1张图片

你可能感兴趣的:(EXT,HTML,JSP)