---
title: "扩展EXTJS ComboBox为下拉选择树"
author: "yongtree"
date: "2011-04-20T12:50:58Z"
tags: []
categories: ["Web前端"]
slug: "extjs-combobox"
draft: false
lastmod: "2011-04-20T12:50:58Z"
id: 1009144
url: "https://yongtree.iteye.com/blog/1009144"
---

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

js 代码

01. Ext.ns("OECP.ui");
02. /\**
03. \* 下拉列表选择树
04. \* &lt;br&gt;
05. \* 依赖EXTJS3版本
06. \* @class OECP.ui.ComboBoxTree
07. \* @extends Ext.form.ComboBox
08. \* @author yongtree
09. \*/
10. OECP.ui.ComboBoxTree = Ext.extend(Ext.form.ComboBox, {
11. /\**
12. \* 回调函数,用于传递选择的id，text属性
13. \*
14. \* @type
15. \*/
16. callback : Ext.emptyFn,
17. store : new Ext.data.SimpleStore({
18. fields : \[],
19. data : \[\[]]
20. }),
21. editable : this.editable||false,
22. mode : 'local',
23. emptyText : this.emptyText||"请选择",
24. allowBlank : this.allowBlank||true,
25. blankText : this.blankText||"必须输入!",
26. triggerAction : 'all',
27. maxHeight : 200,
28. anchor : '95%',
29. displayField : 'text',
30. valueField : 'id',
31. tpl : "&lt;tpl for='.'&gt;&lt;div style='height:200px'&gt;&lt;div id='tree'&gt;&lt;/div&gt;&lt;/div&gt;&lt;/tpl&gt;",
32. selectedClass : '',
33. onSelect : Ext.emptyFn,
34. /\**
35. \* 根的名字
36. \*
37. \* @type String
38. \*/
39. rootText : this.rootText||'root',
40. /\**
41. \* 树的请求地址
42. \*
43. \* @type String
44. \*/
45. treeUrl : this.treeUrl,
46. tree : null,
47. initComponent : function() {
48. this.tree = new Ext.tree.TreePanel({
49. height : 200,
50. scope : this,
51. autoScroll : true,
52. split : true,
53. root : new Ext.tree.AsyncTreeNode({
54. expanded : true,
55. id : '\_oecp',
56. text : this.rootText
57. }),
58. loader : new Ext.tree.TreeLoader({
59. url : this.treeUrl
60. }),
61. rootVisible : true
62. });
63. var c = this;
64. /\**
65. \* 点击选中节点并回调传值
66. \*/
67. this.tree.on('click', function(node) {
68. if (node.id != null && node.id != '') {
69. if (node.id != '\_oecp') {
70. c.setValue(node.text);
71. c.callback.call(this, node.id, node.text);
72. c.collapse();
73. } else {
74. Ext.Msg.alert("提示", "此节点无效，请重新选择!")
75. }
76. }
77. })
78. this.on('expand', function() {
79. this.tree.render('tree');
80. this.tree.expandAll();
81. });
82. OECP.ui.ComboBoxTree.superclass.initComponent.call(this);
83. }
84. })

在页面上如下使用：

xhtml 代码

01. &lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"&gt;
02. &lt;html&gt;
03. &lt;head&gt;
04. &lt;meta http-equiv="content-type" content="text/html; charset=UTF-8" /&gt;
05. &lt;title&gt;ComboBoxTree.html&lt;/title&gt;
06. &lt;link rel="stylesheet" type="text/css"
07. href="../../extjs/resources/css/ext-all.css" /&gt;
08. &lt;script type="text/javascript"
09. src="../../extjs/adapter/ext/ext-base.js"&gt;&lt;/script&gt;
10. &lt;script type="text/javascript" src="../../extjs/ext-all.js"&gt;&lt;/script&gt;
11. &lt;script type="text/javascript" src="../core/ComboBoxTree.js"&gt;&lt;/script&gt;
12. &lt;/head&gt;
13. &lt;body&gt;
14. &lt;div id="cbt"&gt;&lt;/div&gt;
15. &lt;/body&gt;
16. &lt;/html&gt;
17. &lt;script type="text/javascript"&gt;
18. Ext.onReady(function() {
19. new OECP.ui.ComboBoxTree({
20. renderTo:"cbt",
21. treeUrl:"ComboBoxTree.json",
22. editable:false,
23. rootText:"选择公司",
24. valueField:"com",
25. displayField:"\_com",
26. callback:function(id,text){
27. alert(id);
28. alert(text);
29. //可以做更多的处理。
30. }
31. });
32. })
33. &lt;/script&gt;

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

OECP平台正在研发之中，在第一个稳定版本发布的时候，将开放相关的API。  
相关的API，可参见：[http://prj.oecp.cn/projects/oecp-platform/wiki/OECPuiComboBoxTree](http://prj.oecp.cn/projects/oecp-platform/wiki/OECPuiComboBoxTree)

原文：[http://www.oecp.cn/hi/yongtree/blog/2300](http://www.oecp.cn/hi/yongtree/blog/2300)