PDA

View Full Version : ComboBox default selection



gaboo
4 Dec 2009, 1:40 AM
Hello,

I would like to set the default selection to ComboBox, but unfortunately I
cannot make it. I call the setValue(rec) (or setSelection(java.util.List<D> selection)) method, but in this case all the
other items of store disappear and only the selected item can be seen in the
list.
As i remember it worked with the version of 2.0.1, but does not work with the version of
2.1

Test code:


public class ComboTest implements EntryPoint {

public void onModuleLoad() {
FormPanel p = new FormPanel();

ModelData m1 = new BaseModelData();
m1.set("name", "first");
ModelData m2 = new BaseModelData();
m2.set("name", "second");

ComboBox<ModelData> cb = new ComboBox<ModelData>();
cb.setFieldLabel("Combobox");
cb.setDisplayField("name");
cb.setStore(new ListStore<ModelData>());
cb.getStore().add(Arrays.asList(m1, m2));
p.add(cb);

cb.setValue(m2);

RootPanel.get().add(p);
}
}

Best regards,

Gaboo

gaboo
21 Dec 2009, 12:41 AM
I didnt find any solution so i use gwt's ListBox instead of gxt's ComboBox:


public static AdapterField _createLB(String label, String name, List<BaseModelData> datas, int defSelection) {
ListBox lb = new ListBox();
lb.setName(name);
int x = 0;
for (BaseModelData i : datas) {
int val = ((Integer)i.get("id")).intValue();
lb.addItem((String)i.get("name"), Integer.toString(val));
if (val == defSelection)
lb.setSelectedIndex(x);
x++;
}

AdapterField f = new AdapterField(lb);
f.setFieldLabel(label);
return f;
}

public static void _setLBSelect(final int id, final ListBox lb) {
int c = lb.getItemCount();
for (int i = 0; i < c; i++) {
int val = Integer.parseInt(lb.getValue(i));
if (val == id) {
lb.setSelectedIndex(i);
break;
}
}
}

sven
21 Dec 2009, 12:42 AM
cb.setTriggerAction(TriggerAcion.ALL);

gaboo
21 Dec 2009, 1:04 PM
Thanks Sven!

me2
12 Jan 2010, 7:38 AM
In version 2.1 the ComboBox was changed to have lazy render by default - we've found this caused problems with setting default values, so had to call setLazyRender(false) on combo boxes that need a default value.

Abraham1
22 Mar 2010, 9:21 AM
Interesting post dude….Forums are always helpful in one way or the other, anyways. Thanks for giving out information. It’s really nice and mean full.

WesleyMoy
18 Aug 2010, 11:05 PM
cb.setTriggerAction(TriggerAcion.ALL);

Expanding a bit on this answer: when you set that default value, the combo box contains the text "second". When you expand the combo box, you are only showing the results that start with the text "second". By setting TriggerAction to ALL, you are choosing not to have the combo box filter at all.

You can try this with the two values "apple" and "applepie" for comparison.