【问题标题】:How to get data from fieldset in Sencha Touch 2?如何从 Sencha Touch 2 中的字段集中获取数据?
【发布时间】:2012-06-03 14:35:21
【问题描述】:

我在 Sencha Touch 2 中有一个字段集,如下所示:

{
                    id:'contactForm',
                    xtype: 'fieldset',
                    title: 'Information',
                    items: [
                        {
                            xtype: 'textfield',
                            label: 'First Name',
                            placeHolder: 'Your First Name',
                            name:'firstName',
                            id:'firstName',
                        },
                        {
                            xtype: 'textfield',
                            label: 'Last Name',
                            placeHolder: 'Your Last Name',
                            name:'lastName'
                        },
                        {
                            xtype: 'emailfield',
                            label: 'Email',
                            placeHolder: 'email@example.com'
                        },
                        {
                            xtype: 'button',
                            height: 37,
                            style: 'margin-left:35%',
                            width: 100,
                            iconAlign: 'center',
                            text: 'Submit',
                            action:'ContactSubmit'                        
                        },
                        {
                            xtype: 'hiddenfield',
                            id: 'HNumberOfBedRoom',
                            value:'2'
                        },
                        {
                            xtype: 'hiddenfield',
                            id: 'HPetFriendlyId',
                            value:'2'
                        }                
                ]
}

在我的控制器中, 我有以下内容:

refs: { contactForm: '#contactForm' }

我可以使用

检索值

var frmItems=this.getContactForm().getItems();
console.log(frmItems.items[1]._value);

这很好,但我想检索类似的值

frm.get('name/id of component')

有什么办法可以做到吗?

【问题讨论】:

    标签: touch extjs


    【解决方案1】:

    您应该能够将id 分配给您的字段,然后分配Ext.getCmp('-yourId-');Ext.get('-yourId-');

    (http://docs.sencha.com/touch/2-0/#!/api/Ext.Component-cfg-id)

    【讨论】:

      【解决方案2】:

      使用 Ext.form.Panel 作为您的主要容器。

      示例 sn-p

      Ext.define('App.view.ContactForm',
      {
        extend : 'Ext.form.Panel',
        xtype : 'contactform',
        id : 'contactForm',
        config : {
          items : [
            { 
              xtype : 'fieldset',
              items : [
                 {
                   {
                      xtype: 'textfield',
                      label: 'First Name',
                      placeHolder: 'Your First Name',
                      name:'firstName',
                   },
               ]
             }
      });
      

      在你的控制器中

      refs : { contactForm : '#contactForm' }

      然后在你的函数中你可以做

      this.getContactForm().getValues().firstName
      

      (使用字段的名称值)

      var vals = this.getContactForm().getValues();
      vals.firstName;
      

      如果您绝对可以提供帮助,请避免在顶层使用 Ext.getCmp() 或平面 Ext.get()。如果您正在构建自定义控件,则可能必须使用这些控件,否则您将自己弄得太难了。

      【讨论】:

      • 这样更简单、更有条理且可扩展。感谢您指出这一点。
      【解决方案3】:

      不要太挣扎

      首先将 id 分配给该字段,然后使用 id 获取值。

           {
                xtype: 'textfield',
                id: 'username',      // id
                name: 'username',
                placeHolder: '------UserName------',                 
              },
      
      
               Ext.getCmp('username').getValue();     // now get value using that id..
      

      【讨论】:

        【解决方案4】:
        refs:[
                {
                    ref:'contentForm',
                    // selector:'#contentForm'
                    contentForm:'#contentForm'
                }
            ],
        

        -

        var form = Ext.getCmp('contentForm');
        
        console.log(form.getValues());
        

        【讨论】:

          【解决方案5】:

          itemId: firstName 分配给textfield

          Controller,你想在哪里获取值,使用这个:

          Ext.ComponentQuery.query('textfield[itemId=firstName]')[0].getData();
          

          【讨论】:

            猜你喜欢
            • 2013-04-14
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2012-03-29
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2012-10-15
            相关资源
            最近更新 更多