check-radio.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. Ext.require([
  2. 'Ext.form.*',
  3. 'Ext.layout.container.Column',
  4. 'Ext.window.MessageBox',
  5. 'Ext.fx.target.Element'
  6. ]);
  7. Ext.onReady(function(){
  8. /*====================================================================
  9. * Individual checkbox/radio examples
  10. *====================================================================*/
  11. // Using checkbox/radio groups will generally be more convenient and flexible than
  12. // using individual checkbox and radio controls, but this shows that you can
  13. // certainly do so if you only have a single control at a time.
  14. var individual = {
  15. xtype: 'container',
  16. layout: 'hbox',
  17. margin: '0 0 10',
  18. items: [{
  19. xtype: 'fieldset',
  20. flex: 1,
  21. title: 'Individual Checkboxes',
  22. defaultType: 'checkbox', // each item will be a checkbox
  23. layout: 'anchor',
  24. defaults: {
  25. anchor: '100%',
  26. hideEmptyLabel: false
  27. },
  28. items: [{
  29. xtype: 'textfield',
  30. name: 'txt-test1',
  31. fieldLabel: 'Alignment Test'
  32. }, {
  33. fieldLabel: 'Favorite Animals',
  34. boxLabel: 'Dog',
  35. name: 'fav-animal-dog',
  36. inputValue: 'dog'
  37. }, {
  38. boxLabel: 'Cat',
  39. name: 'fav-animal-cat',
  40. inputValue: 'cat'
  41. }, {
  42. checked: true,
  43. boxLabel: 'Monkey',
  44. name: 'fav-animal-monkey',
  45. inputValue: 'monkey'
  46. }]
  47. }, {
  48. xtype: 'component',
  49. width: 10
  50. }, {
  51. xtype: 'fieldset',
  52. flex: 1,
  53. title: 'Individual Radios',
  54. defaultType: 'radio', // each item will be a radio button
  55. layout: 'anchor',
  56. defaults: {
  57. anchor: '100%',
  58. hideEmptyLabel: false
  59. },
  60. items: [{
  61. xtype: 'textfield',
  62. name: 'txt-test2',
  63. fieldLabel: 'Alignment Test'
  64. }, {
  65. checked: true,
  66. fieldLabel: 'Favorite Color',
  67. boxLabel: 'Red',
  68. name: 'fav-color',
  69. inputValue: 'red'
  70. }, {
  71. boxLabel: 'Blue',
  72. name: 'fav-color',
  73. inputValue: 'blue'
  74. }, {
  75. boxLabel: 'Green',
  76. name: 'fav-color',
  77. inputValue: 'green'
  78. }]
  79. }]
  80. };
  81. /*====================================================================
  82. * CheckGroup example
  83. *====================================================================*/
  84. var checkGroup = {
  85. xtype: 'fieldset',
  86. title: 'Checkbox Groups (initially collapsed)',
  87. layout: 'anchor',
  88. defaults: {
  89. anchor: '100%',
  90. labelStyle: 'padding-left:4px;'
  91. },
  92. collapsible: true,
  93. collapsed: true,
  94. items: [{
  95. xtype: 'textfield',
  96. name: 'txt-test3',
  97. fieldLabel: 'Alignment Test'
  98. },{
  99. // Use the default, automatic layout to distribute the controls evenly
  100. // across a single row
  101. xtype: 'checkboxgroup',
  102. fieldLabel: 'Auto Layout',
  103. cls: 'x-check-group-alt',
  104. items: [
  105. {boxLabel: 'Item 1', name: 'cb-auto-1'},
  106. {boxLabel: 'Item 2', name: 'cb-auto-2', checked: true},
  107. {boxLabel: 'Item 3', name: 'cb-auto-3'},
  108. {boxLabel: 'Item 4', name: 'cb-auto-4'},
  109. {boxLabel: 'Item 5', name: 'cb-auto-5'}
  110. ]
  111. },{
  112. xtype: 'checkboxgroup',
  113. fieldLabel: 'Single Column',
  114. // Put all controls in a single column with width 100%
  115. columns: 1,
  116. items: [
  117. {boxLabel: 'Item 1', name: 'cb-col-1'},
  118. {boxLabel: 'Item 2', name: 'cb-col-2', checked: true},
  119. {boxLabel: 'Item 3', name: 'cb-col-3'}
  120. ]
  121. },{
  122. xtype: 'checkboxgroup',
  123. fieldLabel: 'Multi-Column (horizontal)',
  124. cls: 'x-check-group-alt',
  125. // Distribute controls across 3 even columns, filling each row
  126. // from left to right before starting the next row
  127. columns: 3,
  128. items: [
  129. {boxLabel: 'Item 1', name: 'cb-horiz-1'},
  130. {boxLabel: 'Item 2', name: 'cb-horiz-2', checked: true},
  131. {boxLabel: 'Item 3', name: 'cb-horiz-3'},
  132. {boxLabel: 'Item 4', name: 'cb-horiz-4'},
  133. {boxLabel: 'Item 5', name: 'cb-horiz-5'}
  134. ]
  135. },{
  136. xtype: 'checkboxgroup',
  137. fieldLabel: 'Multi-Column (vertical)',
  138. // Distribute controls across 3 even columns, filling each column
  139. // from top to bottom before starting the next column
  140. columns: 3,
  141. vertical: true,
  142. items: [
  143. {boxLabel: 'Item 1', name: 'cb-vert-1'},
  144. {boxLabel: 'Item 2', name: 'cb-vert-2', checked: true},
  145. {boxLabel: 'Item 3', name: 'cb-vert-3'},
  146. {boxLabel: 'Item 4', name: 'cb-vert-4'},
  147. {boxLabel: 'Item 5', name: 'cb-vert-5'}
  148. ]
  149. },{
  150. xtype: 'checkboxgroup',
  151. fieldLabel: 'Multi-Column<br />(custom widths)',
  152. cls: 'x-check-group-alt',
  153. // Specify exact column widths (could also include float values for %)
  154. columns: [100, 100],
  155. vertical: true,
  156. items: [
  157. {boxLabel: 'Item 1', name: 'cb-custwidth', inputValue: 1},
  158. {boxLabel: 'Item 2', name: 'cb-custwidth', inputValue: 2, checked: true},
  159. {boxLabel: 'Item 3', name: 'cb-custwidth', inputValue: 3},
  160. {boxLabel: 'Item 4', name: 'cb-custwidth', inputValue: 4},
  161. {boxLabel: 'Item 5', name: 'cb-custwidth', inputValue: 5}
  162. ]
  163. },{
  164. xtype: 'checkboxgroup',
  165. fieldLabel: 'Custom Layout<br />(w/ validation)',
  166. allowBlank: false,
  167. msgTarget: 'side',
  168. autoFitErrors: false,
  169. anchor: '-18',
  170. // You can change the 'layout' to anything you want, and include any nested
  171. // container structure, for complete layout control. In this example we only
  172. // want one item in the middle column, which would not be possible using the
  173. // default 'checkboxgroup' layout's columns config. We also want to put
  174. // headings at the top of each column.
  175. layout: 'column',
  176. defaultType: 'container',
  177. items: [{
  178. columnWidth: .25,
  179. items: [
  180. {xtype: 'component', html: 'Heading 1', cls:'x-form-check-group-label'},
  181. {xtype: 'checkboxfield', boxLabel: 'Item 1', name: 'cb-cust-1'},
  182. {xtype: 'checkboxfield', boxLabel: 'Item 2', name: 'cb-cust-2'}
  183. ]
  184. },{
  185. columnWidth: .5,
  186. items: [
  187. {xtype: 'component', html: 'Heading 2', cls:'x-form-check-group-label'},
  188. {xtype: 'checkboxfield', boxLabel: 'A long item just for fun', name: 'cb-cust-3'}
  189. ]
  190. },{
  191. columnWidth: .25,
  192. items: [
  193. {xtype: 'component', html: 'Heading 3', cls:'x-form-check-group-label'},
  194. {xtype: 'checkboxfield', boxLabel: 'Item 4', name: 'cb-cust-4'},
  195. {xtype: 'checkboxfield', boxLabel: 'Item 5', name: 'cb-cust-5'}
  196. ]
  197. }]
  198. }]
  199. };
  200. /*====================================================================
  201. * RadioGroup examples
  202. *====================================================================*/
  203. // NOTE: These radio examples use the exact same options as the checkbox ones
  204. // above, so the comments will not be repeated. Please see comments above for
  205. // additional explanation on some config options.
  206. var radioGroup = {
  207. xtype: 'fieldset',
  208. title: 'Radio Groups',
  209. // in this section we use the form layout that will aggregate all of the fields
  210. // into a single table, rather than one table per field.
  211. layout: 'form',
  212. defaults: {
  213. labelStyle: 'padding-left:4px;'
  214. },
  215. collapsible: true,
  216. items: [{
  217. xtype: 'textfield',
  218. name: 'txt-test4',
  219. fieldLabel: 'Alignment Test'
  220. },{
  221. xtype: 'radiogroup',
  222. fieldLabel: 'Auto Layout',
  223. cls: 'x-check-group-alt',
  224. items: [
  225. {boxLabel: 'Item 1', name: 'rb-auto', inputValue: 1},
  226. {boxLabel: 'Item 2', name: 'rb-auto', inputValue: 2, checked: true},
  227. {boxLabel: 'Item 3', name: 'rb-auto', inputValue: 3},
  228. {boxLabel: 'Item 4', name: 'rb-auto', inputValue: 4},
  229. {boxLabel: 'Item 5', name: 'rb-auto', inputValue: 5}
  230. ]
  231. },{
  232. xtype: 'radiogroup',
  233. fieldLabel: 'Single Column',
  234. columns: 1,
  235. items: [
  236. {boxLabel: 'Item 1', name: 'rb-col', inputValue: 1},
  237. {boxLabel: 'Item 2', name: 'rb-col', inputValue: 2, checked: true},
  238. {boxLabel: 'Item 3', name: 'rb-col', inputValue: 3}
  239. ]
  240. },{
  241. xtype: 'radiogroup',
  242. fieldLabel: 'Multi-Column (horizontal)',
  243. cls: 'x-check-group-alt',
  244. columns: 3,
  245. items: [
  246. {boxLabel: 'Item 1', name: 'rb-horiz-1', inputValue: 1},
  247. {boxLabel: 'Item 2', name: 'rb-horiz-1', inputValue: 2, checked: true},
  248. {boxLabel: 'Item 3', name: 'rb-horiz-1', inputValue: 3},
  249. {boxLabel: 'Item 1', name: 'rb-horiz-2', inputValue: 1, checked: true},
  250. {boxLabel: 'Item 2', name: 'rb-horiz-2', inputValue: 2}
  251. ]
  252. },{
  253. xtype: 'radiogroup',
  254. fieldLabel: 'Multi-Column (vertical)',
  255. columns: 3,
  256. vertical: true,
  257. items: [
  258. {boxLabel: 'Item 1', name: 'rb-vert', inputValue: 1},
  259. {boxLabel: 'Item 2', name: 'rb-vert', inputValue: 2, checked: true},
  260. {boxLabel: 'Item 3', name: 'rb-vert', inputValue: 3},
  261. {boxLabel: 'Item 4', name: 'rb-vert', inputValue: 4},
  262. {boxLabel: 'Item 5', name: 'rb-vert', inputValue: 5}
  263. ]
  264. },{
  265. xtype: 'radiogroup',
  266. fieldLabel: 'Multi-Column<br />(custom widths)',
  267. cls: 'x-check-group-alt',
  268. columns: [100, 100],
  269. vertical: true,
  270. items: [
  271. {boxLabel: 'Item 1', name: 'rb-custwidth', inputValue: 1},
  272. {boxLabel: 'Item 2', name: 'rb-custwidth', inputValue: 2, checked: true},
  273. {boxLabel: 'Item 3', name: 'rb-custwidth', inputValue: 3},
  274. {boxLabel: 'Item 4', name: 'rb-custwidth', inputValue: 4},
  275. {boxLabel: 'Item 5', name: 'rb-custwidth', inputValue: 5}
  276. ]
  277. },{
  278. xtype: 'radiogroup',
  279. fieldLabel: 'Custom Layout<br />(w/ validation)',
  280. allowBlank: false,
  281. msgTarget: 'side',
  282. autoFitErrors: false,
  283. anchor: '-18',
  284. layout: 'column',
  285. defaultType: 'container',
  286. items: [{
  287. columnWidth: .25,
  288. items: [
  289. {xtype: 'component', html: 'Heading 1', cls:'x-form-check-group-label'},
  290. {xtype: 'radiofield', boxLabel: 'Item 1', name: 'rb-cust', inputValue: 1},
  291. {xtype: 'radiofield', boxLabel: 'Item 2', name: 'rb-cust', inputValue: 2}
  292. ]
  293. },{
  294. columnWidth: .5,
  295. items: [
  296. {xtype: 'component', html: 'Heading 2', cls:'x-form-check-group-label'},
  297. {xtype: 'radiofield', boxLabel: 'A long item just for fun', name: 'rb-cust', inputValue: 3}
  298. ]
  299. },{
  300. columnWidth: .25,
  301. items: [
  302. {xtype: 'component', html: 'Heading 3', cls:'x-form-check-group-label'},
  303. {xtype: 'radiofield', boxLabel: 'Item 4', name: 'rb-cust', inputValue: 4},
  304. {xtype: 'radiofield', boxLabel: 'Item 5', name: 'rb-cust', inputValue: 5}
  305. ]
  306. }]
  307. }]
  308. };
  309. // combine all that into one huge form
  310. var fp = Ext.create('Ext.FormPanel', {
  311. title: 'Check/Radio Groups Example',
  312. frame: true,
  313. fieldDefaults: {
  314. labelWidth: 110
  315. },
  316. width: 600,
  317. renderTo:'form-ct',
  318. bodyPadding: 10,
  319. items: [
  320. individual,
  321. checkGroup,
  322. radioGroup
  323. ],
  324. buttons: [{
  325. text: 'Save',
  326. handler: function(){
  327. if(fp.getForm().isValid()){
  328. Ext.Msg.alert('Submitted Values', 'The following will be sent to the server: <br />'+
  329. fp.getForm().getValues(true).replace(/&/g,', '));
  330. }
  331. }
  332. },{
  333. text: 'Reset',
  334. handler: function(){
  335. fp.getForm().reset();
  336. }
  337. }]
  338. });
  339. });