首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > JavaScript >

扩展EXTJS ComboBox为上拉选择树

2012-10-23 
扩展EXTJS ComboBox为下拉选择树在做OECP平台的登陆页面时,需要选择相应的公司进行登陆,公司的选择是树形

扩展EXTJS ComboBox为下拉选择树

在做OECP平台的登陆页面时,需要选择相应的公司进行登陆,公司的选择是树形结构的,而extjs的下拉combobox为列表结构,为了让页面操作更加简单,决定将下拉列表改造成下拉树。
在这里主要用到了Extjs的extend的语法,扩展比较简单,直接上代码吧。

js 代码
  1. Ext.ns("OECP.ui"); /**
  2. * 下拉列表选择树 * <br>
  3. * 依赖EXTJS3版本 * @class OECP.ui.ComboBoxTree
  4. * @extends Ext.form.ComboBox * @author yongtree
  5. */ OECP.ui.ComboBoxTree = Ext.extend(Ext.form.ComboBox, {
  6. /** * 回调函数,用于传递选择的id,text属性
  7. * * @type
  8. */ callback : Ext.emptyFn,
  9. store : new Ext.data.SimpleStore({ fields : [],
  10. data : [[]] }),
  11. editable : this.editable||false, mode : 'local',
  12. emptyText : this.emptyText||"请选择", allowBlank : this.allowBlank||true,
  13. blankText : this.blankText||"必须输入!", triggerAction : 'all',
  14. maxHeight : 200, anchor : '95%',
  15. displayField : 'text', valueField : 'id',
  16. tpl : "<tpl for='.'><div style='height:200px'><div id='tree'></div></div></tpl>", selectedClass : '',
  17. onSelect : Ext.emptyFn, /**
  18. * 根的名字 *
  19. * @type String */
  20. rootText : this.rootText||'root', /**
  21. * 树的请求地址 *
  22. * @type String */
  23. treeUrl : this.treeUrl, tree : null,
  24. initComponent : function() {
  25. this.tree = new Ext.tree.TreePanel({ height : 200,
  26. scope : this, autoScroll : true,
  27. split : true, root : new Ext.tree.AsyncTreeNode({
  28. expanded : true, id : '_oecp',
  29. text : this.rootText }),
  30. loader : new Ext.tree.TreeLoader({
  31. url : this.treeUrl
  32. }), rootVisible : true
  33. }); var c = this;
  34. /** * 点击选中节点并回调传值
  35. */ this.tree.on('click', function(node) {
  36. if (node.id != null && node.id != '') { if (node.id != '_oecp') {
  37. c.setValue(node.text); c.callback.call(this, node.id, node.text);
  38. c.collapse(); } else {
  39. Ext.Msg.alert("提示", "此节点无效,请重新选择!") }
  40. }
  41. })
  42. this.on('expand', function() { this.tree.render('tree');
  43. this.tree.expandAll(); });
  44. OECP.ui.ComboBoxTree.superclass.initComponent.call(this);
  45. }
  46. })

在页面上如下使用:
xhtml 代码
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html>
  2. <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
  3. <title>ComboBoxTree.html</title> <link rel="stylesheet" type="text/css"
  4. href="../../extjs/resources/css/ext-all.css" /> <script type="text/javascript"
  5. src="../../extjs/adapter/ext/ext-base.js"></script> <script type="text/javascript" src="../../extjs/ext-all.js"></script>
  6. <script type="text/javascript" src="../core/ComboBoxTree.js"></script> </head>
  7. <body>
  8. <div id="cbt"></div> </body>
  9. </html> <script type="text/javascript">
  10. Ext.onReady(function() { new OECP.ui.ComboBoxTree({
  11. renderTo:"cbt", treeUrl:"ComboBoxTree.json",
  12. editable:false, rootText:"选择公司",
  13. valueField:"com", displayField:"_com",
  14. callback:function(id,text){ alert(id);
  15. alert(text);
  16. //可以做更多的处理。 }
  17. }); })
  18. </script>


附件中包含了该组件源代码和一个demo。

OECP平台正在研发之中,在第一个稳定版本发布的时候,将开放相关的API。
相关的API,可参见:http://prj.oecp.cn/projects/oecp-platform/wiki/OECPuiComboBoxTree
原文:http://www.oecp.cn/hi/yongtree/blog/2300

热点排行