【问题标题】:How to bind array to arrystore in order to populate combo in extjs如何将数组绑定到 arrystore 以在 extjs 中填充组合
【发布时间】:2011-08-03 15:40:22
【问题描述】:

我有数组

var cars = new Array('audi','benz','citron','nissan','alto');

我想将此数据添加到如下所示的数组存储中

 var myStore = new Ext.data.ArrayStore({
        data   : cars ,
        fields : ['names']
    });

关于将此数组存储绑定到组合

 var myCombo = new Ext.form.ComboBox({
        store: myStore ,
        displayField: 'name',
        valueField: 'name',
        typeAhead: true,
        mode: 'local',
        forceSelection: true,
        triggerAction: 'all',
        emptyText: 'Select a state...',
        selectOnFocus: true,

    });

该组合仅将数组中每个单词的首字母显示为 a、b、c、n、a

我如何正确显示组合,因为我正在使用的数组是以编程方式填充然后绑定到 arraystore

【问题讨论】:

    标签: javascript arrays extjs combobox store


    【解决方案1】:

    ArrayStore 使用的数据格式是数组数组。如下重新格式化您的商店数据应该允许它工作:

    var cars = [['audi'], ['benz'], ['citron'], ['nissan'], ['alto']];
    

    从您的格式转换为所需的格式非常简单:

    for ( var i = 0, c = cars.length; i < c; i++ ) {
        cars[i] = [cars[i]];
    }
    

    【讨论】:

    • 我知道数据存储数据格式是这样的,但我需要以某种方式将数组转换为数组存储以便将其绑定到组合。
    • 如果无法控制原始格式,在代码中转换数组是一件简单的事情。答案已更新。
    【解决方案2】:

    或者,如果您只是将数组作为存储配置传递,那也可以(假设是最新版本):

    new Ext.form.ComboBox({
        store: ['Audi', 'Benz', 'Citron', 'Nissan', 'Alto']
    });
    

    请注意,如果是这种情况,则无需指定 displayField/valueField。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-11-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-13
      相关资源
      最近更新 更多