Select by default the first element of a combo on a Netzke::Basepack::Form

124 views
Skip to first unread message

Jorge Segura

unread,
Nov 8, 2013, 7:35:57 AM11/8/13
to net...@googlegroups.com
Hi, I just want to know how to select the first element of a comboBox defined by double underscore(: store__name) on a Netzke::Basepack::form
I now maybe this is a silly question but I can't figure out how to make it work...

class MyForm < Netzke::Basepack::Form
  def configure(c)
    super
    c.model = 'MyModel
    c.items = [
      {
        field_label: 'Stores',
        name: :store__name,
        scope: { id: Store.where(:name => 'Almacen Saldos de Obra').first().id},
        value: record && record.id ? record.store_id : nil
      }
    ]


Praveen K

unread,
Nov 8, 2013, 11:55:54 PM11/8/13
to net...@googlegroups.com
Hi Jorge,

You can do this by adding event handler for combo store. For Example:

       js_method :init_component, <<-JS
            this.callParent();
            Ext.each(this.query('combo'), function(cmp) {
                cmp.store.on('load', function(self, params) {
                  if (self.getCount() >= 2 && self.getAt(0).get("field1") == null) {
                        this.setValue(self.getAt(1));
                        this.fireEvent('select', this);
                    }
                }, cmp);
             }, this);
      JS

Jorge Segura

unread,
Dec 1, 2013, 9:11:21 PM12/1/13
to net...@googlegroups.com
Hi Praveen, thanks for your answer. Your code indeed initialize the combo when I click the button of the combo.
There is a way to initialize the combo when the form loads?
I mean, when the form loads I would like to see the combo initialized with the first item.

I hope you can help me.

Jorge Segura

unread,
Dec 1, 2013, 10:23:34 PM12/1/13
to net...@googlegroups.com
By the way, I have tried adding this line in the combo creation: data_store: {auto_load: true}, without sucess...

Praveen K

unread,
Dec 2, 2013, 12:15:54 AM12/2/13
to net...@googlegroups.com
Hi Jorge Segura,

You can do this by using following code:
js_method :init_component, <<-JS
      function(){
         this.callParent();
         Ext.each(this.query('combo'), function(cmp) {
           cmp.store.on('load', function(self, params) {
             if (self.getCount() >= 2 && self.getAt(0).get("field1") == null) {
               this.setValue(self.getAt(1));
               this.fireEvent('select', this);
            }
          }, cmp);
        }, this);
        this.refreshComboStore(<array of item_ids of combo stores>);
}
JS

 
js_method :refresh_combo_store, <<-JS
   function(item_ids) {
      item_ids = Ext.isArray(item_ids) ? item_ids : [item_ids]
      Ext.each(item_ids, function(item_id) {
         var cmp = this.query("combo#" + item_id)[0];
         cmp.reset();
         cmp.store.reload();
         console.log("Reloading Store of - " + item_id);
      }, this);
   } 
JS
Reply all
Reply to author
Forward
0 new messages