Question

I have a window that displays values about a user in a popup when a certain cell is clicked.. in my UserController I load a json of the user based on id, is there any way to pass particular values of this json into my window? I know I could use .getForm().setValues() if it were a form. Can I nest a form inside my window?

Controller snippet:

loadUserDetails: function(userId) {
        var view = Ext.widget('userdetails').show();
        Ext.Ajax.request({
            url:  /user/get.htm?alt=json',
                params: { id: userId },
            success: function(response) {
                var json = Ext.JSON.decode(response.responseText);
            }
        });
    }

View snippet (window):

Ext.define('App.view.user.UserDetails', {
    extend: 'Ext.window.Window'
    ,alias: 'widget.userdetails'
    ,id: 'userdetails'
    ,title: 'User Details'
    ,height: 300
    ,width: 380

    ,initComponent: function() {
        var me = this;
        this.items = [{
            xtype: 'fieldset',
            title: 'Information',
            margin: '5',
            width: 350,
            height: 250,
            defaults: {xtype: 'displayfield', margin: '3', labelWidth: 100, width: 300},
            items: [{
                id: 'id',
                width: 150,
                fieldLabel: 'User Id'
            },{
                id: 'email',
                width: 250,
                fieldLabel: 'User Email'
            }]

        }];
        this.callParent(arguments);
    }
});
Was it helpful?

Solution

Given this class:

Ext.define('App.view.user.UserDetails', {
    extend: 'Ext.window.Window'
    ,alias: 'widget.userdetails'
    ,id: 'userdetails'
    ,title: 'User Details'
    ,height: 300
    ,width: 380
    ,items: [{
        xtype: 'fieldset',
        title: 'Information',
        margin: '5',
        width: 350,
        height: 250,
        defaults: {xtype: 'displayfield', margin: '3', labelWidth: 100, width: 300},
        items: [{
            // you should avoid using fixed id that will prevent using the component
            // multiple times
            // id: 'id',
            width: 150,
            fieldLabel: 'User Id'

            ,itemId: 'userId' // <= add item id for easy retrieval
        },{
            // id: 'email',
            width: 250,
            fieldLabel: 'User Email'

            ,name: 'userEmail' // <= or a name, as is logical for a field
        }]
    }]
});

You can use component queries to get a reference to your field and set its value:

var win = Ext.create('App.view.user.UserDetails', {
    autoShow: true
});

// query by itemId
win.down('#userId').setValue(123);

// or by name
win.down('field[name=userEmail]').setValue('user@example.com');

Or you can nest a form in your window so that you can use setValues (and other form features):

Ext.define('App.view.user.UserDetails', {
    extend: 'Ext.window.Window'
    ,alias: 'widget.userdetails'
    ,id: 'userdetails'
    ,title: 'User Details'
    ,height: 300
    ,width: 380
    ,layout: 'fit'
    ,items: [{
        xtype: 'form'
        ,items: [{
            xtype: 'fieldset',
            title: 'Information',
            margin: '5',
            width: 350,
            height: 250,
            defaults: {xtype: 'displayfield', margin: '3', labelWidth: 100, width: 300},
            items: [{
                width: 150,
                fieldLabel: 'User Id'
                // name is required for setValues to work
                ,name: 'userId'
            },{
                // id: 'email',
                width: 250,
                fieldLabel: 'User Email'
                ,name: 'userEmail'
            }]
        }]
    }]
});

var win = Ext.create('App.view.user.UserDetails', {
    autoShow: true
});

win.down('form').setValues({
    ...
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top