2

我有点像 ExtJS 菜鸟。I have a loaded up an ExtJS ComboBox (call it CBa) where the value field contains a JSON string that, when selected, should be loaded into a second combobox (CBb).

我能找到的所有示例都告诉我如何从外部 URL 加载 ComboBox,但在这种情况下,我想从本地已有的字符串加载它。

我在每个 ComboBox 上放置了什么魔法来实现这一点?

4

1 回答 1

1

您可以像这样创建本地组合:

// The data store containing the list of states
var states = Ext.create('Ext.data.Store', {
    fields: ['abbr', 'name'],
    data : [
        {"abbr":"AL", "name":"Alabama"},
        {"abbr":"AK", "name":"Alaska"},
        {"abbr":"AZ", "name":"Arizona"}
        //...
    ]
});

var combo2 = Ext.create('Ext.form.ComboBox',{

   // combo config;
});
// Create the combo box, attached to the states data store
Ext.create('Ext.form.ComboBox', {
    fieldLabel: 'Choose State',
    store: states,
    queryMode: 'local', //makes it a local combo. Don't need a url.
    displayField: 'name',
    valueField: 'abbr',
    renderTo: Ext.getBody(),
    listeners : {
          select : function() {
                var value = this.getValue();
                combo2.setValue(value);
          }
    }
});

使用 的select事件combo将选定的值设置到第二个组合中。请注意,选择的值应该是在data中定义的值storecombo2以便对其进行设置。在此处阅读 setValue 的文档以获取确切信息。

阅读评论后编辑:

您可以动态设置第二个组合的商店。将上面提到的选择事件更改为:

select : function()
{
      var dataArray = [],data = this.getValue(); //Json string
      dataArray.push(Ext.decode(data)); //In case there is only one object in the string instead of any array.
      combo2.getStore().loadData(dataArray, false); //dataArray is automatically parsed into Model Objects. Second param to allow append to existing store data.
}

}

于 2012-10-20T13:12:38.913 回答