`
yongtree
  • 浏览: 230659 次
  • 性别: Icon_minigender_1
  • 来自: 青岛
社区版块
存档分类
最新评论

扩展EXTJS ComboBox为下拉选择树

    博客分类:
  • RIA
阅读更多

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

js 代码
  1. Ext.ns("OECP.ui");
  2. /**
  3. * 下拉列表选择树
  4. * <br>
  5. * 依赖EXTJS3版本
  6. * @class OECP.ui.ComboBoxTree
  7. * @extends Ext.form.ComboBox
  8. * @author yongtree
  9. */
  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 : "<tpl for='.'><div style='height:200px'><div id='tree'></div></div></tpl>",
  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 代码
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  2. <html>
  3. <head>
  4. <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
  5. <title>ComboBoxTree.html</title>
  6. <link rel="stylesheet" type="text/css"
  7. href="../../extjs/resources/css/ext-all.css" />
  8. <script type="text/javascript"
  9. src="../../extjs/adapter/ext/ext-base.js"></script>
  10. <script type="text/javascript" src="../../extjs/ext-all.js"></script>
  11. <script type="text/javascript" src="../core/ComboBoxTree.js"></script>
  12. </head>
  13. <body>
  14. <div id="cbt"></div>
  15. </body>
  16. </html>
  17. <script type="text/javascript">
  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. </script>


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

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


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics