Java Blog for Alex Wan

          Let life be beautiful like summer flowers and death like autumn leaves.

          統計

          留言簿(10)

          BlogJava

          Blogs

          DIV+CSS

          JQuery相關

          友情鏈接

          常去的地方

          數據供應

          閱讀排行榜

          評論排行榜

          [Extjs]基于Ext2.0的樹狀選擇框(TreeComboBox)

          背景:有一些時候我們處理的數據本身就是樹狀的結構,當我們需要在從這些數據中做選擇的時候,希望出來的數據也是樹狀,以方便在邏輯上區分.
          實現方法:擴展和樹相關的類,使得他們可以用于選擇.
          實現代碼:
          Ext.ux.OptionTreeNode
          Ext.ux.OptionTreeNode=function(attributes)
          {
              
              Ext.ux.OptionTreeNode.superclass.constructor.call(
          this,attributes);
              
          this.value=attributes.value||'';
              
          this.proirity=attributes.proirity||'';
          }
          ;
          Ext.extend(Ext.ux.OptionTreeNode ,Ext.tree.TreeNode, 
          {
          }
          );

          Ext.ux.AsyncOptionTreeNode
          Ext.ux.AsyncOptionTreeNode=function(attributes)
          {
              
              Ext.ux.AsyncOptionTreeNode.superclass.constructor.call(
          this,attributes);
              
          this.value=attributes.value||'';
              
          this.proirity=attributes.proirity||'';
          }
          ;
          Ext.extend(Ext.ux.AsyncOptionTreeNode ,Ext.tree.AsyncTreeNode, 
          {
          }
          );

          Ext.ux.OptionTreeLoader
          Ext.ux.OptionTreeLoader = function(config) {
              Ext.ux.OptionTreeLoader.superclass.constructor.call(
          this, config);
          }
          ;

          Ext.extend(Ext.ux.OptionTreeLoader, Ext.tree.TreeLoader, 
          {
              createNode : 
          function(attr){
                  Ext.apply(attr, 
          this.baseAttr || {});
                  
          if(typeof attr.uiProvider == 'string'){
                      attr.uiProvider 
          = this.uiProviders[attr.uiProvider] || eval(attr.uiProvider);
                  }


                  
          return(attr.leaf ?
                      
          new Ext.ux.OptionTreeNode(attr) :
                          
          new Ext.ux.AsyncOptionTreeNode(attr));
              }

          }
          );

          Ext.ux.TreeComboBox
          Ext.ux.TreeComboBox=function(config)
          {
              
          var treeId=config.hiddenName + '-tree'+Ext.id();
              
          var treeConfig = Ext.apply({}{
                      border:
          false,
                      id:treeId
                  }
          {
                  loader: 
          new Ext.ux.OptionTreeLoader({dataUrl:'tree/optionNodes.html'}),
                  border:
          false,
                   root:
          new Ext.ux.AsyncOptionTreeNode({text: '根節點',id:'0',value:''})}

              );
              
          this.tree=new Ext.tree.TreePanel(treeConfig);
              
          //this.hiddenField=new Ext.form.Hidden({name:config.valueName});
              var _combobox=this;
              
          var _tree=this.tree;
              _tree.on('click',
          function(node){
                  
          var record=_combobox.setValueAndText(node.value,node.text);
                    _combobox.collapse();
                    _combobox.fireEvent('select', _combobox, record, 
          0);
                }
          );
                
          this.onExpand=function()
                
          {
                    _tree.render(treeId);
                }

              Ext.ux.TreeCombobox.superclass.constructor.call(
          this,Ext.apply(
          {
                  hiddenName:config.hiddenName,
                  
          //name:config.name,
                  fieldLabel:config.fieldLabel,
                  emptyText:config.emptyText,
                  valueField:config.valueField
          ||'value',
                  displayField:config.displayField
          ||'text',
                  store:
          new Ext.data.SimpleStore({fields:[],data:[[]]}),
                  editable:
          false,
                  shadow:
          false,
                  autoScroll:
          true,
                  mode: 'local',
                  triggerAction:'all',
                  maxHeight: config.maxHeight
          ||200,
                  tpl: '
          <tpl for="."><div style="height:'+config.maxHeight+'px"><div id="'+treeId+'"></div></div></tpl>',
                  selectedClass:'',
                  onSelect:Ext.emptyFn
          }
          ,config
          ));
          }
          ;
          Ext.extend(Ext.ux.TreeComboBox ,Ext.form.ComboBox, 
          {
              onRender : 
          function(ct, position){
                  Ext.ux.TreeComboBox.superclass.onRender.apply(
          this, arguments);
                  
          this.on("expand",this.onExpand,this);
                  
          if(this.allowBlanl==false)
                  
          this.setDefault();
              }
          ,
               setValue : 
          function(v){
                  
          if(v)
                  
          this. setValueAndText(v.code,v.name);
                  
          else
                  Ext.form.ComboBox.superclass.setValue.call(
          this, v);
              }
          ,
              setValueAndText : 
          function(v,t){
                  
          //Ext.log(v+t);
                  var text = v==''?'根節點':t;
                  
          this.lastSelectionText = text;
                  
          if(this.hiddenField){
                      
          this.hiddenField.value = v;
                  }

                  Ext.form.ComboBox.superclass.setValue.call(
          this, text);
                  
          this.value = v;
                  
          var RecordType = Ext.data.Record.create([
                      
          {name: this.valueField},
                      
          {name: this.displayField}
                  ]);
                  
          var data={};
                  data[
          this.valueField]=v;
                  data[
          this.displayField]=t;
                  
          var record = new RecordType(data);
                  
          return record;
              }
          ,
              reset:
          function()
              
          {
                  
          this.tree.getRootNode().collapse();
                  Ext.ux.RegionField.superclass.reset.call(
          this);
              }

          }
          );


          例子:
              var treeComboBox=new Ext.ux.TreeComboBox({
                  hiddenName:'treeComboBox',
                  fieldLabel:'樹狀選擇框',
                  maxHeight:
          250,
                  treeWidth:
          200}
          );


          Let life be beautiful like summer flowers and death like autumn leaves.

          posted on 2008-08-11 09:14 Alexwan 閱讀(5496) 評論(5)  編輯  收藏 所屬分類: JavaScript

          評論

          # re: [Extjs]基于Ext2.0的樹狀選擇框(TreeComboBox) 2008-08-11 10:03 EricFan

          這種純累code的文章真的很難看下去啊  回復  更多評論   

          # re: [Extjs]基于Ext2.0的樹狀選擇框(TreeComboBox) 2008-08-11 10:30 萬洪泉

          不用思考的文章,看了又有什么用呢?  回復  更多評論   

          # re: [Extjs]基于Ext2.0的樹狀選擇框(TreeComboBox) 2008-08-11 10:42 zhuxing

          現在ext真的有點走紅了
          最近看到不少產品中的web解決方案都用了ext  回復  更多評論   

          # re: [Extjs]基于Ext2.0的樹狀選擇框(TreeComboBox) 2008-08-11 11:02 萬洪泉

          EXT在內存方面比較薄弱,使用的內存多,而且沒有一個好的釋放內存機制!  回復  更多評論   

          # re: [Extjs]基于Ext2.0的樹狀選擇框(TreeComboBox) 2009-03-31 15:45 extjs

          ie下顯示不正常呀  回復  更多評論   

          主站蜘蛛池模板: 华亭县| 拜泉县| 揭阳市| 华容县| 晋城| 辽宁省| 保山市| 千阳县| 景泰县| 鄯善县| 天峻县| 花莲市| 广元市| 诸城市| 北票市| 米林县| 靖江市| 开原市| 乌鲁木齐县| 鄂伦春自治旗| 西藏| 进贤县| 融水| 泉州市| 西昌市| 东乌| 茂名市| 土默特右旗| 资阳市| 杭州市| 全椒县| 历史| 石家庄市| 永嘉县| 石狮市| 曲沃县| 河北区| 临沧市| 盐亭县| 桐庐县| 芷江|