2010-03-14 22 views
10

estoy usando ExtJS para crear un FormPanel:establecer los valores de campos de formulario en ExtJS

new Ext.FormPanel({ 
    labelAlign: 'top', 
    title: 'Loading Contact...', 
    bodyStyle:'padding:5px', 
    width: 600, 
    autoScroll: true, 
    closable: true, 
    items: [{ 
     layout:'column', 
     border:false, 
     items:[{ 
      columnWidth:.5, 
      layout: 'form', 
      border:false, 
      items: [{ 
       xtype:'textfield', 
       fieldLabel: 'First Name', 
       name: 'first_name', 
       id: 'first_name', 
       anchor:'95%' 
      }, { 
       xtype:'datefield', 
       fieldLabel: 'Birthdate', 
       name: 'birthdate', 
       width: 150, 
      }] 
     },{ 
      columnWidth:.5, 
      layout: 'form', 
      border:false, 
      items: [{ 
       xtype:'textfield', 
       fieldLabel: 'Last Name', 
       name: 'last_name', 
       anchor:'95%' 
      },{ 
       xtype:'textfield', 
       fieldLabel: 'Email', 
       name: 'email', 
       vtype:'email', 
       anchor:'95%' 
      }] 
     }] 
    },{ 
     xtype:'tabpanel', 
     plain:true, 
     activeTab: 0, 
     height:300, 
     /* 
     * By turning off deferred rendering we are guaranteeing that the 
     * form fields within tabs that are not activated will still be 
     * rendered. This is often important when creating multi-tabbed 
     * forms. 
     */ 
     deferredRender: false, 
     defaults:{bodyStyle:'padding:10px'}, 
     items:[{ 
      title:'Address', 
      layout:'form', 
      defaults: {width: 230}, 
      defaultType: 'textfield', 

      items: [{ 
       fieldLabel: 'Line1', 
       name: 'line1', 
       allowBlank:false, 
      },{ 
       fieldLabel: 'Line2', 
       name: 'line2', 
      },{ 
       fieldLabel: 'City', 
       name: 'city', 
       allowBlank: false, 
      },{ 
       xtype:"combo", 
       fieldLabel:"State", 
       name:"state", 
       hiddenName:"combovalue" 
       }, { 
       fieldLabel: 'Zipcode', 
       name: 'zipcode', 
       allowBlank: false, 
      }] 
     },{ 
      title:'Phone Numbers', 
      layout:'form', 
      defaults: {width: 230}, 
      defaultType: 'textfield', 

      items: [{ 
       fieldLabel: 'Home', 
       name: 'home_phone', 
      },{ 
       fieldLabel: 'Cell', 
       name: 'cell_phone' 
      },{ 
       fieldLabel: 'Emergency', 
       name: 'emergency_phone' 
      }] 
     },{ 
      cls:'x-plain', 
      title:'Notes', 
      layout:'fit', 
      items: { 
       xtype:'htmleditor', 
       name:'notes', 
       fieldLabel:'Notes' 
      } 
     }] 
    }], 

    buttons: [{ 
     text: 'Save' 
    },{ 
     text: 'Cancel' 
    }] 
}) 

¿Cómo accedo a los campos de formulario con el nombre de establecer su valor de forma manual? Gracias

Respuesta

24

Es muy fácil:

  • obtener las manos en forma de panel (que por cierto es Ext.form.FormPanel y no sólo Ext.FormPanel):

    var formPanel = new Ext.form.FormPanel({...}); 
    
  • obtener el Ext.form.BasicForm subyacente

    var form = formPanel.getForm(); 
    
  • yo U puede entonces utilizar findField(name) para recuperar sus campos de formulario por sus nombres:

    var cellField = form.findField('cell_phone'); 
    
+0

Gracias, eso fue fácil. – jeremib

+0

Es mucho más fácil simplemente dar a cada campo una identificación y hacer 'Ext.getCmp ('cell_phone');'. También es más rápido (búsqueda directa de hash en lugar de un bucle interno cada vez). –

+0

@bmoeskau: Eso es correcto solo en caso de que aplique un 'id' a los campos de su formulario. 'Ext.getCmp()' (que es un acceso directo a 'Ext.ComponentMgr.get()') solo puede recuperar componentes por sus ids. Usando el código que el OP presentó, 'Ext.getCmp ('cell_phone');' no funcionará. –

19

También ellos se pueden configurar de forma masiva mediante el método() setValues.

por ejemplo:

Ext.getCmp('formname').getForm().setValues({ 
fielda: 'value1', 
fieldb: 'value2' 
}) 
3

Nice! trabajó para mí: D

Pero se puede establecer el valor por defecto:

...
artículos: [{
xtype: 'campo de texto',
fieldLabel: 'Nombre',
nombre: 'nombre',
Identificación: 'nombre',
valor: 'somevalue',
anclaje: '95%'
},
...

Cuestiones relacionadas