从json对象中提取字符串

时间:2022-09-21 19:19:16

I have an attribute in the extjs code defined as :

我在extjs代码中有一个属性定义为:

datajson: { 
    'name' : 'Hello',
     ....
}

Now I need to set the value of a textfield to the name defined inside this attribute.

现在我需要将textfield的值设置为此属性中定义的名称。

I have tried the following but its not working:

我尝试了以下但它不起作用:

Ext.getCmp('name').setValue((this.datajson.xyz.name).toString());

The id of the textfield whose value I am trying to set is 'name'

我试图设置其值的文本字段的id是'name'

Here is the full code structure.

这是完整的代码结构。

    Ext.define('Test', {
        extend : 'Ext.Container',

        alias : 'widget.myapp',

        datajson:{
                'xyz' : {
                    ...
                    "name" : 'Hello',
                    ...
                }
             },


        initComponent : function() {

            this.appcombo = Ext.create('Ext.form.field.ComboBox', {
                            store: Ext.create('Ext.data.Store', { 
                                   fields: ['text'], 
                                   data: [ 
                                        {text: "a"}, 
                                        {text: "b"}, 
                                        {text: "c"}
                                    ] 
                                }),
                                listeners: {
                                    select: function(combo, record, index) {
                                        Ext.getCmp('name').setValue((this.datajson.xyz.name).toString());
                                    }
                                }
                            });

           ...
           ...

           //Here we have a textfield within a Panel whose id is 'name'

So basically I am loading the page and then selecting one value in combo box which serves as the event to load the values in the text-fields with the data which is in datajson.

所以基本上我正在加载页面然后在组合框中选择一个值作为事件来加载文本字段中的值与datajson中的数据。

EDIT: More information added.

编辑:添加了更多信息。

1 个解决方案

#1


The scope within your select function no longer contains datajson. To continue to use this.datajson you can pass the required scope into the function using bind

select函数中的范围不再包含datajson。要继续使用this.datajson,您可以使用bind将所需范围传递给函数

Ext.application({
name: 'Fiddle',

datajson: {
    'name': 'Hello'
},

displayName: function (combo, record, index) {
    Ext.getCmp('name').setValue((this.datajson.name));
},

launch: function () {
    Ext.create('Ext.form.Panel', {
        title: 'Contact Info',
        width: 300,
        bodyPadding: 10,
        renderTo: Ext.getBody(),
        items: [{
            xtype: 'combo',
            store: Ext.create('Ext.data.Store', {
                fields: ['text'],
                data: [{
                    text: "a"
                }, {
                    text: "b"
                }, {
                    text: "c"
                }]
            }),
            listeners: {
                select: this.displayName.bind(this)
            }
        }, {
            xtype: 'textfield',
            id: 'name',
            fieldLabel: 'Name'
        }]
    });

}
});

#1


The scope within your select function no longer contains datajson. To continue to use this.datajson you can pass the required scope into the function using bind

select函数中的范围不再包含datajson。要继续使用this.datajson,您可以使用bind将所需范围传递给函数

Ext.application({
name: 'Fiddle',

datajson: {
    'name': 'Hello'
},

displayName: function (combo, record, index) {
    Ext.getCmp('name').setValue((this.datajson.name));
},

launch: function () {
    Ext.create('Ext.form.Panel', {
        title: 'Contact Info',
        width: 300,
        bodyPadding: 10,
        renderTo: Ext.getBody(),
        items: [{
            xtype: 'combo',
            store: Ext.create('Ext.data.Store', {
                fields: ['text'],
                data: [{
                    text: "a"
                }, {
                    text: "b"
                }, {
                    text: "c"
                }]
            }),
            listeners: {
                select: this.displayName.bind(this)
            }
        }, {
            xtype: 'textfield',
            id: 'name',
            fieldLabel: 'Name'
        }]
    });

}
});