如何将对象转换为数组,并将中的值设置为sencha中的selectfield

How to convert object to array and set value in to selectfield in sencha

本文关键字:设置 sencha selectfield 中的 对象 转换 数组      更新时间:2023-09-26

实际上我从服务器上得到了国家:英格兰,那么如何设置selectfield那个英格兰。我无法在选择字段中设置。

在其他文本字段中,我是这样管理的;

Ext.getCmp('email').setValue(email);

但为什么不在selectfield中工作呢

我有很好的sencha。现在我想转换数组。

var testcountry=  Ext.create('Test.store.CountryList');
console.log("Length of Country==="+testcountry.getCount());
console.log("Country==="+testcountry);
for (var i = 0 ; i < testcountry.getCount() ; i ++){
    console.log("Country Value--"+testcountry.getAt(i).data.country);
}

这里console.log("Country Value--"+testcountry.getAt(i).data.Country)

控制台中的所有国家/地区打印未定义。

型号:

Ext.define('Test.model.countryList', {
    extend: 'Ext.data.Model',
    alias: 'widget.countrylist', 
    config: {
        fields: ['text']
    }
});

店内:

Ext.define('Test.store.countryList', {
    extend: 'Ext.data.Store',
    config: {
    storeId: 'countryList',
    model: 'Test.model.countryList',
    data: [
    { text: ''},
    { text: 'Japan'},
    { text: 'India'},
    { text: 'Spain'},
    { text: 'Australia'},
    { text: 'Sudan'},
    { text: 'Brazil'},
    { text: 'Mexico'},
    { text: 'England'},
    { text: 'Chaina'},
   ]
}
});

在控制台中,如下所示:

Length of Country===9 
Country===[object Object] 
Country Value--undefined 
Country Value--undefined 
Country Value--undefined 
Country Value--undefined 
Country Value--undefined 
Country Value--undefined 
Country Value--undefined 
Country Value--undefined 

如何打印所有国家/地区。

您的For循环片段应该如下所示:

for (var i = 0 ; i < testcountry.getCount() ; i ++){
    console.log("Country Value--"+testcountry.getAt(i).getData().text);
}

您需要提到您在模型中使用的确切字段名称。

更新:

如果你想在选择字段中填充这个,你会这样做:

{
                        xtype: 'selectfield',
                        placeHolder: 'Select Country',
                        store: 'countryList'
                    }