`
crabdave
  • 浏览: 1294830 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Ext.ux.RadioGroup的使用(让各radio使用不同的名称) 示例

阅读更多

Ext.ux.RadioGroup的使用(让各radio使用不同的名称) 示例

      我们在使用Ext.form.RadioGroup的时候需要将各radio组件的name设置成同一个,这样用户在点选时才能保证一次只有一个radio被选中,但在项目开发中往往需要去取某一个指定的radio做一些操作,如果使用Ext.form.RadioGroup给开发带来了很多的不方便。为避免该类问题,我们在创建RadioGroup组件的时候我们往往使用Ext官方论坛中的Ext.ux.RadioGroup扩展组件来代替。

创建调用的HTML:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK" />
<title></title>
<link rel="stylesheet" type="text/css" href="extjs/resources/css/ext-all.css" />
 <script type="text/javascript" src="extjs/adapter/ext/ext-base.js"></script>
 <script type="text/javascript" src="extjs/ext-all.js"></script>
 <script type="text/javascript" src="./Ext.ux.RadioGroup.js"></script>
<style type="text/css">
</style>
<script>
Ext.onReady(function() {
 new Ext.Panel({
  renderTo : document.body,
  layout : 'form',
  items : [{
   xtype : 'ux-radiogroup',
   fieldLabel : 'ux-radiogroup',
   name : 'radiobox_a',
   horizontal : true,
   radios : [{
    value : 1,
    boxLabel : 'box 1',
    name:'b1',
    id:'b1',
    listeners : {
     'check' : function(r, c) {
      alert(r.boxLabel + ": " + (c ? "checked" : "unchecked"));
     }
    }
   }, {
    value : 2,
    name:'b2',
    id:'b2',
    boxLabel : 'box 2',
    checked : true
   }, {
    value : 3,
    name:'b3',
    id:'b2',
    boxLabel : 'box 3'
   }]
  },new Ext.form.RadioGroup({
                       width : 220,
                      // hideLabel:true,
       fieldLabel : 'radiogroup',
                       style:'margin-left:100px;',
                       columns : 1,
                       vertical :true,
                       items:[
                              {boxLabel:'box 1',inputValue:'value1',name:'RadioGroup'},
         {boxLabel:'box 2',inputValue:'value2',name:'RadioGroup'},
                               {boxLabel:'box 3',inputValue:'value3',name:'RadioGroup',
                           listeners:{
                                check:function(el,checked){
                                    alert(checked);
         }
                                   }
                               }]
                          })]

 });

});
</script>
</head>
<body>

</body>
</html>

 

Ext.ux.RadioGroup.js文件源码:

/**
 * @author Robert Williams (vtswingkid)
 * @version 1.0.4
 */
Ext.namespace('Ext.ux');
Ext.ux.RadioGroup = Ext.extend(Ext.form.Field,  {
    /**
     * @cfg {String} focusClass The CSS class to use when the checkbox receives focus (defaults to undefined)
     */
    focusClass : undefined,
    /**
     * @cfg {String} fieldClass The default CSS class for the checkbox (defaults to "x-form-field")
     */
    fieldClass: "x-form-field",
    /**
     * @cfg {Boolean} checked True if the the checkbox should render already checked (defaults to false)
     */
    checked: false,
    /**
     * @cfg {String/Object} autoCreate A DomHelper element spec, or true for a default element spec (defaults to
     * {tag: "input", type: "radio", autocomplete: "off"})
     */
    defaultAutoCreate : { tag: "input", type: 'radio', autocomplete: "off"},
    /**
     * @cfg {String} boxLabel The text that appears beside the checkbox
     */
 
 getId:function(){
  //if multiple radios are defined use this information
  if(this.radios && this.radios instanceof Array){
   if(this.radios.length){
    var r=this.radios[0];
    this.value=r.value;
    this.boxLabel=r.boxLabel;
    this.checked=r.checked || false;
    this.readOnly=r.readOnly || false;
    this.disabled=r.disabled || false;
    this.tabIndex=r.tabIndex;
    this.cls=r.cls;
    this.listeners=r.listeners;
    this.style=r.style;
    this.bodyStyle=r.bodyStyle;
    this.hideParent=r.hideParent;
    this.hidden=r.hidden;
   }
  }
  Ext.ux.RadioGroup.superclass.getId.call(this);
 },

 // private
    initComponent : function(){
        Ext.ux.RadioGroup.superclass.initComponent.call(this);
        this.addEvents(
            /**
             * @event change
             * Fires when the radio value changes.
             * @param {Ext.vx.RadioGroup} this This radio
             * @param {Boolean} checked The new checked value
             */
            'check'
        );
    },

    // private
    onResize : function(){
        Ext.ux.RadioGroup.superclass.onResize.apply(this, arguments);
        if(!this.boxLabel){
            this.el.alignTo(this.wrap, 'c-c');
        }
    },
   
    // private
    initEvents : function(){
        Ext.ux.RadioGroup.superclass.initEvents.call(this);
        this.el.on("click", this.onClick,  this);
        this.el.on("change", this.onClick,  this);
    },

 // private
    getResizeEl : function(){
        return this.wrap;
    },

    // private
    getPositionEl : function(){
        return this.wrap;
    },

    /**
     * Overridden and disabled. The editor element does not support standard valid/invalid marking. @hide
     * @method
     */
    markInvalid : Ext.emptyFn,
    /**
     * Overridden and disabled. The editor element does not support standard valid/invalid marking. @hide
     * @method
     */
    clearInvalid : Ext.emptyFn,

    // private
    onRender : function(ct, position){
        Ext.ux.RadioGroup.superclass.onRender.call(this, ct, position);
        this.wrap = this.el.wrap({cls: "x-form-check-wrap"});
        if(this.boxLabel){
            this.wrap.createChild({tag: 'label', htmlFor: this.el.id, cls: 'x-form-cb-label', html: this.boxLabel});
        }
  if(!this.isInGroup){
   this.wrap.applyStyles({'padding-top':'2px'});
  }
        if(this.checked){
            this.setChecked(true);
        }else{
            this.checked = this.el.dom.checked;
        }
  if (this.radios && this.radios instanceof Array) {
   this.els=new Array();
   this.els[0]=this.el;
   for(var i=1;i<this.radios.length;i++){
    var r=this.radios[i];
    this.els[i]=new Ext.ux.RadioGroup({
     renderTo:this.wrap,
     hideLabel:true,
     boxLabel:r.boxLabel,
     checked:r.checked || false,
     value:r.value,
     name:this.name || this.id,
     readOnly:r.readOnly || false,
     disabled:r.disabled || false,
     tabIndex:r.tabIndex,
     cls:r.cls,
     listeners:r.listeners,
     style:r.style,
     bodyStyle:r.bodyStyle,
     hideParent:r.hideParent,
     hidden:r.hidden,
     isInGroup:true
    });
    if (this.horizontal) {
     this.els[i].el.up('div.x-form-check-wrap').applyStyles({
      'display': 'inline',
      'padding-left': '5px'
     });
    }
   }
   if(this.hidden)this.hide();
  }
    },
   
    initValue : function(){
        if(this.value !== undefined){
            this.el.dom.value=this.value;
        }else if(this.el.dom.value.length > 0){
            this.value=this.el.dom.value;
        }
    },
 
    // private
    onDestroy : function(){
  if (this.radios && this.radios instanceof Array) {
   var cnt = this.radios.length;
   for(var x=1;x<cnt;x++){
    this.els[x].destroy();
   }
  }
        if(this.wrap){
            this.wrap.remove();
        }
        Ext.ux.RadioGroup.superclass.onDestroy.call(this);
    },

 setChecked:function(v){
        if(this.el && this.el.dom){
   var fire = false;
   if(v != this.checked)fire=true;
   this.checked=v;
            this.el.dom.checked = this.checked;
            this.el.dom.defaultChecked = this.checked;
         if(fire)this.fireEvent("check", this, this.checked);
     }
    },
    /**
     * Returns the value of the checked radio.
     * @return {Mixed} value
     */
    getValue : function(){
        if(!this.rendered) {
            return this.value;
        }
        var p=this.el.up('form');//restrict to the form if it is in a form
  if(!p)p=Ext.getBody();
  var c=p.child('input[name='+escape(this.el.dom.name)+']:checked', true);
  return (c)?c.value:this.value;
    },

 // private
    onClick : function(){
        if(this.el.dom.checked != this.checked){
   var p = this.el.up('form');
   if (!p)
    p = Ext.getBody();
   var els = p.select('input[name=' + escape(this.el.dom.name) + ']');
   els.each(function(el){
    if (el.dom.id == this.id) {
     this.setChecked(true);
    }
    else {
     var e = Ext.getCmp(el.dom.id);
     e.setChecked.apply(e, [false]);
    }
   }, this);
        }
    },

    /**
     * Checks the radio box with the matching value
     * @param {Mixed} v
     */

    setValue : function(v){
        if(!this.rendered) {
            this.value=v;
            return;
        }
        var p=this.el.up('form');//restrict to the form if it is in a form
        if(!p)p=Ext.getBody();
        var target = p.child('input[name=' + escape(this.el.dom.name) + '][value=' + v + ']', true);
        if (target) target.checked = true;
    },
 
 clear: function(){
  if (!this.rendered) return;
  var p = this.el.up('form');//restrict to the form if it is in a form
  if (!p) p = Ext.getBody();
  var c = p.child('input[name=' + escape(this.el.dom.name) + ']:checked', true);
  if (c) c.checked = false;
 },
 
 disable: function(){
  if (!this.rendered) return;
  var p = this.el.up('form');//restrict to the form if it is in a form
  if (!p) p = Ext.getBody();
  var els = p.select('input[name=' + escape(this.el.dom.name) + ']');
  els.each(function(el){
   if (el.dom.id == this.id) {
    Ext.ux.RadioGroup.superclass.disable.call(this);
   }
   else {
    var e = Ext.getCmp(el.dom.id);
    Ext.ux.RadioGroup.superclass.disable.call(e);
   }
  }, this);
 },
 
 enable: function(){
  if (!this.rendered) return;
  var p = this.el.up('form');//restrict to the form if it is in a form
  if (!p) p = Ext.getBody();
  var els = p.select('input[name=' + escape(this.el.dom.name) + ']');
  els.each(function(el){
   if (el.dom.id == this.id) {
    Ext.ux.RadioGroup.superclass.enable.call(this);
   }
   else {
    var e = Ext.getCmp(el.dom.id);
    Ext.ux.RadioGroup.superclass.enable.call(e);
   }
  }, this);
 },

 hide: function(){
  if (!this.rendered) return;
  this.wrap.hide();
  this.wrap.parent().parent().hide();
 },
 
 show: function(){
  if (!this.rendered) return;
  this.wrap.show();
  this.wrap.parent().parent().show();
 }
});
Ext.reg('ux-radiogroup', Ext.ux.RadioGroup);

 

分享到:
评论

相关推荐

    RadioGroup自定义选项卡样式

    radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { for (int i = 0; i &lt; group.getChildCount();...

    Ext RadioGroup实例

    你可以在`RadioGroup.htm`文档中查看更详细的示例代码和演示效果。 最后,我们提到的`Scripts`和`Extjs311`可能分别代表脚本文件夹和Ext JS 3.1.1版本的库文件。确保引入这些文件才能在页面上正确运行上述代码。 ...

    ext checkboxgroup 回填数据解决

    重写 代码如下: //解决checkboxgroup回填数据问题 Ext.override(Ext.form.BasicForm,{ findField : function(id){ var field = this.items.get(id); if(!field){ this.items.each(function(f){ if(f.isXType(...

    ExtJSWeb应用程序开发指南(第2版)

    4.1.8 Ext.form.CheckboxGroup和Ext.form.RadioGroup 4.1.9 Ext.form.field.Trigger触发字段 4.1.10 Ext.form.field.Spinner微调字段 4.1.11 Ext.form.field.Picker拾取器字段 4.1.12 Ext.form.field.ComboBox...

    使用RadioGroup与RadioButton

    3. **获取选中状态**:可以使用`RadioGroup.getCheckedRadioButtonId()`来获取当前选中的`RadioButton`的ID,然后通过`findViewById()`找到对应的`RadioButton`。 ```java int checkedId = radioGroup....

    Dev express radiogroup 动态添加item.zip

    radioGroup.Items.Add(newItem); ``` 如果需要删除某个item,可以使用Remove或RemoveAt方法,如: ```csharp // 删除指定索引的item radioGroup.Items.RemoveAt(index); // 或者删除指定item radioGroup.Items....

    动态添加RadioGroup的RadioButton.zip

    radioGroup.setId(R.id.radio_group); // 添加RadioButton RadioButton radioButton1 = new RadioButton(this); radioButton1.setText("选项1"); radioButton1.setId(1); radioGroup.addView(radioButton1); ...

    ExtJs_xtype一览

    - `radio`:`Ext.form.Radio`,单选按钮。 - `textarea`:`Ext.form.TextArea`,多行文本输入框。 - `textfield`:`Ext.form.TextField`,单行文本输入框。 - `trigger`:`Ext.form.TriggerField`,触发式输入框...

    radiogroup

    radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { switch (checkedId) { case R.id.radio_...

    安卓Andriod源码——动态添加RadioGroup的RadioButton.zip

    radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { // 这里可以获取当前选中的RadioButton的id ...

    RadioGroup+fragment实现切换

    radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { switch (checkedId) { case R.id....

    RadioGroup

    radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { // 获取选中的RadioButton RadioButton ...

    换行的RadioGroup可以使用

    radioGroup.setOrientation(RadioGroup.VERTICAL); ``` 这样,`RadioGroup`将按垂直方向添加新的`RadioButton`,并允许换行。 4. **自定义控件**:`FlexRadioGroup-master`可能是一个开源项目,提供了自定义的`...

    EXT3.2 多选下拉框

    多选下拉框在EXT JS中通常通过`Ext.form.CheckboxGroup`或`Ext.form.RadioGroup`类来实现,但在EXT3.2中,实现多选下拉框功能通常会使用`Ext.form.FieldSet`或`Ext.form.ComboBox`的自定义扩展。这类组件提供了复选...

    Android ViewPager+RadioGroup 自定义控件示例

    本示例旨在教你如何将这些组件结合使用,创建一个自定义控件,以实现上部导航和主页面导航的功能。以下是关于这三个组件的详细解释以及如何在实际项目中进行集成。 1. **ViewPager**:`ViewPager`是Android支持库中...

    ExtJS-3.4.0系列目录

    - `Ext.form.CheckboxGroup` 和 `Ext.form.RadioGroup`:用于组合复选框和单选框。 - `Ext.form.field.Trigger`:触发字段,常用于下拉搜索框。 - `Ext.form.field.Spinner`:用于数字的微调。 - `Ext.form....

    RadioGroup换行

    这个工具类写好的可以直接拿来用,可以换行多列使用,不影响单个选择

    Android中RadioGroup的作用与定义.pdf

    radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { // 通过checkedId找到被选中的RadioButton并...

    RadioGroup支持RadioButton多行多列

    radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { switch (checkedId) { case R.id.radio1: /...

Global site tag (gtag.js) - Google Analytics