Checkbox与RadioGroup的使用方法

没错!是CheckboxRadioGroup而不是CheckboxRadio

Ex提供的CheckboxRadio,在控件同名成组时,例如将性别的男与女两个Radio控件都使用sex作为名称时,findField方法只能获取第一个控件,setValue也只能设置第一个控件,这样就造成了如果要使用FormLoad方式加载编辑数据或者用SetValues加载编辑数据时出现问题。要解决这个问题,第一种方法是定义名称不同的控件;第二种方法是控件名称相同,在加载编辑数据时手动处理控件状态。第一种方法对Checkbox问题不是太大,因为各Checkbox之间没关联。但是Radio就不同,彼此间是关联,例如性别选择男了,那女须为未选择状态,这个在名称相同时,会自动处理,不需要写多余代码,但是名称不同,则要通过check事件去修改其它控件的状态。第二种方法存在问题是,Checkbox同名,要获取第一个控件后的控件比较困难,要处理也困难。基于以上原因,笔者习惯的做法是Checkbox使用不同名的定义方法,Radio使用Ext官方论坛用户vtswingkid开发的Ext.ux.RadioGroup扩展代替。

Ext.ux.RadioGroup在的下载地址是:http://extjs.com/forum/showthread.php?t=23250

Checkbox在定义时必须使用ColumnLayout,第一列的控件有标签,后续列的控件则隐藏标签。不然,会很难实现习惯的Checkbox对齐方式。Ext.ux.RadioGroup在扩展的同时已经处理好对齐问题了,所以不必象Checkbox那样使用ColumnLayout

Checkbox的主要配置参数请看表1

Ext.ux.RadioGroup的主要配置参数请看表2

