当前位置: 首页 > 文档资料 > ExtJS 入门教程 >

Form

优质
小牛编辑
121浏览
2023-12-01

在大多数Web应用程序中,表单是从用户获取信息的最重要的小部件,例如登录表单/反馈表单,以便可以将值保存在数据库中以供将来参考。 表单窗口小部件用于此目的。

在创建表单之前,我们应该了解xTypes。

xType定义Ext JS UI组件的类型,该组件在呈现组件期间确定。 例如,元素可以是我们将xType作为textField的文本框,或者元素只能具有我们具有Numeric xType的数值。

不同类型的xType

TEXTFIELD (TEXTFIELD)

此xType表示文本字段,用户可以在其中输入值。 文本字段的Ext JS类是'Ext.Form.Field.Text'。

{ 	
   xtype: 'textfield',
   fieldLabel: 'Text field'  
} 

文字区域

这是代表文本区域。 Ext JS类是'Ext.form.field.TextArea'。

{
   xtype: 'textarea',
   fieldLabel: 'Text Area'
}

显示字段

这是为了表示显示文本字段。 Ext JS类是'Ext.form.Label'。

{
   xtype: 'displayfield',
   fieldLabel: 'Display Field''
}

日期字段

这是为了表示日期字段,其中包含日期选择器以选择日期。 Ext JS类是'Ext.form.field.Date'。

{
   xtype: 'datefield',
   fieldLabel: 'Date picker'
}

BUTTON

这是代表一个按钮。 Ext JS类是'Ext.button.Button'。

{
   xtype: 'button', 
   text : 'Button'
}

无线电场

这是为了表示无线电场,我们可以在其中选择所有单选按钮中的任何一个,或者可以自定义以一次选择多个。 Ext JS类是'Ext.form.field.Radio'。

{
   xtype      : 'fieldcontainer',
   fieldLabel : 'Radio field',
   defaultType: 'radiofield',
   defaults: {
      flex: 1
   },
   layout: 'hbox',
   items: [{
      boxLabel  : 'A',
      inputValue: 'a',
      id        : 'radio1'
   },{
      boxLabel  : 'B',
      inputValue: 'b',
      id        : 'radio2'
   },{
      boxLabel  : 'C',
      inputValue: 'c',
      id 	      : 'radio3'
   }]
}

CHECKBOX FIELD

这是为了表示一个复选框字段,我们可以一次选择多个值。 Ext JS类是'Ext.form.field.Checkbox'。

{
   xtype: 'fieldcontainer',
   fieldLabel: 'Check Box Field',
   defaultType: 'checkboxfield',
   items: [{
      boxLabel  : 'HTML',
      inputValue: 'html',
      id        : 'checkbox1'
   },{
      boxLabel  : 'CSS',
      inputValue: 'css',
      checked   : true,
      id        : 'checkbox2'
   },{
      boxLabel  : 'JavaScript',
      inputValue: 'js',
      id        : 'checkbox3'
   }]
}

组合框

这是代表一个组合框。 组合框包含项目列表。 Ext JS类是'Ext.form.field.ComboBox'。

{
   xtype : 'combobox',
   fieldLabel: 'Combo box',
   store: store,
   valueField: 'name'
}
// store for drop down values
var store = Ext.create('Ext.data.Store', {
   id : 'statesid',
   fields: ['abbr', 'name'],
   data : [
      {"abbr":"HTML", "name":"HTML"},
      {"abbr":"CSS", "name":"CSS"},
      {"abbr":"JS", "name":"JavaScript"}
   ]
});

时间场

这是表示时间字段,其中可以预定义最大和最小时间值。 Ext JS类是'Ext.form.field.Time'。

{
   xtype: 'timefield',
   fieldLabel: 'Time field',
   minValue: '6:00 AM',
   maxValue: '8:00 PM',
   increment: 30,
   anchor: '100%'
}

文件字段

这是为了表示我们可以浏览和上传文件的文件上传字段。 Ext JS类是'Ext.form.field.File'。

{
   xtype: 'filefield',
   fieldLabel: 'File field',
   labelWidth: 50,
   msgTarget: 'side',
   allowBlank: false,
   anchor: '100%',
   buttonText: 'Select File...'
}

SPINNER

这是为了表示一个微调器字段,其中列表可以旋转并添加。 Ext JS类是'Ext.form.field.Spinner'。

{ 
   xtype: 'spinnerfield',
   fieldLabel: 'Spinner field'
}

数字场

这是为了表示一个数字字段,我们可以在其中预定义最大值和最小值。 Ext JS类是'Ext.form.field.Number'。

{
   xtype: 'numberfield',
   anchor: '100%',
   fieldLabel: 'Numeric field',
   maxValue: 99,
   minValue: 0
}

