跳到主要内容
极客日志极客日志面向AI+效率的开发者社区
首页博客我的书AI学习GitHub 精选镜像AI 生图工具UI配色美学关于
搜索内容 / 工具 / 仓库 / 镜像...⌘K搜索
注册
博客列表
JavaScript大前端

ExtJS 表单控件:单选框与复选框组合使用示例

ExtJS 表单面板中单选框组与复选框组的配置示例,展示了自动布局、单列、多列及自定义宽度的布局方式,包含验证与重置功能。

暖阳发布于 2025/1/19更新于 2026/10/1989 浏览
ExtJS 表单控件:单选框与复选框组合使用示例
Ext.onReady(function() {
  Ext.QuickTips.init();

  // turn on validation errors beside the field globally
  Ext.form.Field.prototype.msgTarget = 'side';

  var individual = [ {
    bodyStyle: 'padding-right:5px;',
    items: {
      xtype: 'fieldset',
      title: 'Individual Checkboxes',
      autoHeight: true,
      defaultType: 'checkbox', // each item will be a checkbox
      items: [ {
        xtype: 'textfield',
        name: 'txt-test1',
        fieldLabel: 'Alignment Test'
      }, {
        fieldLabel: 'Favorite Animals',
        boxLabel: 'Dog',
        name: 'fav-animal-dog'
      }, {
        fieldLabel: '',
        labelSeparator: '',
        boxLabel: 'Cat',
        name: 'fav-animal-cat'
      }, {
        checked: true,
        fieldLabel: '',
        labelSeparator: '',
        boxLabel: 'Monkey',
        name: 'fav-animal-monkey'
      } ]
    }
  }, {
    bodyStyle: 'padding-left:5px;',
    items: {
      xtype: 'fieldset',
      title: 'Individual Radios',
      autoHeight: true,
      defaultType: 'radio', // each item will be a radio button
      items: [ {
        xtype: 'textfield',
        name: 'txt-test2',
        fieldLabel: 'Alignment Test'
      }, {
        checked: true,
        fieldLabel: 'Favorite Color',
        boxLabel: 'Red',
        name: 'fav-color',
        inputValue: 'red'
      }, {
        fieldLabel: '',
        labelSeparator: '',
        boxLabel: 'Blue',
        name: 'fav-color',
        inputValue: 'blue'
      }, {
        fieldLabel: '',
        labelSeparator: '',
        boxLabel: 'Green',
        name: 'fav-color',
        inputValue: 'green'
      } ]
    }
  } ];

  /*====================================================================
   * CheckGroup example
   *====================================================================*/
  var checkGroup = {
    xtype: 'fieldset',
    title: 'Checkbox Groups (initially collapsed)',
    autoHeight: true,
    layout: 'form',
    collapsed: true, // initially collapse the group
    collapsible: true,
    items: [ {
      xtype: 'textfield',
      name: 'txt-test3',
      fieldLabel: 'Alignment Test',
      anchor: '95%'
    }, {
      // Use the default, automatic layout to distribute the controls evenly
      // across a single row
      xtype: 'checkboxgroup',
      fieldLabel: 'Auto Layout',
      items: [ {
        boxLabel: 'Item 1',
        name: 'cb-auto-1'
      }, {
        boxLabel: 'Item 2',
        name: 'cb-auto-2',
        checked: true
      }, {
        boxLabel: 'Item 3',
        name: 'cb-auto-3'
      }, {
        boxLabel: 'Item 4',
        name: 'cb-auto-4'
      }, {
        boxLabel: 'Item 5',
        name: 'cb-auto-5'
      } ]
    }, {
      xtype: 'checkboxgroup',
      fieldLabel: 'Single Column',
      itemCls: 'x-check-group-alt',
      // Put all controls in a single column with width 100%
      columns: 1,
      items: [ {
        boxLabel: 'Item 1',
        name: 'cb-col-1'
      }, {
        boxLabel: 'Item 2',
        name: 'cb-col-2',
        checked: true
      }, {
        boxLabel: 'Item 3',
        name: 'cb-col-3'
      } ]
    }, {
      xtype: 'checkboxgroup',
      fieldLabel: 'Multi-Column (horizontal)',
      // Distribute controls across 3 even columns, filling each row
      // from left to right before starting the next row
      columns: 3,
      items: [ {
        boxLabel: 'Item 1',
        name: 'cb-horiz-1'
      }, {
        boxLabel: 'Item 2',
        name: 'cb-horiz-2',
        checked: true
      }, {
        boxLabel: 'Item 3',
        name: 'cb-horiz-3'
      }, {
        boxLabel: 'Item 4',
        name: 'cb-horiz-4'
      }, {
        boxLabel: 'Item 5',
        name: 'cb-horiz-5'
      } ]
    }, {
      xtype: 'checkboxgroup',
      fieldLabel: 'Multi-Column (vertical)',
      itemCls: 'x-check-group-alt',
      // Distribute controls across 3 even columns, filling each column
      // from top to bottom before starting the next column
      columns: 3,
      vertical: true,
      items: [ {
        boxLabel: 'Item 1',
        name: 'cb-vert-1'
      }, {
        boxLabel: 'Item 2',
        name: 'cb-vert-2',
        checked: true
      }, {
        boxLabel: 'Item 3',
        name: 'cb-vert-3'
      }, {
        boxLabel: 'Item 4',
        name: 'cb-vert-4'
      }, {
        boxLabel: 'Item 5',
        name: 'cb-vert-5'
      } ]
    }, {
      xtype: 'checkboxgroup',
      fieldLabel: 'Multi-Column<br />(custom widths)',
      // Specify exact column widths (could also include float values for %)
      columns: [ 100, 100 ],
      vertical: true,
      items: [ {
        boxLabel: 'Item 1',
        name: 'cb-custwidth',
        inputValue: 1
      }, {
        boxLabel: 'Item 2',
        name: 'cb-custwidth',
        inputValue: 2,
        checked: true
      }, {
        boxLabel: 'Item 3',
        name: 'cb-custwidth',
        inputValue: 3
      }, {
        boxLabel: 'Item 4',
        name: 'cb-custwidth',
        inputValue: 4
      }, {
        boxLabel: 'Item 5',
        name: 'cb-custwidth',
        inputValue: 5
      } ]
    }, {
      xtype: 'checkboxgroup',
      itemCls: 'x-check-group-alt',
      fieldLabel: 'Custom Layout<br />(w/ validation)',
      allowBlank: false,
      anchor: '95%',
      items: [ {
        // You can pass sub-item arrays along with width/columnWidth configs
        // ColumnLayout-style for complete layout control.  In this example we
        // only want one item in the middle column, which would not be possible
        // using the columns config.  We also want to make sure that our headings
        // end up at the top of each column as expected.
        columnWidth: '.25',
        items: [ {
          xtype: 'label',
          text: 'Heading 1',
          cls: 'x-form-check-group-label',
          anchor: '-15'
        }, {
          boxLabel: 'Item 1',
          name: 'cb-cust-1'
        }, {
          boxLabel: 'Item 2',
          name: 'cb-cust-2'
        } ]
      }, {
        columnWidth: '.5',
        items: [ {
          xtype: 'label',
          text: 'Heading 2',
          cls: 'x-form-check-group-label',
          anchor: '-15'
        }, {
          boxLabel: 'A long item just for fun',
          name: 'cb-cust-3'
        } ]
      }, {
        columnWidth: '.25',
        items: [ {
          xtype: 'label',
          text: 'Heading 3',
          cls: 'x-form-check-group-label',
          anchor: '-15'
        }, {
          boxLabel: 'Item 4',
          name: 'cb-cust-4'
        }, {
          boxLabel: 'Item 5',
          name: 'cb-cust-5'
        } ]
      } ]
    } ]
  };

  /*====================================================================
   * RadioGroup examples
   *====================================================================*/

  var radioGroup = {
    xtype: 'fieldset',
    title: 'Radio Groups',
    autoHeight: true,
    items: [ {
      xtype: 'textfield',
      name: 'txt-test4',
      fieldLabel: 'Alignment Test',
      anchor: '95%'
    }, {
      xtype: 'radiogroup',
      fieldLabel: 'Auto Layout',
      items: [ {
        boxLabel: 'Item 1',
        name: 'rb-auto',
        inputValue: 1
      }, {
        boxLabel: 'Item 2',
        name: 'rb-auto',
        inputValue: 2,
        checked: true
      }, {
        boxLabel: 'Item 3',
        name: 'rb-auto',
        inputValue: 3
      }, {
        boxLabel: 'Item 4',
        name: 'rb-auto',
        inputValue: 4
      }, {
        boxLabel: 'Item 5',
        name: 'rb-auto',
        inputValue: 5
      } ]
    }, {
      xtype: 'radiogroup',
      fieldLabel: 'Single Column',
      itemCls: 'x-check-group-alt',
      columns: 1,
      items: [ {
        boxLabel: 'Item 1',
        name: 'rb-col',
        inputValue: 1
      }, {
        boxLabel: 'Item 2',
        name: 'rb-col',
        inputValue: 2,
        checked: true
      }, {
        boxLabel: 'Item 3',
        name: 'rb-col',
        inputValue: 3
      } ]
    }, {
      xtype: 'radiogroup',
      fieldLabel: 'Multi-Column<br />(horiz. auto-width)',
      columns: 3,
      items: [ {
        boxLabel: 'Item 1',
        name: 'rb-horiz',
        inputValue: 1
      }, {
        boxLabel: 'Item 2',
        name: 'rb-horiz',
        inputValue: 2,
        checked: true
      }, {
        boxLabel: 'Item 3',
        name: 'rb-horiz',
        inputValue: 3
      }, {
        boxLabel: 'Item 4',
        name: 'rb-horiz',
        inputValue: 4
      }, {
        boxLabel: 'Item 5',
        name: 'rb-horiz',
        inputValue: 5
      } ]
    }, {
      xtype: 'radiogroup',
      fieldLabel: 'Multi-Column<br />(vert. auto-width)',
      itemCls: 'x-check-group-alt',
      columns: 3,
      vertical: true,
      items: [ {
        boxLabel: 'Item 1',
        name: 'rb-vert',
        inputValue: 1
      }, {
        boxLabel: 'Item 2',
        name: 'rb-vert',
        inputValue: 2,
        checked: true
      }, {
        boxLabel: 'Item 3',
        name: 'rb-vert',
        inputValue: 3
      }, {
        boxLabel: 'Item 4',
        name: 'rb-vert',
        inputValue: 4
      }, {
        boxLabel: 'Item 5',
        name: 'rb-vert',
        inputValue: 5
      } ]
    }, {
      xtype: 'radiogroup',
      fieldLabel: 'Multi-Column<br />(custom widths)',
      columns: [ 100, 100 ],
      vertical: true,
      items: [ {
        boxLabel: 'Item 1',
        name: 'rb-custwidth',
        inputValue: 1
      }, {
        boxLabel: 'Item 2',
        name: 'rb-custwidth',
        inputValue: 2,
        checked: true
      }, {
        boxLabel: 'Item 3',
        name: 'rb-custwidth',
        inputValue: 3
      }, {
        boxLabel: 'Item 4',
        name: 'rb-custwidth',
        inputValue: 4
      }, {
        boxLabel: 'Item 5',
        name: 'rb-custwidth',
        inputValue: 5
      } ]
    }, {
      xtype: 'radiogroup',
      itemCls: 'x-check-group-alt',
      fieldLabel: 'Custom Layout<br />(w/ validation)',
      allowBlank: false,
      anchor: '95%',
      items: [ {
        columnWidth: '.25',
        items: [ {
          xtype: 'label',
          text: 'Heading 1',
          cls: 'x-form-check-group-label',
          anchor: '-15'
        }, {
          boxLabel: 'Item 1',
          name: 'rb-cust',
          inputValue: 1
        }, {
          boxLabel: 'Item 2',
          name: 'rb-cust',
          inputValue: 2
        } ]
      }, {
        columnWidth: '.5',
        items: [ {
          xtype: 'label',
          text: 'Heading 2',
          cls: 'x-form-check-group-label',
          anchor: '-15'
        }, {
          boxLabel: 'A long item just for fun',
          name: 'rb-cust',
          inputValue: 3
        } ]
      }, {
        columnWidth: '.25',
        items: [ {
          xtype: 'label',
          text: 'Heading 3',
          cls: 'x-form-check-group-label',
          anchor: '-15'
        }, {
          boxLabel: 'Item 4',
          name: 'rb-cust',
          inputValue: 4
        }, {
          boxLabel: 'Item 5',
          name: 'rb-cust',
          inputValue: 5
        } ]
      } ]
    } ]
  };

  // combine all that into one huge form
  var fp = new Ext.FormPanel({
    title: 'Check/Radio Groups Example',
    frame: true,
    labelWidth: 110,
    width: 600,
    renderTo: 'container',
    bodyStyle: 'padding:0 10px 0;',
    items: [ {
      layout: 'column',
      border: false,
      // defaults are applied to all child items unless otherwise specified by child item
      defaults: {
        columnWidth: '.5',
        border: false
      },
      items: individual
    }, checkGroup, radioGroup ],
    buttons: [
      {
        text: 'Save',
        handler: function() {
          if (fp.getForm().isValid()) {
            Ext.Msg.alert('Submitted Values',
              'The following will be sent to the server: <br />'
              + fp.getForm().getValues(true)
              .replace(/&/g, ', '));
          }
        }
      }, {
        text: 'Reset',
        handler: function() {
          fp.getForm().reset();
        }
      }
    ]
  });
});