Ext.ux.RadioGroup的主要配置参数请看表2

 
 
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
  2. <html debug='true'>
  3. <head>
  4. <title>Checkbox与RadioGroup例子</title>
  5. <meta http-equiv="content-type" content="text/html; charset=utf-8">
  6. <link rel="stylesheet" type="text/css" href="../lib/ext/resources/css/ext-all.css" />
  7. <script type="text/javascript" src="../lib/ext/ext-base.js"></script>
  8. <script type="text/javascript" src="../lib/ext/ext-all.js"></script>
  9. <script type="text/javascript" src="../lib/ext/radiogroup.js"></script>
  10. <script type="text/javascript" src="../lib/ext/locale/ext-lang-zh_CN.js"></script>
  11. <style>
  12. </style>
  13. </head>
  14. <body>
  15. <h1 style="margin:20px 0px 0px 20px;">第4章 Checkbox与RadioGroup例子</h1>
  16. <br />
  17. <div style="padding-left:20px;">
  18. <p>
  19. <div id="form1"></div><br>
  20. <div >执行操作:</div>
  21. <textarea id='op' rows="10" style="width:800px;"></textarea>
  22. </p>
  23. <br />
  24. </div>
  25. <script>
  26. var app={};
  27. Ext.onReady(function(){
  28. var frm = new Ext.form.FormPanel({
  29. applyTo: "form1",
  30. autoHeight: true,
  31. width: 860,
  32. height:300,
  33. frame: true,
  34. labelWidth:80,
  35. labelSeparator:":",
  36. title:'Checkbox与RadioGroup例子',
  37. items:[
  38. {layout:'column',border:false,items:[
  39. {columnWidth:.5,layout: 'form',border:false,items: [
  40. {xtype:'fieldset',title:'控件名不同',height:110,items:[
  41. {layout:'column',border:false,items:[
  42. {columnWidth:.5,layout: 'form',border:false,items: [
  43. {xtype:'checkbox',fieldLabel:'角色',boxLabel:'系统管理员',name:'role1',anchor:'100%'}
  44. ]},
  45. {columnWidth:.25,layout: 'form',border:false,items: [
  46. {xtype:'checkbox',hideLabel:true,boxLabel:'经理',name:'role2',anchor:'100%'}
  47. ]},
  48. {columnWidth:.25,layout: 'form',border:false,items: [
  49. {xtype:'checkbox',hideLabel:true,boxLabel:'普通用户',name:'role3',anchor:'100%'}
  50. ]}
  51. ]},
  52. {layout:'column',border:false,items:[
  53. {columnWidth:.3,layout: 'form',border:false,items: [
  54. {xtype:'button',text:'选择“系统管理员”',scope:this,
  55. handler:function(){
  56. frm.form.findField('role1').setValue('true');
  57. Ext.get('op').dom.value+="执行:frm.form.findField('role1').setValue('true')/n";
  58. }
  59. }
  60. ]},
  61. {columnWidth:.4,layout: 'form',border:false,items: [
  62. {xtype:'button',text:'不选择“系统管理员”',scope:this,
  63. handler:function(){
  64. frm.form.findField('role1').setValue('false');
  65. Ext.get('op').dom.value+="执行:frm.form.findField('role1').setValue('false')/n";
  66. }
  67. }
  68. ]},
  69. {columnWidth:.3,layout: 'form',border:false,items: [
  70. {xtype:'button',text:'选择“经理”',scope:this,
  71. handler:function(){
  72. frm.form.findField('role2').setValue('true');
  73. Ext.get('op').dom.value+="执行:frm.form.findField('role2').setValue('true')/n";
  74. }
  75. }
  76. ]}
  77. ]},
  78. {layout:'column',border:false,items:[
  79. {columnWidth:.3,layout: 'form',border:false,items: [
  80. {xtype:'button',text:'不选择“经理”',scope:this,
  81. handler:function(){
  82. frm.form.findField('role2').setValue('false');
  83. Ext.get('op').dom.value+="执行:frm.form.findField('role2').setValue('false')/n";
  84. }
  85. }
  86. ]},
  87. {columnWidth:.4,layout: 'form',border:false,items: [
  88. {xtype:'button',text:'选择“普通用户”',scope:this,
  89. handler:function(){
  90. frm.form.findField('role3').setValue('true');
  91. Ext.get('op').dom.value+="执行:frm.form.findField('role3').setValue('true')/n";
  92. }
  93. }
  94. ]},
  95. {columnWidth:.3,layout: 'form',border:false,items: [
  96. {xtype:'button',text:'不选择“普通用户”',scope:this,
  97. handler:function(){
  98. frm.form.findField('role3').setValue('false');
  99. Ext.get('op').dom.value+="执行:frm.form.findField('role3').setValue('false')/n";
  100. }
  101. }
  102. ]}
  103. ]}
  104. ]}
  105. ]},
  106. {columnWidth:.5,bodyStyle:'padding: 0 0 0 5px',layout:'form',border:false,items: [
  107. {xtype:'fieldset',title:'控件名相同',height:110,items:[
  108. {layout:'column',border:false,items:[
  109. {columnWidth:.5,layout: 'form',border:false,items: [
  110. {xtype:'checkbox',fieldLabel:'角色2',boxLabel:'系统管理员',name:'role',anchor:'90%',inputValue:"系统管理员"}
  111. ]},
  112. {columnWidth:.25,layout: 'form',border:false,items: [
  113. {xtype:'checkbox',hideLabel:true,boxLabel:'经理',name:'role',anchor:'90%',inputValue:"经理"}
  114. ]},
  115. {columnWidth:.25,layout: 'form',border:false,items: [
  116. {xtype:'checkbox',hideLabel:true,boxLabel:'普通用户',name:'role',anchor:'90%',inputValue:"普通用户"}
  117. ]}
  118. ]},
  119. {layout:'column',border:false,items:[
  120. {columnWidth:.3,layout: 'form',border:false,items: [
  121. {xtype:'button',text:'findField("role")',scope:this,
  122. handler:function(){
  123. var obj=frm.form.findField('role');
  124. Ext.get('op').dom.value+="执行:var obj=frm.form.findField('role')/n"+
  125. 'obj.inputValue:'+obj.inputValue+'/n';
  126. }
  127. }
  128. ]},
  129. {columnWidth:.4,layout: 'form',border:false,items: [
  130. {xtype:'button',text:"setValue('true')",scope:this,
  131. handler:function(){
  132. frm.form.findField('role').setValue('true');
  133. Ext.get('op').dom.value+="执行:frm.form.findField('role').setValue('true')/n";
  134. }
  135. }
  136. ]},
  137. {columnWidth:.3,layout: 'form',border:false,items: [
  138. {xtype:'button',text:"setValue('false')",scope:this,
  139. handler:function(){
  140. frm.form.findField('role').setValue('false');
  141. Ext.get('op').dom.value+="执行:frm.form.findField('role').setValue('false')/n";
  142. }
  143. }
  144. ]}
  145. ]}
  146. ]}
  147. ]}
  148. ]},
  149. {xtype:'fieldset',title:'RadioGroup',height:60,items:[
  150. {layout:'column',border:false,items:[
  151. {columnWidth:.3,layout: 'form',border:false,items: [
  152. {xtype:'ux-radiogroup',
  153. fieldLabel:'性别',
  154. name:'sex',
  155. horizontal:true,
  156. radios:[{
  157. value:'男',
  158. checked:true,
  159. boxLabel:'男'
  160. }, {
  161. value:'女',
  162. boxLabel:'女'
  163. }]
  164. }
  165. ]},
  166. {columnWidth:.3,layout: 'form',border:false,items: [
  167. {xtype:'button',text:"setValue('男')",scope:this,
  168. handler:function(){
  169. frm.form.findField('sex').setValue('男');
  170. Ext.get('op').dom.value+="执行:frm.form.findField('sex').setValue('男')/n";
  171. }
  172. }
  173. ]},
  174. {columnWidth:.3,layout: 'form',border:false,items: [
  175. {xtype:'button',text:"setValue('女')",scope:this,
  176. handler:function(){
  177. frm.form.findField('sex').setValue('女');
  178. Ext.get('op').dom.value+="执行:frm.form.findField('sex').setValue('女')/n";
  179. }
  180. }
  181. ]}
  182. ]}
  183. ]},
  184. {layout:'column',border:false,items:[
  185. {columnWidth:.5,layout: 'form',border:false,items: [
  186. {xtype:'button',text:"form.setValues({role1:true,role:true,sex:'男'})",scope:this,
  187. handler:function(){
  188. frm.form.setValues({role1:true,role:true,sex:'男'});
  189. Ext.get('op').dom.value+="执行:frm.form.setValues({role1:true,role:true,sex:'男'})/n";
  190. }
  191. }
  192. ]},
  193. {columnWidth:.5,layout: 'form',border:false,items: [
  194. {xtype:'button',text:"form.getValues()",scope:this,
  195. handler:function(){
  196. Ext.get('op').dom.value+="执行:frm.form.getValues()/n"
  197. +'结果:'+Ext.encode(frm.form.getValues())+'/n';
  198. }
  199. }
  200. ]}
  201. ]}
  202. ],
  203. buttons: [{
  204. text: '保存',
  205. scope:this,
  206. handler:function(){
  207. if(frm.form.isValid()){
  208. frm.form.doAction('submit',{
  209. url:'form.ashx',
  210. method:'post',
  211. params:'',
  212. success:function(form,action){
  213. Ext.Msg.alert('操作',action.result.data);
  214. },
  215. failure:function(){
  216. //Ext.Msg.alert('操作','保存失败!');
  217. }
  218. });
  219. }
  220. }
  221. },{
  222. text: '取消',
  223. scope:this,
  224. handler:function(){frm.form.reset();}
  225. }]
  226. });
  227. })
  228. </script>
  229. </body>
  230. </html>

