Extjs4 新的布局方式

来源:互联网 发布:淘宝古装衣服图片 编辑:程序博客网 时间:2024/04/29 11:24

本问介绍一个常见的??例子“联系我们”在弹出窗口的形式。表单使用vbox和hbox布局,以实现一个统一的灵活的布局。

JS代码:

 
  1. Ext.require([
  2.     'Ext.form.*'
  3. ]);
  4. Ext.onReady(function() {
  5.     var win;
  6.     function showContactForm() {
  7.         if (!win) {
  8.             var form = Ext.widget('form', {
  9.                 layout: {
  10.                     type: 'vbox',
  11.                     align: 'stretch'
  12.                 },
  13.                 bodyBorder: 0,
  14.                 fieldDefaults: {
  15.                     labelAlign: 'top',
  16.                     labelWidth: 100,
  17.                     labelStyle: 'font-weight:bold'
  18.                 },
  19.                 defaults: {
  20.                     margins: '0 0 10 0'
  21.                 },
  22.                 items: [{
  23.                     xtype: 'fieldcontainer',
  24.                     fieldLabel: 'Your Name',
  25.                     labelStyle: 'font-weight:bold;padding:0',
  26.                     layout: 'hbox',
  27.                     defaultType: 'textfield',
  28.                     fieldDefaults: {
  29.                         labelAlign: 'top'
  30.                     },
  31.                     items: [{
  32.                         flex: 1,
  33.                         name: 'firstName',
  34.                         fieldLabel: 'First',
  35.                         allowBlank: false
  36.                     }, {
  37.                         width: 30,
  38.                         name: 'middleInitial',
  39.                         fieldLabel: 'MI',
  40.                         margins: '0 0 0 5'
  41.                     }, {
  42.                         flex: 2,
  43.                         name: 'lastName',
  44.                         fieldLabel: 'Last',
  45.                         allowBlank: false,
  46.                         margins: '0 0 0 5'
  47.                     }]
  48.                 }, {
  49.                     xtype: 'textfield',
  50.                     fieldLabel: 'Your Email Address',
  51.                     vtype: 'email',
  52.                     allowBlank: false
  53.                 }, {
  54.                     xtype: 'textfield',
  55.                     fieldLabel: 'Subject',
  56.                     allowBlank: false
  57.                 }, {
  58.                     xtype: 'textareafield',
  59.                     fieldLabel: 'Message',
  60.                     labelAlign: 'top',
  61.                     flex: 1,
  62.                     margins: '0',
  63.                     allowBlank: false
  64.                 }],
  65.                 buttons: [{
  66.                     text: 'Cancel',
  67.                     handler: function() {
  68.                         this.up('form').getForm().reset();
  69.                         this.up('window').hide();
  70.                     }
  71.                 }, {
  72.                     text: 'Send',
  73.                     handler: function() {
  74.                         if (this.up('form').getForm().isValid()) {
  75.                             // In a real application, this would submit the form to the configured url
  76.                             // this.up('form').getForm().submit();
  77.                             this.up('form').getForm().reset();
  78.                             this.up('window').hide();
  79.                             Ext.MessageBox.alert('Thank you!''Your inquiry has been sent. We will respond as soon as possible.');
  80.                         }
  81.                     }
  82.                 }]
  83.             });
  84.             win = Ext.widget('window', {
  85.                 title: 'Contact Us',
  86.                 closeAction: 'hide',
  87.                 bodyPadding: 10,
  88.                 width: 400,
  89.                 height: 400,
  90.                 minHeight: 400,
  91.                 layout: 'fit',
  92.                 resizable: true,
  93.                 modal: true,
  94.                 items: [form]
  95.             });
  96.         }
  97.         win.show();
  98.     }
  99.     var mainPanel = Ext.widget('panel', {
  100.         renderTo: Ext.getBody(),
  101.         title: 'Welcome!',
  102.         width: 500,
  103.         bodyPadding: 20,
  104.         items: [{
  105.             xtype: 'component',
  106.             html: 'Thank you for visiting our site! We welcome your feedback; please click the button below to ' +
  107.                   'send us a message. We will respond to your inquiry as quickly as possible.',
  108.             style: 'margin-bottom: 20px;'
  109.         }, {
  110.             xtype: 'container',
  111.             style: 'text-align:center',
  112.             items: [{
  113.                 xtype: 'button',
  114.                 cls: 'contactBtn',
  115.                 scale: 'large',
  116.                 text: 'Contact Us',
  117.                 handler: showContactForm
  118.             }]
  119.         }]
  120.     });
  121. });

代码实现在一个window窗口内使用新的布局方式(vbox和hbox)显示一个form表单,相对于Extjs 4.0之前的方式,将更加灵活。

原创粉丝点击