ext-2.0扩展树形下拉框(代码及演示)

转自: http://www.iteye.com/topic/148441

演示地址:www.gx80.cn/example/ext-2.0/form/treeField.html

原代码:

js 代码
  1. /**
  2. *ExtJSLibrary2.0extend
  3. *Version:1.0
  4. *Author:飞天色鼠
  5. *Date:2007-12-13
  6. *E-mail:gx80@qq.com
  7. *HomePage:http://www.gx80.cn
  8. */
  9. /**
  10. *TreeField
  11. */
  12. Ext.form.TreeField=Ext.extend(Ext.form.TriggerField,{
  13. readOnly:true,
  14. defaultAutoCreate:{tag:"input",type:"text",size:"24",autocomplete:"off"},
  15. displayField:'text',
  16. valueField:undefined,
  17. hiddenName:undefined,
  18. listWidth:undefined,
  19. minListWidth:50,
  20. layerHeight:undefined,
  21. minLayerHeight:60,
  22. dataUrl:undefined,
  23. tree:undefined,
  24. value:undefined,
  25. baseParams:{},
  26. treeRootConfig:{
  27. id:'',
  28. text:'pleaseselect...',
  29. draggable:false
  30. },
  31. initComponent:function(){
  32. Ext.form.TreeField.superclass.initComponent.call(this);
  33. this.addEvents(
  34. 'select',
  35. 'expand',
  36. 'collapse',
  37. 'beforeselect'
  38. );
  39. if(this.transform){
  40. this.allowDomMove=false;
  41. vars=Ext.getDom(this.transform);
  42. if(!this.hiddenName){
  43. this.hiddenName=s.name;
  44. }
  45. s.name=Ext.id();
  46. if(!this.lazyRender){
  47. this.target=true;
  48. this.el=Ext.DomHelper.insertBefore(s,this.autoCreate||this.defaultAutoCreate);
  49. Ext.removeNode(s);
  50. this.render(this.el.parentNode);
  51. }else{
  52. Ext.removeNode(s);
  53. }
  54. }
  55. },
  56. onRender:function(ct,position){
  57. Ext.form.TreeField.superclass.onRender.call(this,ct,position);
  58. if(this.hiddenName){
  59. this.hiddenField=this.el.insertSibling({tag:'input',type:'hidden',name:this.hiddenName,id:(this.hiddenId||this.hiddenName)},
  60. 'before',true);
  61. this.hiddenField.value=
  62. this.hiddenValue!==undefined?this.hiddenValue:
  63. this.value!==undefined?this.value:'';
  64. this.el.dom.removeAttribute('name');
  65. }
  66. if(Ext.isGecko){
  67. this.el.dom.setAttribute('autocomplete','off');
  68. }
  69. this.initList();
  70. },
  71. initList:function(){
  72. if(!this.list){
  73. varcls='x-treefield-list';
  74. this.list=newExt.Layer({
  75. shadow:this.shadow,cls:[cls,this.listClass].join(''),constrain:false
  76. });
  77. varlw=this.listWidth||Math.max(this.wrap.getWidth(),this.minListWidth);
  78. this.list.setWidth(lw);
  79. this.list.swallowEvent('mousewheel');
  80. this.innerList=this.list.createChild({cls:cls+'-inner'});
  81. this.innerList.setWidth(lw-this.list.getFrameWidth('lr'));
  82. this.innerList.setHeight(this.layerHeight||this.minLayerHeight);
  83. if(!this.tree){
  84. this.tree=this.createTree(this.innerList);
  85. }
  86. vartreeField=this;
  87. this.tree.on('click',function(node,e){
  88. treeField.onSelect(node);
  89. });
  90. this.tree.render();
  91. }
  92. },
  93. onSelect:function(node){
  94. if(this.fireEvent('beforeselect',node,this)!=false){
  95. this.setValue(node);
  96. this.collapse();
  97. this.fireEvent('select',this,node);
  98. }
  99. },
  100. createTree:function(el){
  101. varTree=Ext.tree;
  102. vartree=newTree.TreePanel({
  103. el:el,
  104. autoScroll:true,
  105. animate:true,
  106. containerScroll:true,
  107. loader:newTree.TreeLoader({
  108. dataUrl:this.dataUrl,
  109. baseParams:this.baseParams
  110. })
  111. });
  112. varroot=newTree.AsyncTreeNode(this.treeRootConfig);
  113. tree.setRootNode(root);
  114. returntree;
  115. },
  116. getValue:function(){
  117. if(this.valueField){
  118. returntypeofthis.value!='undefined'?this.value:'';
  119. }else{
  120. returnExt.form.TreeField.superclass.getValue.call(this);
  121. }
  122. },
  123. setValue:function(node){
  124. vartext=node[this.displayField];
  125. varvalue=node[this.valueField||this.displayField];
  126. if(this.hiddenField){
  127. this.hiddenField.value=value;
  128. }
  129. Ext.form.TreeField.superclass.setValue.call(this,text);
  130. this.value=value;
  131. },
  132. onDestroy:function(){
  133. if(this.list){
  134. this.list.destroy();
  135. }
  136. Ext.form.TreeField.superclass.onDestroy.call(this);
  137. },
  138. collapseIf:function(e){
  139. if(!e.within(this.wrap)&&!e.within(this.list)){
  140. this.collapse();
  141. }
  142. },
  143. expand:function(){
  144. if(this.isExpanded()||!this.hasFocus){
  145. return;
  146. }
  147. this.list.alignTo(this.wrap,this.listAlign);
  148. this.list.show();
  149. Ext.getDoc().on('mousewheel',this.collapseIf,this);
  150. Ext.getDoc().on('mousedown',this.collapseIf,this);
  151. this.fireEvent('expand',this);
  152. },
  153. collapse:function(){
  154. if(!this.isExpanded()){
  155. return;
  156. }
  157. this.list.hide();
  158. Ext.getDoc().un('mousewheel',this.collapseIf,this);
  159. Ext.getDoc().un('mousedown',this.collapseIf,this);
  160. this.fireEvent('collapse',this);
  161. },
  162. isExpanded:function(){
  163. returnthis.list&&this.list.isVisible();
  164. },
  165. onTriggerClick:function(){
  166. if(this.disabled){
  167. return;
  168. }
  169. if(this.isExpanded()){
  170. this.collapse();
  171. }else{
  172. this.onFocus({});
  173. this.expand();
  174. }
  175. this.el.focus();
  176. }
  177. });
  178. Ext.reg('treefield',Ext.form.TreeField);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值