【问题标题】:ExtJS: Combobox after reload store dont set valueExtJS:重新加载后的组合框存储不设置值
【发布时间】:2011-04-16 10:57:00
【问题描述】:

我想我有一个非常流行的问题,但现在没有找到答案。 :) 我有 2 个类似的组合框 - 起初我通过 id 设置我的值 - comboT.setValue("22763"); 并正确设置了与此 id 链接的文本值。 在第二个组合框我首先重新加载存储(jsonstore)然后设置值 - comboC.setValue("3"); 但是这个组合只设置 ID 而不是文本值(如果我打开列表我可以看到什么组合正确标记了文本值。之后(如果列表只是关闭没有选择)文本值正确显示在组合中。 如何解决这个问题呢? 谢谢。

【问题讨论】:

    标签: combobox extjs reload store


    【解决方案1】:

    像这样,因为我是从记忆中做的,所以语法可能略有偏差:

    var val = 3;
    var store = comboC.getStore();
    store.on("load", function() {
       comboC.setValue(val);
    }):
    store.load();
    

    【讨论】:

      【解决方案2】:

      加载存储是异步的,您可能希望将设置新值移动到store.load({...})callback: 事件处理程序中,否则,您会在实际加载存储之前设置值。

      编辑:为了完整起见,举个例子,所以你有一个替代版本(在某些情况下,可能不希望将回调绑定到商店本身,就像 ormuriauga 所做的那样):

      var val = 3;
      var store = comboC.getStore();
      store.load({
         callback: function() {
            comboC.setValue(val);
         }
      });
      

      【讨论】:

        【解决方案3】:

        关于如何通过在底层数据存储中搜索字符串来设置组合框值的另一个示例。我可以通过使用这些答案中的示例作为基线来编写代码:

        //The store's data definition must have at least a data.id field defined    
        set_combobox_value_from_store = function (combobox, valueField, value) {
        //Get a reference to the combobox's underlying store
        var store = combobox.getStore();
        store.load({
            callback: function () {
                //Find item index in store
                var index = store.find(valueField, value, false);
                if (index < 0) return;
                //Get model data id
                var dataId = store.getAt(index).data.Id;
                //Set combobox value and fire OnSelect event
                combobox.setValueAndFireSelect(dataId);
            }
        });
        

        【讨论】:

          【解决方案4】:

          在 extjs 4.1 中,当 Model 中的 valueField 类型为“字符串”时,combo.setValue() 似乎有效。这是我的代码

          Ext.define('Model.CboObras', {
                         extend: 'Ext.data.Model',
                          idProperty: 'co_obra',
                          fields: [{
                              name: 'co_obra',
                              type: 'int'
                          }, {
                              name: 'nb_obra',
                              type: 'string'
                          }]
                      });
          

          这不起作用。

          当我将代码更改为:

             Ext.define('Model.CboObras', {
                extend: 'Ext.data.Model',
                idProperty: 'co_obra',
                fields: [{
                   name: 'co_obra',
                       type: 'string'
                    }, {
                       name: 'nb_obra',
                      type: 'string'
                   }]
             });
          

          之后我使用这个:

              var store = comboC.getStore();
              store.load({
             callback: function() {
                comboC.setValue(val);
             }
          });
          

          它现在就像一个魅力!

          【讨论】:

            猜你喜欢
            • 2013-01-11
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2020-01-13
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多