EXTJS下拉复选框

 
前些日子,有网友问,有没有extjs4.1下能用的带复选功能的下拉框,答应帮他看看之后,下载了extjs4.1(最近很忙,快赶上皮鞋的忙劲了)。然后找了一些资料,做了一个简单的带复选框的下拉框插件。


顺带的说下 extjs4中的动态加载功能非常好用,之前做的一些DEMO都是直接加载了所需要的js文件。那么以后,我做的DEMO将直接引用extjs主文件,并使用动态加载来完成DEMO的运行。注意:由于使用的是动态加载,在运行DEMO之前,请配置好你的IIS或者apache,切勿直接双击html运行DEMO,至于为什么要配置运行环境,简单的来说,本地js的权限不够,具体的原因,请百度或者谷歌(推荐谷歌)。


1、目录结构:根目录放置了ext-all-debug.js、resources文件夹(里面是extjs的css文件)、ux文件夹(插件目录)、demo文件夹(各种demo)如下图:


2、CheckCombo.js:

  1. Ext.define('Ext.ux.CheckCombo',{
  2. extend: 'Ext.form.field.ComboBox',
  3. alias: 'widget.checkcombo',
  4. multiSelect: true,
  5. allSelector: false,
  6. addAllSelector: false,
  7. allText: 'All',
  8. createPicker: function() {
  9. var me = this,
  10. picker,
  11. menuCls = Ext.baseCSSPrefix + 'menu',
  12. opts = Ext.apply({
  13. pickerField: me,
  14. selModel: {
  15. mode: me.multiSelect ? 'SIMPLE' : 'SINGLE'
  16. },
  17. floating: true,
  18. hidden: true,
  19. ownerCt: me.ownerCt,
  20. cls: me.el.up('.' + menuCls) ? menuCls : '',
  21. store: me.store,
  22. displayField: me.displayField,
  23. focusOnToFront: false,
  24. pageSize: me.pageSize,
  25. tpl:
  26. [
  27. '<ul><tpl for=".">',
  28. '<li role="option" class="' + Ext.baseCSSPrefix + 'boundlist-item"><span class="x-combo-checker"> </span> {' + me.displayField + '}</li>',
  29. '</tpl></ul>'
  30. ]
  31. }, me.listConfig, me.defaultListConfig);
  32. picker = me.picker = Ext.create('Ext.view.BoundList', opts);
  33. if (me.pageSize) {
  34. picker.pagingToolbar.on('beforechange', me.onPageChange, me);
  35. }
  36. me.mon(picker, {
  37. itemclick: me.onItemClick,
  38. refresh: me.onListRefresh,
  39. scope: me
  40. });
  41. me.mon(picker.getSelectionModel(), {
  42. 'beforeselect': me.onBeforeSelect,
  43. 'beforedeselect': me.onBeforeDeselect,
  44. 'selectionchange': me.onListSelectionChange,
  45. scope: me
  46. });
  47. return picker;
  48. },
  49. getValue: function()
  50. {
  51. return this.value.join(',');
  52. },
  53. getSubmitValue: function()
  54. {
  55. return this.getValue();
  56. },
  57. expand: function()
  58. {
  59. var me = this,
  60. bodyEl, picker, collapseIf;
  61. if (me.rendered && !me.isExpanded && !me.isDestroyed) {
  62. bodyEl = me.bodyEl;
  63. picker = me.getPicker();
  64. collapseIf = me.collapseIf;
  65. //显示
  66. picker.show();
  67. me.isExpanded = true;
  68. me.alignPicker();
  69. bodyEl.addCls(me.openCls);
  70. if(me.addAllSelector == true && me.allSelector == false)
  71. {
  72. me.allSelector = picker.getEl().insertHtml('afterBegin', '<div class="x-boundlist-item" role="option"><span class="x-combo-checker"> </span> '+me.allText+'</div>', true);
  73. me.allSelector.on('click', function(e)
  74. {
  75. if(me.allSelector.hasCls('x-boundlist-selected'))
  76. {
  77. me.allSelector.removeCls('x-boundlist-selected');
  78. me.setValue('');
  79. me.fireEvent('select', me, []);
  80. }
  81. else
  82. {
  83. var records = [];
  84. me.store.each(function(record)
  85. {
  86. records.push(record);
  87. });
  88. me.allSelector.addCls('x-boundlist-selected');
  89. me.select(records);
  90. me.fireEvent('select', me, records);
  91. }
  92. });
  93. }
  94. // 监听
  95. me.mon(Ext.getDoc(), {
  96. mousewheel: collapseIf,
  97. mousedown: collapseIf,
  98. scope: me
  99. });
  100. Ext.EventManager.onWindowResize(me.alignPicker, me);
  101. me.fireEvent('expand', me);
  102. me.onExpand();
  103. }
  104. },
  105. onListSelectionChange: function(list, selectedRecords)
  106. {
  107. var me = this,
  108. isMulti = me.multiSelect,
  109. hasRecords = selectedRecords.length > 0;

  110. if (me.isExpanded) {
  111. if (!isMulti) {
  112. Ext.defer(me.collapse, 1, me);
  113. }


  114. if (isMulti || hasRecords) {
  115. me.setValue(selectedRecords, false);
  116. }
  117. if (hasRecords) {
  118. me.fireEvent('select', me, selectedRecords);
  119. }
  120. me.inputEl.focus();
  121. }

  122. if(me.addAllSelector == true && me.allSelector != false)
  123. {
  124. if(selectedRecords.length == me.store.getTotalCount()) me.allSelector.addCls('x-boundlist-selected');
  125. else me.allSelector.removeCls('x-boundlist-selected');
  126. }
  127. }
  128. });
  129. 3、checkbox-combobox.html:

  130. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  131. <html xmlns="http://www.w3.org/1999/xhtml">
  132. <head>
  133. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  134. <title>MHZG.NET-extjs4.1带复选框的下拉框</title>
  135. <link rel="stylesheet" type="text/css" href="/resources/css/ext-all.css" />
  136. <script type="text/javascript" src="/ext-all-debug.js"></script>
  137. <script type="text/javascript" src="/locale/ext-lang-zh_CN.js"></script>
  138. <style type="text/css">
  139. body{ padding:10px;}
  140. .x-combo-checker { background-position: 50% -2px; margin-left: 1px; background-color: transparent; background-image: url("/resources/themes/images/default/grid/unchecked.gif"); background-position: -1px -1px; background-repeat: no-repeat; height: 14px; width: 14px; display: inline-block; }
  141. .x-boundlist-selected .x-combo-checker { background-image: url("/resources/themes/images/default/grid/checked.gif"); }

  142. </style>
  143. <script type="text/javascript">
  144. Ext.Loader.setConfig({ enabled: true });
  145. Ext.Loader.setPath('Ext.ux', '/ux');
  146. var store = Ext.create('Ext.data.Store',{
  147. fields: ['id', 'type'],
  148. data: [
  149. {id: '1', type: '选项一 extjs4.1'},
  150. {id: '2', type: '选项二 extjs4'},
  151. {id: '3', type: '选项三 extjs 4.1 下拉复选框'},
  152. {id: '4', type: '选项四 mhzg.net'},
  153. {id: '5', type: '选项五 mhzg extjs4'},
  154. {id: '6', type: '选项六 mhzg.net extjs 4.1'},
  155. {id: '7', type: '选项七 exjts4.x'}
  156. ]
  157. });
  158. Ext.onReady(function(){
  159. //没有全选功能的CheckCombo
  160. Ext.create('Ext.ux.CheckCombo',{
  161. renderTo: 'cbo',
  162. width:180,
  163. id:'box1',
  164. valueField: 'id',
  165. displayField: 'type',
  166. store: store
  167. });
  168. //带全选功能的CheckCombo
  169. Ext.create('Ext.ux.CheckCombo',{
  170. renderTo: 'cba',
  171. valueField: 'id',
  172. displayField: 'type',
  173. id:'box2',
  174. width:180,
  175. allText:'全选',//默认字符是All
  176. store: store,
  177. addAllSelector: true
  178. });
  179. })
  180. function getValues(v){
  181. var data = Ext.getCmp(v).getValue();
  182. alert(data);
  183. }
  184. </script>
  185. </head>
复制代码

extjs 4.1 没有全选功能的下拉复选框<br /><br />
  1. <div id="cbo"></div><input type="button" onclick="getValues('box1');" value="获取值" /><br /><br />-------------------------------------分割线-------------------------------------<br /><br />extjs 4.1 下拉复选框,并带全选功能<br /><br /><div id="cba"></div><input type="button" onclick="getValues('box2');" value="获取值" /><body></body></html>
复制代码

最后,需要注意的是,无论我们如何组建目录结构,只要我们将目录结构顺序弄明白了,一切都好解决!


原文链接:http://www.mhzg.net/a/20126/20126513220624.html

你可能感兴趣的:(EXTJS下拉复选框)