开发者

ExtJS: Combobox after reload store dont set value

I think I have a very popula开发者_开发知识库r problem, but not found answer for it now. :) I got 2 similar comboboxes - at first i set my value by id - comboT.setValue("22763"); and it properly set a text value linked with this id. At second combobox i at first reload store(jsonstore) and then set value - comboC.setValue("3"); But this combo set only ID not text value (if i open list i can see what combo properly marked text value. And after (if list simply close without select) text value properly displayed at combo. How to solve this problem? Thanks.


Something like this, syntax may be slightly off since I am doing it from memory:

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


Loading the store is asynchronous, you might want to move setting the new value into the callback: event handler of store.load({...}), because otherwise, you set the value before the store is actually loaded.

EDIT: for completeness, an example, so you have an alternative version (in some cases it might be undesireable to bind the callback to the store itself, like ormuriauga did):

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


One more example on how to set the combobox's value by searching a string in the underlying data store. I was able to code this by using the samples in these answers as a baseline:

//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);
    }
});


In extjs 4.1 looks like combo.setValue() works when the type of valueField in the Model is "string". this was my code

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

this does not work.

When I changed my code to this:

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

After that I use this:

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

it now works like a charm!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