PDA

View Full Version : SimpleComboBox usage with objects



Jenya
23 Aug 2010, 1:00 AM
I need to fill-in SimpleComboBox with list of objects which extend BaseModel.

Does anyone know where from the display values are taken?

When I use ComboBox, list of options is correctly displayed via using property "DisplayField" but the same approach for the SimpleComboBox doesn't work.

And what is even more strange, once the item is selected it is correctly shown.

See images below:

This is how list is displayed:
http://dl.dropbox.com/u/1257769/sencha/combo1.gif

When the item is selected (this is probably a reauls of .toString() which i overrided)
http://dl.dropbox.com/u/1257769/sencha/combo2.gif

sven
23 Aug 2010, 1:24 AM
Can you please post a testcase of the code you are using? Than somebody can probably help you with your problem.

Jenya
23 Aug 2010, 5:27 AM
Putting the complete code might be too heavy. I'll provide key extractions:

1)
Fee extends BaseModel

I set up values to the parent's map:

One of them is:

set("value", getFullName());

set("fullName", getFullName())

I also override toStirng() which does
return getFullName();

2) Combo:

SimpleComboBox<Fee> combo = new SimpleComboBox<Fee>();
combo.setFieldLabel("My field");
combo.add(list);

where

List<Fee> list is filled in with objects having properties set (
set("value", getFullName()), etc.)


When I try to do

combo.setDisplayField("fullName")

it not even displaying the [object Object] like it is shown but each line among combo values becomes empty and too thing.


Tha same object when used for the ComboBox works fine.

sven
23 Aug 2010, 5:32 AM
combo.setDisplayField("value")

fullName is not a valid dataindex on your modeldata. However the "value" you want do display is stored under the dataindex value.

Jenya
23 Aug 2010, 5:45 AM
Sorry, not very clear.

Does it mean that for the SimpleComboBox "value" is allways a mandatory dataindex?

What do you mean by "not a valid dataindex"? Are there any restrictions concerning naming dataindexes?

Is there any documentation about it just in case?

sven
23 Aug 2010, 5:50 AM
You can use any dataindex you want. But as you understood your description, your are storing your fullname under the dataIndex "value".

You should really take the time and recreate this in a small testcase that implements EntryPoint. This wont take a long time.

Jenya
23 Aug 2010, 6:25 AM
ok, I'll try to do it asap.

Jenya
23 Aug 2010, 7:03 AM
Here is the approach I use. But this test case is even worse. Combo doesn't even show any value...




package test.client;

import java.util.ArrayList;
import java.util.List;

import com.extjs.gxt.ui.client.data.BaseModel;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.SimpleComboBox;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.ui.RootPanel;

/**
* Entry point classes define <code>onModuleLoad()</code>.
*/
public class Test implements EntryPoint {


public static final String ID = "id";
public static final String NAME = "name";
public static final String VALUE = "value";
public static final String FULL_NAME = "fullName";

public void onModuleLoad() {

RootPanel rp = RootPanel.get("main");

FormPanel form = new FormPanel();
form.setFieldWidth(200);
form.setLabelWidth(150);
form.setPosition(100, 100);
form.setFrame(true);

SimpleComboBox<TestObject> combo = new SimpleComboBox<TestObject>();
combo.add(getlist());
combo.setFieldLabel("Simple Combo Box");
combo.setDisplayField(FULL_NAME);
combo.setEmptyText("Please select...");

form.add(combo);

form.layout();

rp.add(form);

}

private List<TestObject> getlist() {
List<TestObject> result = new ArrayList<TestObject>() {
{
add(new TestObject(1, "One"));
add(new TestObject(2, "Two"));
add(new TestObject(3, "Three"));
add(new TestObject(4, "Four"));
add(new TestObject(5, "Five"));
}
};

return result;
}

public static class TestObject extends BaseModel {

private String name;
private long id;

public void defineDisplayedValues() {
set(ID, getId());
set(NAME, getName());
set(FULL_NAME, getFullName());
set(VALUE, getFullName());
}

private TestObject(long id, String name) {
super();
this.name = name;
this.id = id;
defineDisplayedValues();
}



@Override
public String toString() {
return getFullName();
}

public String getFullName() {
return getName() + " (" + getId() + ")";
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public long getId() {
return id;
}

public void setId(long id) {
this.id = id;
}

}
}

sven
23 Aug 2010, 7:10 AM
For such a approach you dont use a SimpleComboBox. Use the parent class, ComboBox:


class Test implements EntryPoint {

public static final String ID = "id";
public static final String NAME = "name";
public static final String VALUE = "value";
public static final String FULL_NAME = "fullName";

public void onModuleLoad() {

RootPanel rp = RootPanel.get();

FormPanel form = new FormPanel();
form.setFieldWidth(200);
form.setLabelWidth(150);
form.setPosition(100, 100);
form.setFrame(true);

ComboBox<TestObject> combo = new ComboBox<TestObject>();
ListStore<TestObject> ls = new ListStore<TestObject>();
ls.add(getlist());
combo.setStore(ls);
combo.setFieldLabel("Simple Combo Box");
combo.setDisplayField(FULL_NAME);
combo.setEmptyText("Please select...");

form.add(combo);

form.layout();

rp.add(form);

}

private List<TestObject> getlist() {
List<TestObject> result = new ArrayList<TestObject>() {
{
add(new TestObject(1, "One"));
add(new TestObject(2, "Two"));
add(new TestObject(3, "Three"));
add(new TestObject(4, "Four"));
add(new TestObject(5, "Five"));
}
};

return result;
}

public static class TestObject extends BaseModel {

private String name;
private long id;

public void defineDisplayedValues() {
set(ID, getId());
set(NAME, getName());
set(FULL_NAME, getFullName());
set(VALUE, getFullName());
}

private TestObject(long id, String name) {
super();
this.name = name;
this.id = id;
defineDisplayedValues();
}

@Override
public String toString() {
return getFullName();
}

public String getFullName() {
return getName() + " (" + getId() + ")";
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public long getId() {
return id;
}

public void setId(long id) {
this.id = id;
}

}
}

Jenya
23 Aug 2010, 7:12 AM
Well, the reason why I decided to use SimpleComboBox (originally I tried to use ComboBox) is because:

1) Using it in the CellEditor gave problems (see my another thread, I'll put a test case there as well).

2) setting a value in one case was not working (I'll put a code here).

Jenya
23 Aug 2010, 7:49 AM
Ok, issue resolved. I had an accomulation of other problems which made me think that I had to use SimpleComboBox which all-in-all ended up as a noodle.

Thanks for obliging me a test case, it helped me to see one tiny thing that was blocking ComboBox proper usage.

sven
23 Aug 2010, 8:01 AM
Thanks for obliging me a test case, it helped me to see one tiny thing that was blocking ComboBox proper usage.

That is why it is in most cases the best approach to simple create a small testcase. Creating a testcase most times takes a few minutes, finding the problem in the real application can take hours.

Jenya
23 Aug 2010, 8:03 AM
True :)