PDA

View Full Version : Listen for ComboBox value changes



winter
13 Feb 2012, 10:42 AM
When a user selects an item from the combobox list, you could catch that in gxt2 by

combo.addListener(Events.Select,new Listener <FieldEvent>(){...}


in gxt3 you can don't seem to have such a method. The combo.addValueChangeHandler only triggers on focus out of the component. Is this expected behaviour? How are you supposed to listen for value changes before the component looses focus?

rohdef
14 Feb 2012, 4:28 AM
I've been looking for something similar for other form elements, such as responding to when a users stops typing in a field. I would argue that the ValueChanged or something similar should fire here, since the value is change whether the user leaves the field or not. Unfortunately I've not found any way to achieve this (admitted, I didn't try that hard).

Maybe enabling auto validate will solve this?

Otherwise I think the ComboBox has one advantage in your case, you're most likely able to work around it by listening for collapse events. Although crude and ugly, it should work.

Colin Alworth
15 Feb 2012, 9:47 AM
We are planning on adding a SelectEvent to the combobox again. In the meantime, some users have found it possible to use the collapse event to see that the field is closing and to detect changes.

As for ValueChangedEvent, we plan to use this as it was used in 2.x - only fired when the field is blurred, and then only when the value has changed. The basic reasoning there is that this is how the dom event works when editing an input, and how other GWT fields work as well.

Keystrokes can be detected on any field with the various key events. A Timer can be used to watch for intervals where the user hasn't made any changes, cancelling and re-scheduling each event. This whole setup could be made into a plugin suitable for any Field, so you can build this functionality in as you need it.

WesleyMoy
3 Apr 2012, 1:59 PM
ComboBox now fires SelectionEvent when an item in the drop down list is selected. This allows you to react to the user choosing an item from the drop down list without waiting for the user to move focus away from the combo box.

Look for this change in the Release Candidate build.

zoec
4 Mar 2014, 4:37 AM
Hi,

I've just encountered this issue. Combo boxes are often used to create change in other widgets, such as disabling/enabling appropriately based on selection. A user will expect the value change to be picked up without having to make an additional click.

I appreciate that we can use the SelectionHandler instead, but this doesn't really solve the problem.

It might be that you should have a separate Drop down control (which is how we are using it - a user cannot type into our boxes) for this type of scenario which behaves like a DOM SELECT element. If you have one and I missed it, I apologise in advance.

Neilcoder
12 May 2014, 7:19 AM
+1

It would be good if there was some kind of method (unless I've missed it some how) setting the combobox to automtically set the value to cause the value change event to fire (which I'm having problems with manually but if you think that's off topic please ignore for the sake of thread spoiling). e.g. comboboxInstance.setValueChangeOnSelection(true); I beleive there used to be something like this in GXT 2.25

http://www.sencha.com/forum/showthread.php?160547-%28Beta%29-ComboBox-issues/page3

Colin Alworth
12 May 2014, 7:51 AM
Try field.setValue(newValue, true) - Field (CellComponent actually) implements HasValue, which has http://www.gwtproject.org/javadoc/latest/com/google/gwt/user/client/ui/HasValue.html#setValue%28T,%20boolean%29



/**
* Sets this object's value. Fires
* {@link com.google.gwt.event.logical.shared.ValueChangeEvent} when
* fireEvents is true and the new value does not equal the existing value.
* <p>
* It is acceptable to fail assertions or throw (documented) unchecked
* exceptions in response to bad values.
*
* @param value the object's new value
* @param fireEvents fire events if true and value is new
*/
void setValue(T value, boolean fireEvents);

Neilcoder
13 May 2014, 2:43 AM
Thanks Colin,

I think I see the problem I was having with setValue() now...

I was trying to use that before, but without the boolean parameter, the fireEvents defaults to false, so my deduction is that's why I wasn't getting a value change event?



/**
* Sets this object's value without firing any events. This should be identical to calling setValue(value, false).
*
* It is acceptable to fail assertions or throw (documented) unchecked exceptions in response to bad values.
*
* Widgets must accept null as a valid value. By convention, setting a widget to null clears value, calling getValue()
* on a cleared widget returns null. Widgets that can not be cleared (e.g. CheckBox) must find another valid meaning
* for null input.
*
*
* @param value the parsed value
*/
@Override
public void setValue(T value) {
setValue(value, false);
}

I was using setValue(value) before so that might have been what was causing me issues, i think


Seems to work as expected now.

Here's an example if anyone should be looking for it;



comboInstance.addSelectionHandler(new SelectionHandler<String>(){

@Override
public void onSelection(SelectionEvent<String> event) {
System.out.println("comboInstance - onSelection" + event.getSource().toString());
comboInstance.setValue(event.getSelectedItem(), true);

}

});

comboInstance.addValueChangeHandler(new ValueChangeHandler<String>(){

@Override
public void onValueChange(ValueChangeEvent<String> event) {
System.out.println("comboInstance - onValueChange" + event.getSource().toString());
//TODO Enter the code or method call here you want carried out on selection

}

});