RadioGroup.html 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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-RadioGroup'>/**
  19. </span> * A {@link Ext.form.FieldContainer field container} which has a specialized layout for arranging
  20. * {@link Ext.form.field.Radio} controls into columns, and provides convenience {@link Ext.form.field.Field}
  21. * methods for {@link #getValue getting}, {@link #setValue setting}, and {@link #validate validating} the
  22. * group of radio buttons as a whole.
  23. *
  24. * # Validation
  25. *
  26. * Individual radio buttons themselves have no default validation behavior, but
  27. * sometimes you want to require a user to select one of a group of radios. RadioGroup
  28. * allows this by setting the config `{@link #allowBlank}:false`; when the user does not check at
  29. * one of the radio buttons, 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 RadioGroup makes it easy to arrange the radio buttons 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 Radio components at any depth will still be managed by the RadioGroup's validation.
  39. *
  40. * # Example usage
  41. *
  42. * @example
  43. * Ext.create('Ext.form.Panel', {
  44. * title: 'RadioGroup Example',
  45. * width: 300,
  46. * height: 125,
  47. * bodyPadding: 10,
  48. * renderTo: Ext.getBody(),
  49. * items:[{
  50. * xtype: 'radiogroup',
  51. * fieldLabel: 'Two Columns',
  52. * // Arrange radio buttons into two columns, distributed vertically
  53. * columns: 2,
  54. * vertical: true,
  55. * items: [
  56. * { boxLabel: 'Item 1', name: 'rb', inputValue: '1' },
  57. * { boxLabel: 'Item 2', name: 'rb', inputValue: '2', checked: true},
  58. * { boxLabel: 'Item 3', name: 'rb', inputValue: '3' },
  59. * { boxLabel: 'Item 4', name: 'rb', inputValue: '4' },
  60. * { boxLabel: 'Item 5', name: 'rb', inputValue: '5' },
  61. * { boxLabel: 'Item 6', name: 'rb', inputValue: '6' }
  62. * ]
  63. * }]
  64. * });
  65. *
  66. */
  67. Ext.define('Ext.form.RadioGroup', {
  68. extend: 'Ext.form.CheckboxGroup',
  69. alias: 'widget.radiogroup',
  70. <span id='Ext-form-RadioGroup-cfg-items'> /**
  71. </span> * @cfg {Ext.form.field.Radio[]/Object[]} items
  72. * An Array of {@link Ext.form.field.Radio Radio}s or Radio config objects to arrange in the group.
  73. */
  74. <span id='Ext-form-RadioGroup-cfg-allowBlank'> /**
  75. </span> * @cfg {Boolean} allowBlank
  76. * True to allow every item in the group to be blank.
  77. * If allowBlank = false and no items are selected at validation time, {@link #blankText} will
  78. * be used as the error text.
  79. */
  80. allowBlank : true,
  81. //&lt;locale&gt;
  82. <span id='Ext-form-RadioGroup-cfg-blankText'> /**
  83. </span> * @cfg {String} blankText
  84. * Error text to display if the {@link #allowBlank} validation fails
  85. */
  86. blankText : 'You must select one item in this group',
  87. //&lt;/locale&gt;
  88. // private
  89. defaultType : 'radiofield',
  90. // private
  91. groupCls : Ext.baseCSSPrefix + 'form-radio-group',
  92. getBoxes: function(query) {
  93. return this.query('[isRadio]' + (query||''));
  94. },
  95. checkChange: function() {
  96. var value = this.getValue(),
  97. key = Ext.Object.getKeys(value)[0];
  98. // If the value is an array we skip out here because it's during a change
  99. // between multiple items, so we never want to fire a change
  100. if (Ext.isArray(value[key])) {
  101. return;
  102. }
  103. this.callParent(arguments);
  104. },
  105. <span id='Ext-form-RadioGroup-method-setValue'> /**
  106. </span> * Sets the value of the radio group. The radio with corresponding name and value will be set.
  107. * This method is simpler than {@link Ext.form.CheckboxGroup#setValue} because only 1 value is allowed
  108. * for each name.
  109. *
  110. * @param {Object} value The map from names to values to be set.
  111. * @return {Ext.form.CheckboxGroup} this
  112. */
  113. setValue: function(value) {
  114. var cbValue, first, formId, radios,
  115. i, len, name;
  116. if (Ext.isObject(value)) {
  117. for (name in value) {
  118. if (value.hasOwnProperty(name)) {
  119. cbValue = value[name];
  120. first = this.items.first();
  121. formId = first ? first.getFormId() : null;
  122. radios = Ext.form.RadioManager.getWithValue(name, cbValue, formId).items;
  123. len = radios.length;
  124. for (i = 0; i &lt; len; ++i) {
  125. radios[i].setValue(true);
  126. }
  127. }
  128. }
  129. }
  130. return this;
  131. }
  132. });
  133. </pre>
  134. </body>
  135. </html>