Search code examples
functionextjshiddenxtype

Extjs - textlabel value change on hidden field content


I'm trying to change the value of a displayfield inside a form acording to a value on another field (hidden).

The hidden field retrives it's value from mapping and the displayfield will also retrive content from mapping, but from two diferent values (mapping1, mapping2) acording to the hidden value. This is what I'm trying to do :

mapping: ['myHidden''mapping1','mapping2'],
form: [{
    fieldLabel: 'myHidden',
    xtype: 'hidden',
    name: 'myHidden',
    listeners: {
          'change': function(){
                   if(this.getValue=='X'){
                         this.up('form').findfield('myText').setName('mapping1');
                   }
                   else{
                         this.up('form').findfield('myText').setName('mapping2');
                    }
                }
            }
    },{
        fieldLabel: 'myText', 
        xtype: 'displayfield'
    }]      

As I undestand the mapping to 'myText' is given by the name tag, but since I wan't to change it's value How should I do it?

I just want to change the mapping of 'myText' on form load based on the value that I'm getting from server trough 'myHidden' field.

thank you for you help.


Solution

  • I've been able to get the first example to work as I wanted. I don't know if it is the best solution but I'll place it here so you can use it if you're facing the same problem.

    Basically I've mapped the values to hidden fields and then I change the content of the displayfield according to the value on myHidden

    mapping: ['myHidden''mapping1','mapping2'],
    form: [{
        fieldLabel: 'mapping1',
        xtype: 'hidden',
        id: 'mapping1',
    },{
        fieldLabel: 'mapping2',
        xtype: 'hidden',
        id: 'mapping2',
    },{
        fieldLabel: 'myHidden',
        xtype: 'hidden',
        name: 'myHidden',
        listeners: {
              'change': function(){
                       if(this.getValue=='X'){
                             Ext.getCmp('myText').setValue(Ext.getCmp('mapping1').getValue());
                       }
                       else{
                             Ext.getCmp('myText').setValue(Ext.getCmp('mapping2').getValue());
                        }
                    }
                }
        },{
            fieldLabel: 'myText', 
            xtype: 'displayfield',
            id: 'myText'
        }]