隐藏场

顾名思义,这个xtype是隐藏值。

{
   xtype: 'hiddenfield',
   value: 'value from hidden field'
}

表单面板的语法

以下是创建表单的简单语法。

Ext.create('Ext.form.Panel');

例子 (Example)

以下是一个显示所有xTypes形式的简单示例。

<!DOCTYPE html>
<html>
   <head>
      <link href = "https://cdnjs.cloudflare.com/ajax/libs/extjs/6.0.0/classic/theme-classic/resources/theme-classic-all.css" 
         rel = "stylesheet" />
      <script type = "text/javascript" 
         src = "https://cdnjs.cloudflare.com/ajax/libs/extjs/6.0.0/ext-all.js"></script>
      <script type = "text/javascript">
         Ext.onReady(function() {
            Ext.create('Ext.form.Panel', {
               id: 'newForm',
               renderTo: 'formId',
               border: true,
               width: 600,
               items: [{
                  xtype: 'textfield',
                  fieldLabel: 'Text Field'
               },{
                  xtype: 'displayfield',
                  fieldLabel: 'Display Field'
               },{
                  xtype: 'textarea',
                  fieldLabel: 'Text Area'
               },{
                  xtype: 'datefield',
                  fieldLabel: 'Date picker'
               },{
                  xtype: 'button',
                  text: 'Button'
               },{
                  xtype: 'fieldcontainer',
                  fieldLabel: 'Radio field',
                  defaultType: 'radiofield',
                  defaults: {
                     flex: 1
                  },
                  layout: 'hbox',
                  items: [{
                     boxLabel: 'A',
                     inputValue: 'a',
                     id: 'radio1'
                  },{
                     boxLabel: 'B',
                     inputValue: 'b',
                     id: 'radio2'
                  },{
                     boxLabel: 'C',
                     inputValue: 'c',
                     id: 'radio3'
                  }]
               },{
                  xtype: 'fieldcontainer',
                  fieldLabel: 'Check Box Field',
                  defaultType: 'checkboxfield',
                  items: [{
                     boxLabel: 'HTML',
                     inputValue: 'html',
                     id: 'checkbox1'
                  },{
                     boxLabel: 'CSS',
                     inputValue: 'css',
                     checked: true,
                     id: 'checkbox2'
                  },{
                     boxLabel: 'JavaScript',
                     inputValue: 'js',
                     id: 'checkbox3'
                  }]
               },{
                  xtype: 'hiddenfield',
                  name: 'hidden field ',
                  value: 'value from hidden field'
               },{
                  xtype: 'numberfield',
                  anchor: '100%',
                  fieldLabel: 'Numeric Field',
                  maxValue: 99,
                  minValue: 0
               },{
                  xtype: 'spinnerfield',
                  fieldLabel: 'Spinner Field',
                  step: 6,
                  // override onSpinUp (using step isn't necessary)
                  onSpinUp: function() {
                     var me = this;
                     if (!me.readOnly) {
                        var val = me.step;    // set the default value to the step value
                        if(me.getValue() !== '') {
                           val = parseInt(me.getValue().slice(0, -5));   // gets rid of " Pack"
                        }                          
                        me.setValue((val + me.step) + ' Pack');
                     }
                  },
                  // override onSpinDown
                  onSpinDown: function() {
                     var me = this;
                     if (!me.readOnly) {
                        if(me.getValue() !== '') {
                           val = parseInt(me.getValue().slice(0, -5));   // gets rid of " Pack"
                        }            
                        me.setValue((val - me.step) + ' Pack');
                     }
                  }
               },{
                  xtype: 'timefield',
                  fieldLabel: 'Time field',
                  minValue: '6:00 AM',
                  maxValue: '8:00 PM',
                  increment: 30,
                  anchor: '100%'
               },{
                  xtype: 'combobox',
                  fieldLabel: 'Combo Box',
                  store: Ext.create('Ext.data.Store', {
                     fields: ['abbr', 'name'],
                     data: [{
                        'abbr': 'HTML',
                        'name': 'HTML'
                     },{
                        'abbr': 'CSS',
                        'name': 'CSS'
                     },{
                        'abbr': 'JS',
                        'name': 'JavaScript'
                     }]
                  }),
                  valueField: 'abbr',
                  displayField: 'name'
               },{
                  xtype: 'filefield',
                  fieldLabel: 'File field',
                  labelWidth: 50,
                  msgTarget: 'side',
                  allowBlank: false,
                  anchor: '100%',
                  buttonText: 'Select File...'
               }]
            });
         });
      </script>
   </head>
   <body>
      <div id = "formId"></div>
   </body>
</html>