19

I have a problem with ExtJs combobox, consider I have a combobox that has 4 items and a callback function in select event on combobox.

When I'm going to set the combobox selected value with setValue(), ExtJs don't fire select event.

How can I fix this problem?

Should I fire this event after setValue() by myself?

Narendra Jadhav
  • 10,052
  • 15
  • 33
  • 44
Afshin Mehrabani
  • 33,262
  • 29
  • 136
  • 201

2 Answers2

21

I can tell you what the sencha support told for that one year ago:

Well, event is, by definition, a function call triggered by a user action and there's no user action when you call select.

Anyway, the "fix" is easy: you know that you call select so just after calling select you can call your select listener function.

It depends on your case what you should do. For me I hanged my implementation so that I was able to call re responsible method but on the other hand I don't see any downside when you fire the event yourself. So I think it is up to you which approach you like more.

Here is a example how you can fire the event by yourself (partly from the comment by @JohanHaest)

To make it simple I say you have only single selection enabled

var record = combo.store.getById(id);
combo.select(id);
combo.fireEvent('select', combo, record);

or

combo.select(model);
combo.fireEvent('select', combo, model);

There is a possible Hack in the current release (4.1.3) when setting a second argument on the select method to true. This will, according to the sourcecode, fire the select event. But the First argument has to be a Model instance.

// source-snipped
select: function(r, /* private */ assert)

So calling

combo.select(model, true);

will fire the select event but this behavior may change at any time (version) cause the assert is marked as private

Community
  • 1
  • 1
sra
  • 23,820
  • 7
  • 55
  • 89
  • Thanks for answer. So, I should trigger `select` event myself, right? – Afshin Mehrabani Jan 10 '13 at 07:56
  • Yes, either that or call the select listener function yourself. – Christiaan Westerbeek Jan 10 '13 at 08:05
  • Ok, fine. But if I fire `select` event manually, I should pass the arguments to the event also, correct? I mean `select(combo, records, eOpts)`. – Afshin Mehrabani Jan 10 '13 at 08:08
  • 3
    The following code fires event: `combo.fireEvent('select', combo, records);` – Johan Haest Jan 10 '13 at 08:09
  • 1
    @AfshinMehrabani Well, there is nothing more to say except that you have to fetch the record(s) by yourself from the store bound to the combo – sra Jan 10 '13 at 08:12
  • @JohanHaest Thanks for code but I should have that `combo` and `records` variable to pass it to the event. I don't think it's a good approach because I should manually manage the combobox! – Afshin Mehrabani Jan 10 '13 at 08:13
  • @sra Dear lord :) Thank you guys for answering me. BTW, I think this is a big bug in the ExtJs that should fixed as soon as possible. – Afshin Mehrabani Jan 10 '13 at 08:13
  • @AfshinMehrabani...setValue() fires `change` event And `select` event Fires when at least one list item is selected. – Avinash T. Jan 10 '13 at 09:00
  • 2
    @AvinashT. No, that is wrong. When the picker is closed the event is not fired. You have to use a hack to archive this as I stated in my edit – sra Jan 10 '13 at 09:07
  • @AvinashT. Read **comments** of this event on Sencha docs: http://docs.sencha.com/ext-js/4-1/#!/api/Ext.form.field.ComboBox-method-select – Afshin Mehrabani Jan 10 '13 at 10:23
  • this assertion is wrong: `event is, by definition, a function call triggered by a user action`. There can be user events, system events, state events, ... – Skrol29 Jun 13 '19 at 08:16
3

I came across this because I was incorrectly using valueField on combobox... I'm not sure what this is supposed to do but if you have it set incorrectly it breaks the combobox - it prevents the change event from firing at all and stops the select event from firing more than once.

My guess is its so you can have multiple objects in the list that represent the same item and the valueField represents the key so if you set it wrongly then the key is undefined for everything and therefore the same for everything. Which means nothing after the first set counts as a new value.

It seems like pretty niche behaviour (and not what I wanted) so I just stripped it out in my case and things started working.


Also I think the event you want is the change event - the documentation (for 4.1) says

Fires when the value of a field is changed via the setValue method.

whereas select only fires when someone clicks the combobox

Just a bit of an aside but I'd also question using an event for this - you know you've called setValue so why don't you just do the thing that you want to happen after you call set value. Events have a tendency to make code more confusing and if you dont need multicasting then its normally better to not bother with them.

JonnyRaa
  • 7,559
  • 6
  • 45
  • 49
  • Thanks a lot! I had some testing data in combo with same value, and I couldn't fire change. – Djomla Jan 30 '15 at 16:31