【问题标题】:How to select a combobox value in ExtJs?如何在 ExtJs 中选择组合框值?
【发布时间】:2012-06-15 12:54:12
【问题描述】:

我试图在加载到商店后在下拉列表中简单地选择一个项目。这不起作用:

Ext.getCmp('ddlModel').setValue(aircraftStore.getAt(0).data.ModelTypeCode);

这会引发异常:

Ext.getCmp('ddlModel').selectByValue(aircraftStore.getAt(0).data.ModelTypeCode);

这是一个例外: 'this.view' 为 null 或不是对象

有人知道如何在 ExtJs 中做到这一点吗?

【问题讨论】:

  • 因为文档建议使用 setValue。 “必须加载商店并扩展列表才能使此功能起作用,否则使用 setValue”。它按预期对我有用

标签: extjs extjs3


【解决方案1】:

我认为正确的方法是使用此属性配置您的组合框:

autoSelect: true

true 选择数据存储收集的第一个结果(默认值 为真)。错误值需要从 下拉列表设置组件值,除非值 (typeAheadDelay) 是真的

【讨论】:

    【解决方案2】:

    在我的情况下,我需要获取组合框的 id,然后在 if 中进行比较,从而能够传递第二个窗口,使用此方法并且它有效。

    var ValorSeleccionado = Ext.getCmp('cmb_tipoderol_usr').getValue(); // 'cmb_tipoderol_usr' is the id of the combobox.
    

    然后对比动作

    if (ValorSeleccionado == 1 ) { Do Action }
    

    【讨论】:

      【解决方案3】:

      在许多情况下,您可能希望将组合框设置为某个索引。在 ExtJs 4.2 中,您可以这样做:

      function setIndex(combobox, value)
      {
          combobox.setValue(combobox.store.data.items[value].data.field1);
      }
      

      【讨论】:

        【解决方案4】:

        我创建了一个函数来设置 ExtJs 中组合框的值:

        function ComboSetter(comboBox, value) {
            var store = comboBox.store;
            var valueField = comboBox.valueField;
            var displayField = comboBox.displayField;
        
            var recordNumber = store.findExact(valueField, value, 0);
        
            if (recordNumber == -1)
                return -1;
        
            var displayValue = store.getAt(recordNumber).data[displayField];
            comboBox.setValue(value);
            comboBox.setRawValue(displayValue);
            comboBox.selectedIndex = recordNumber;
            return recordNumber;
        }
        

        【讨论】:

        • 我花了6个小时才知道如何显示正确的字符串并在comboBox.setRawValue(displayValue);找到它
        【解决方案5】:

        Ext.getCmp('ddlModel').select(aircraftStore.getAt(0));

        【讨论】:

        • ExtJs 3.4 中一定有一个bug,因为selectByValue 和select 都会抛出异常,即使下拉列表被展开。
        猜你喜欢
        • 1970-01-01
        • 2011-08-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-10-24
        • 1970-01-01
        • 2012-06-29
        • 1970-01-01
        相关资源
        最近更新 更多