更多推荐文章

查看全部
  • Linux Socket 编程实战:基于 UDP 的简易英译汉翻译服务器
  • 引入 Frontend Design Skill 增强大模型前端生成的审美能力
  • C++ QT 串口调试助手项目开发实战
  • GitHub Copilot 学生认证通过指南:2FA、材料与常见问题处理
  • C++ vector 深度剖析与模拟实现
  • 前端安全实战:防范 XSS、CSRF 与敏感数据泄露
  • 2026 年 1 月主流远程桌面工具横评与功能对比
  • mux.js 完全指南:轻量级视频容器处理工具入门
  • 使用 cpolar 远程部署 Open-Lovable 实现网页克隆
  • AI 辅助前端 UI 设计工具 UI UX Pro Max 实战指南
  • Python 核心知识点速查:31 个基础要点
  • 贪心算法实战:从摆动序列到股票买卖
  • Flutter wasm_ffi 在鸿蒙端的 WebAssembly 集成与适配实战
  • Java 填充 Word 模板实战:文本、列表与复选框处理
  • Photoshop 安装 WebPShop 插件实现 WebP 格式支持
  • SkyWalking .NET / C++ / Lua 探针现状与社区支持
  • Vivado 2023.2 安装与 FPGA 开发环境搭建指南
  • 网络安全渗透测试全流程与核心工具指南
  • 基于 Sentry 自建前端错误监控系统实战
  • TarsosDSP Java 实时音频处理指南

相关免费在线工具

  • Keycode 信息

    查找任何按下的键的javascript键代码、代码、位置和修饰符。 在线工具,Keycode 信息在线工具,online

  • Escape 与 Native 编解码

    JavaScript 字符串转义/反转义;Java 风格 \uXXXX(Native2Ascii)编码与解码。 在线工具,Escape 与 Native 编解码在线工具,online

  • JavaScript / HTML 格式化

    使用 Prettier 在浏览器内格式化 JavaScript 或 HTML 片段。 在线工具,JavaScript / HTML 格式化在线工具,online

  • JavaScript 压缩与混淆

    Terser 压缩、变量名混淆,或 javascript-obfuscator 高强度混淆(体积会增大)。 在线工具,JavaScript 压缩与混淆在线工具,online

  • Base64 字符串编码/解码

    将字符串编码和解码为其 Base64 格式表示形式即可。 在线工具,Base64 字符串编码/解码在线工具,online

  • Base64 文件转换器

    将字符串、文件或图像转换为其 Base64 表示形式。 在线工具,Base64 文件转换器在线工具,online