CheckboxGroup2.html 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title>The source code</title>
  6. <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
  7. <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
  8. <style type="text/css">
  9. .highlight { display: block; background-color: #ddd; }
  10. </style>
  11. <script type="text/javascript">
  12. function highlight() {
  13. document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
  14. }
  15. </script>
  16. </head>
  17. <body onload="prettyPrint(); highlight();">
  18. <pre class="prettyprint lang-js"><span id='Ext-form-CheckboxGroup'>/**
  19. </span> * A {@link Ext.form.FieldContainer field container} which has a specialized layout for arranging
  20. * {@link Ext.form.field.Checkbox} controls into columns, and provides convenience
  21. * {@link Ext.form.field.Field} methods for {@link #getValue getting}, {@link #setValue setting},
  22. * and {@link #validate validating} the group of checkboxes as a whole.
  23. *
  24. * # Validation
  25. *
  26. * Individual checkbox fields themselves have no default validation behavior, but
  27. * sometimes you want to require a user to select at least one of a group of checkboxes. CheckboxGroup
  28. * allows this by setting the config `{@link #allowBlank}:false`; when the user does not check at
  29. * least one of the checkboxes, the entire group will be highlighted as invalid and the
  30. * {@link #blankText error message} will be displayed according to the {@link #msgTarget} config.
  31. *
  32. * # Layout
  33. *
  34. * The default layout for CheckboxGroup makes it easy to arrange the checkboxes into
  35. * columns; see the {@link #columns} and {@link #vertical} config documentation for details. You may also
  36. * use a completely different layout by setting the {@link #layout} to one of the other supported layout
  37. * types; for instance you may wish to use a custom arrangement of hbox and vbox containers. In that case
  38. * the checkbox components at any depth will still be managed by the CheckboxGroup's validation.
  39. *
  40. * @example
  41. * Ext.create('Ext.form.Panel', {
  42. * title: 'Checkbox Group',
  43. * width: 300,
  44. * height: 125,
  45. * bodyPadding: 10,
  46. * renderTo: Ext.getBody(),
  47. * items:[{
  48. * xtype: 'checkboxgroup',
  49. * fieldLabel: 'Two Columns',
  50. * // Arrange checkboxes into two columns, distributed vertically
  51. * columns: 2,
  52. * vertical: true,
  53. * items: [
  54. * { boxLabel: 'Item 1', name: 'rb', inputValue: '1' },
  55. * { boxLabel: 'Item 2', name: 'rb', inputValue: '2', checked: true },
  56. * { boxLabel: 'Item 3', name: 'rb', inputValue: '3' },
  57. * { boxLabel: 'Item 4', name: 'rb', inputValue: '4' },
  58. * { boxLabel: 'Item 5', name: 'rb', inputValue: '5' },
  59. * { boxLabel: 'Item 6', name: 'rb', inputValue: '6' }
  60. * ]
  61. * }]
  62. * });
  63. */
  64. Ext.define('Ext.form.CheckboxGroup', {
  65. extend:'Ext.form.FieldContainer',
  66. mixins: {
  67. field: 'Ext.form.field.Field'
  68. },
  69. alias: 'widget.checkboxgroup',
  70. requires: ['Ext.layout.container.CheckboxGroup', 'Ext.form.field.Base'],
  71. <span id='Ext-form-CheckboxGroup-cfg-name'> /**
  72. </span> * @cfg {String} name
  73. * @private
  74. */
  75. <span id='Ext-form-CheckboxGroup-cfg-items'> /**
  76. </span> * @cfg {Ext.form.field.Checkbox[]/Object[]} items
  77. * An Array of {@link Ext.form.field.Checkbox Checkbox}es or Checkbox config objects to arrange in the group.
  78. */
  79. <span id='Ext-form-CheckboxGroup-cfg-columns'> /**
  80. </span> * @cfg {String/Number/Number[]} columns
  81. * Specifies the number of columns to use when displaying grouped checkbox/radio controls using automatic layout.
  82. * This config can take several types of values:
  83. *
  84. * - 'auto' - The controls will be rendered one per column on one row and the width of each column will be evenly
  85. * distributed based on the width of the overall field container. This is the default.
  86. * - Number - If you specific a number (e.g., 3) that number of columns will be created and the contained controls
  87. * will be automatically distributed based on the value of {@link #vertical}.
  88. * - Array - You can also specify an array of column widths, mixing integer (fixed width) and float (percentage
  89. * width) values as needed (e.g., [100, .25, .75]). Any integer values will be rendered first, then any float
  90. * values will be calculated as a percentage of the remaining space. Float values do not have to add up to 1
  91. * (100%) although if you want the controls to take up the entire field container you should do so.
  92. */
  93. columns : 'auto',
  94. <span id='Ext-form-CheckboxGroup-cfg-vertical'> /**
  95. </span> * @cfg {Boolean} vertical
  96. * True to distribute contained controls across columns, completely filling each column top to bottom before
  97. * starting on the next column. The number of controls in each column will be automatically calculated to keep
  98. * columns as even as possible. The default value is false, so that controls will be added to columns one at a time,
  99. * completely filling each row left to right before starting on the next row.
  100. */
  101. vertical : false,
  102. <span id='Ext-form-CheckboxGroup-cfg-allowBlank'> /**
  103. </span> * @cfg {Boolean} allowBlank
  104. * False to validate that at least one item in the group is checked. If no items are selected at
  105. * validation time, {@link #blankText} will be used as the error text.
  106. */
  107. allowBlank : true,
  108. //&lt;locale&gt;
  109. <span id='Ext-form-CheckboxGroup-cfg-blankText'> /**
  110. </span> * @cfg {String} blankText
  111. * Error text to display if the {@link #allowBlank} validation fails
  112. */
  113. blankText : &quot;You must select at least one item in this group&quot;,
  114. //&lt;/locale&gt;
  115. // private
  116. defaultType : 'checkboxfield',
  117. // private
  118. groupCls : Ext.baseCSSPrefix + 'form-check-group',
  119. <span id='Ext-form-CheckboxGroup-cfg-fieldBodyCls'> /**
  120. </span> * @cfg {String} [fieldBodyCls='x-form-checkboxgroup-body']
  121. * An extra CSS class to be applied to the body content element in addition to {@link #baseBodyCls}.
  122. */
  123. fieldBodyCls: Ext.baseCSSPrefix + 'form-checkboxgroup-body',
  124. // private
  125. layout: 'checkboxgroup',
  126. initComponent: function() {
  127. var me = this;
  128. me.callParent();
  129. me.initField();
  130. },
  131. <span id='Ext-form-CheckboxGroup-method-initValue'> /**
  132. </span> * Initializes the field's value based on the initial config. If the {@link #value} config is specified then we use
  133. * that to set the value; otherwise we initialize the originalValue by querying the values of all sub-checkboxes
  134. * after they have been initialized.
  135. * @protected
  136. */
  137. initValue: function() {
  138. var me = this,
  139. valueCfg = me.value;
  140. me.originalValue = me.lastValue = valueCfg || me.getValue();
  141. if (valueCfg) {
  142. me.setValue(valueCfg);
  143. }
  144. },
  145. <span id='Ext-form-CheckboxGroup-method-onFieldAdded'> /**
  146. </span> * When a checkbox is added to the group, monitor it for changes
  147. * @param {Object} field
  148. * @protected
  149. */
  150. onFieldAdded: function(field) {
  151. var me = this;
  152. if (field.isCheckbox) {
  153. me.mon(field, 'change', me.checkChange, me);
  154. }
  155. me.callParent(arguments);
  156. },
  157. onFieldRemoved: function(field) {
  158. var me = this;
  159. if (field.isCheckbox) {
  160. me.mun(field, 'change', me.checkChange, me);
  161. }
  162. me.callParent(arguments);
  163. },
  164. // private override - the group value is a complex object, compare using object serialization
  165. isEqual: function(value1, value2) {
  166. var toQueryString = Ext.Object.toQueryString;
  167. return toQueryString(value1) === toQueryString(value2);
  168. },
  169. <span id='Ext-form-CheckboxGroup-method-getErrors'> /**
  170. </span> * Runs CheckboxGroup's validations and returns an array of any errors. The only error by default is if allowBlank
  171. * is set to true and no items are checked.
  172. * @return {String[]} Array of all validation errors
  173. */
  174. getErrors: function() {
  175. var errors = [];
  176. if (!this.allowBlank &amp;&amp; Ext.isEmpty(this.getChecked())) {
  177. errors.push(this.blankText);
  178. }
  179. return errors;
  180. },
  181. <span id='Ext-form-CheckboxGroup-method-getBoxes'> /**
  182. </span> * @private Returns all checkbox components within the container
  183. * @param {String} [query] An additional query to add to the selector.
  184. */
  185. getBoxes: function(query) {
  186. return this.query('[isCheckbox]' + (query||''));
  187. },
  188. <span id='Ext-form-CheckboxGroup-method-eachBox'> /**
  189. </span> * @private Convenience function which calls the given function for every checkbox in the group
  190. * @param {Function} fn The function to call
  191. * @param {Object} [scope] scope object
  192. */
  193. eachBox: function(fn, scope) {
  194. Ext.Array.forEach(this.getBoxes(), fn, scope || this);
  195. },
  196. <span id='Ext-form-CheckboxGroup-method-getChecked'> /**
  197. </span> * Returns an Array of all checkboxes in the container which are currently checked
  198. * @return {Ext.form.field.Checkbox[]} Array of Ext.form.field.Checkbox components
  199. */
  200. getChecked: function() {
  201. return this.getBoxes('[checked]');
  202. },
  203. // private override
  204. isDirty: function(){
  205. var boxes = this.getBoxes(),
  206. b ,
  207. bLen = boxes.length;
  208. for (b = 0; b &lt; bLen; b++) {
  209. if (boxes[b].isDirty()) {
  210. return true;
  211. }
  212. }
  213. },
  214. // private override
  215. setReadOnly: function(readOnly) {
  216. var boxes = this.getBoxes(),
  217. b,
  218. bLen = boxes.length;
  219. for (b = 0; b &lt; bLen; b++) {
  220. boxes[b].setReadOnly(readOnly);
  221. }
  222. this.readOnly = readOnly;
  223. },
  224. <span id='Ext-form-CheckboxGroup-method-reset'> /**
  225. </span> * Resets the checked state of all {@link Ext.form.field.Checkbox checkboxes} in the group to their originally
  226. * loaded values and clears any validation messages.
  227. * See {@link Ext.form.Basic}.{@link Ext.form.Basic#trackResetOnLoad trackResetOnLoad}
  228. */
  229. reset: function() {
  230. var me = this,
  231. hadError = me.hasActiveError(),
  232. preventMark = me.preventMark;
  233. me.preventMark = true;
  234. me.batchChanges(function() {
  235. var boxes = me.getBoxes(),
  236. b,
  237. bLen = boxes.length;
  238. for (b = 0; b &lt; bLen; b++) {
  239. boxes[b].reset();
  240. }
  241. });
  242. me.preventMark = preventMark;
  243. me.unsetActiveError();
  244. if (hadError) {
  245. me.updateLayout();
  246. }
  247. },
  248. resetOriginalValue: function(){
  249. var me = this,
  250. boxes = me.getBoxes(),
  251. b,
  252. bLen = boxes.length;
  253. for (b = 0; b &lt; bLen; b++) {
  254. boxes[b].resetOriginalValue();
  255. }
  256. me.originalValue = me.getValue();
  257. me.checkDirty();
  258. },
  259. <span id='Ext-form-CheckboxGroup-method-setValue'> /**
  260. </span> * Sets the value(s) of all checkboxes in the group. The expected format is an Object of name-value pairs
  261. * corresponding to the names of the checkboxes in the group. Each pair can have either a single or multiple values:
  262. *
  263. * - A single Boolean or String value will be passed to the `setValue` method of the checkbox with that name.
  264. * See the rules in {@link Ext.form.field.Checkbox#setValue} for accepted values.
  265. * - An Array of String values will be matched against the {@link Ext.form.field.Checkbox#inputValue inputValue}
  266. * of checkboxes in the group with that name; those checkboxes whose inputValue exists in the array will be
  267. * checked and others will be unchecked.
  268. *
  269. * If a checkbox's name is not in the mapping at all, it will be unchecked.
  270. *
  271. * An example:
  272. *
  273. * var myCheckboxGroup = new Ext.form.CheckboxGroup({
  274. * columns: 3,
  275. * items: [{
  276. * name: 'cb1',
  277. * boxLabel: 'Single 1'
  278. * }, {
  279. * name: 'cb2',
  280. * boxLabel: 'Single 2'
  281. * }, {
  282. * name: 'cb3',
  283. * boxLabel: 'Single 3'
  284. * }, {
  285. * name: 'cbGroup',
  286. * boxLabel: 'Grouped 1'
  287. * inputValue: 'value1'
  288. * }, {
  289. * name: 'cbGroup',
  290. * boxLabel: 'Grouped 2'
  291. * inputValue: 'value2'
  292. * }, {
  293. * name: 'cbGroup',
  294. * boxLabel: 'Grouped 3'
  295. * inputValue: 'value3'
  296. * }]
  297. * });
  298. *
  299. * myCheckboxGroup.setValue({
  300. * cb1: true,
  301. * cb3: false,
  302. * cbGroup: ['value1', 'value3']
  303. * });
  304. *
  305. * The above code will cause the checkbox named 'cb1' to be checked, as well as the first and third checkboxes named
  306. * 'cbGroup'. The other three checkboxes will be unchecked.
  307. *
  308. * @param {Object} value The mapping of checkbox names to values.
  309. * @return {Ext.form.CheckboxGroup} this
  310. */
  311. setValue: function(value) {
  312. var me = this,
  313. boxes = me.getBoxes(),
  314. b,
  315. bLen = boxes.length,
  316. box, name,
  317. cbValue;
  318. me.batchChanges(function() {
  319. for (b = 0; b &lt; bLen; b++) {
  320. box = boxes[b];
  321. name = box.getName();
  322. cbValue = false;
  323. if (value &amp;&amp; value.hasOwnProperty(name)) {
  324. if (Ext.isArray(value[name])) {
  325. cbValue = Ext.Array.contains(value[name], box.inputValue);
  326. } else {
  327. // single value, let the checkbox's own setValue handle conversion
  328. cbValue = value[name];
  329. }
  330. }
  331. box.setValue(cbValue);
  332. }
  333. });
  334. return me;
  335. },
  336. <span id='Ext-form-CheckboxGroup-method-getValue'> /**
  337. </span> * Returns an object containing the values of all checked checkboxes within the group. Each key-value pair in the
  338. * object corresponds to a checkbox {@link Ext.form.field.Checkbox#name name}. If there is only one checked checkbox
  339. * with a particular name, the value of that pair will be the String {@link Ext.form.field.Checkbox#inputValue
  340. * inputValue} of that checkbox. If there are multiple checked checkboxes with that name, the value of that pair
  341. * will be an Array of the selected inputValues.
  342. *
  343. * The object format returned from this method can also be passed directly to the {@link #setValue} method.
  344. *
  345. * NOTE: In Ext 3, this method returned an array of Checkbox components; this was changed to make it more consistent
  346. * with other field components and with the {@link #setValue} argument signature. If you need the old behavior in
  347. * Ext 4+, use the {@link #getChecked} method instead.
  348. */
  349. getValue: function() {
  350. var values = {},
  351. boxes = this.getBoxes(),
  352. b,
  353. bLen = boxes.length,
  354. box, name, inputValue, bucket;
  355. for (b = 0; b &lt; bLen; b++) {
  356. box = boxes[b];
  357. name = box.getName();
  358. inputValue = box.inputValue;
  359. if (box.getValue()) {
  360. if (values.hasOwnProperty(name)) {
  361. bucket = values[name];
  362. if (!Ext.isArray(bucket)) {
  363. bucket = values[name] = [bucket];
  364. }
  365. bucket.push(inputValue);
  366. } else {
  367. values[name] = inputValue;
  368. }
  369. }
  370. }
  371. return values;
  372. },
  373. /*
  374. * Don't return any data for submit; the form will get the info from the individual checkboxes themselves.
  375. */
  376. getSubmitData: function() {
  377. return null;
  378. },
  379. /*
  380. * Don't return any data for the model; the form will get the info from the individual checkboxes themselves.
  381. */
  382. getModelData: function() {
  383. return null;
  384. },
  385. validate: function() {
  386. var me = this,
  387. errors,
  388. isValid,
  389. wasValid;
  390. if (me.disabled) {
  391. isValid = true;
  392. } else {
  393. errors = me.getErrors();
  394. isValid = Ext.isEmpty(errors);
  395. wasValid = !me.hasActiveError();
  396. if (isValid) {
  397. me.unsetActiveError();
  398. } else {
  399. me.setActiveError(errors);
  400. }
  401. }
  402. if (isValid !== wasValid) {
  403. me.fireEvent('validitychange', me, isValid);
  404. me.updateLayout();
  405. }
  406. return isValid;
  407. }
  408. }, function() {
  409. this.borrow(Ext.form.field.Base, ['markInvalid', 'clearInvalid']);
  410. });
  411. </pre>
  412. </body>
  413. </html>