例子的运行结果如图1

从图中可以看到定义了2Checkbox与一组RadioGroup,并设置了相应的测试按钮,在执行操作多行文本框内可以了解各按钮执行的操作以及一些结果。

1组定义了“role1”、“role2”和“role33个不同名称的Checkbox,可顺利通过findField方法找到控件,然后通过setValue方法设置复选框的状态。控制复选框的状态有多种值选择,设置为已选的值有trueonvalue配置参数设置的值,但是不能使用inputValue配置参数设置的值,设置为未选的值有falseoff或空。

2组定义了3个名称相同的Checkbox,通过findField(“role”)按钮可测试通过findField方法找到的控件是那一个,其结果如下:

执行:var obj=frm.form.findField('role')

obj.inputValue:系统管理员

从结果中可以了解到它查找出来的不是数组,只是第1Checkbox

再单击两个setValue按钮可以了解到通过findField方法找到控件后再使用setValue方法只能设置第1Checkbox的状态,因为findField方法找到的是第1Checkbox

看看RadioGroup的定义方法:

 
 
  1. {xtype:'ux-radiogroup',  
  2.  
  3.                                                                            fieldLabel:'性别',  
  4.  
  5.                                                                            name:'sex',  
  6.  
  7.                                                                            horizontal:true,  
  8.  
  9.                                                                            radios:[{  
  10.  
  11.                                                                                     value:'男',  
  12.  
  13.                                                                                     checked:true,  
  14.  
  15.                                                                                     boxLabel:'男'  
  16.  
  17.                                                                            }, {  
  18.  
  19.                                                                                     value:'女',  
  20.  
  21.                                                                                     boxLabel:'女'  
  22.  
  23.                                                                            }]  
  24.  
  25.                                                                  }  

相当简单。RadioGroup的标签为性别,名称为sex,单选按钮水平排列,radios里定义了两个单选按钮,第1个的值为男,初始状态为已选,显示文本为男,第2个的值是女,显示文本为女。通过两个setValue按钮,可测试setValue方法将value配置的值作为参数传递就可以设置那个单选按钮为已选状态了。

FormPanel里还有两个按钮用来测试Form的setValues方法与getValues方法。
先单击setValues按钮,该按钮执行了以下语句:

frm.form.setValues({role1:true,role:true,sex:'男'})
“{role1:true,role:true,sex:'男'}”的意思就是对应名称为role1的控件的设置为已选,就是第1组Checkbox的系统管理员为选中状态。名称为role的因为同名,只能设置第1个Checkbox的状态,所以也是系统管理员为选中状态。名称为sex的值设置为男,表示性别的男为选中状态。
在第1组Checkbox中选择系统管理员和普通用户,第2组Checkbox中选择经理与普通用户,在性别中选择女,然后单击form.getValues()按钮,会获得如下结果:

执行:frm.form.getValues()

结果:{"role1":"on","role3":"on","role":["经理","普通用户"],"sex":""}

从结果中可以了解到,第 1 组中被选中的 role1 role3 两个控件的值为 on ,因为 role2 没有被选中,所以没有值;第 2 role 因为设置了 inputValue ,所以返回的是经理与普通用户两个值,;第 3 sex 则返回了选择值的“女”。以上的结果也是 Form 提交的值,可以单击保存按钮通过 Firebug 测试一下,测试结果如图 2

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值