PDA

View Full Version : [CLOSED] ComboBox getValue return null - rc1



fother
26 Jun 2009, 4:00 AM
click in the button "get combo value" you can see one alert showing that the value is null, but the value was set before the combo render..

Data.java


package example.client;

import com.extjs.gxt.ui.client.data.BaseModel;

public class Data extends BaseModel {

public Data() {

}

public Data(String name) {
setName(name);
}

public String getName() {
return get("name");
}

public void setName(String name) {
set("name", name);
}
}


GreetingService.java


package example.client;

import com.extjs.gxt.ui.client.data.BasePagingLoadResult;
import com.google.gwt.user.client.rpc.RemoteService;
import com.google.gwt.user.client.rpc.RemoteServiceRelativePath;

@RemoteServiceRelativePath("greet")
public interface GreetingService extends RemoteService {
BasePagingLoadResult<Data> getData();
}



GreetingServiceAsync.java


package example.client;

import com.extjs.gxt.ui.client.data.BasePagingLoadResult;
import com.extjs.gxt.ui.client.data.ModelData;
import com.google.gwt.user.client.rpc.AsyncCallback;

public interface GreetingServiceAsync {
void getData(AsyncCallback<BasePagingLoadResult<ModelData>> callback);
}



Gxt.java


package example.client;

import java.util.List;
import com.extjs.gxt.ui.client.data.BaseListLoader;
import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.data.RpcProxy;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.ComboBox;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.RootPanel;

public class Gxt implements EntryPoint {

private final GreetingServiceAsync greetingService = GWT.create(GreetingService.class);

public void onModuleLoad() {

RpcProxy<List<ModelData>> proxy = new RpcProxy<List<ModelData>>() {

@Override
protected void load(Object loadConfig, AsyncCallback callback) {

greetingService.getData(callback);
}
};

BaseListLoader loader = new BaseListLoader(proxy);
ListStore<ModelData> store = new ListStore<ModelData>(loader);

final ComboBox<ModelData> combo = new ComboBox<ModelData>();
combo.setDisplayField("name");
combo.setStore(store);
combo.setFieldLabel("Example");
combo.setValue(new Data("data value"));

FormPanel panel = new FormPanel();
panel.add(combo);

Button button = new Button("get combo value", new SelectionListener<ButtonEvent>() {

@Override
public void componentSelected(ButtonEvent ce) {

ModelData value = combo.getValue();

String msg = "";

if (value != null) {

msg = value.get("name");
} else {
msg = "null";
}

Window.alert(msg);
}
});

RootPanel.get().add(panel);
RootPanel.get().add(button);
}
}



GreetingServiceImpl.java


package example.server;

import java.util.ArrayList;
import java.util.List;
import com.extjs.gxt.ui.client.data.BasePagingLoadResult;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import example.client.Data;
import example.client.GreetingService;

@SuppressWarnings("serial")
public class GreetingServiceImpl extends RemoteServiceServlet implements GreetingService {

public BasePagingLoadResult<Data> getData() {

List<Data> list = new ArrayList<Data>();
list.add(new Data("name 1"));
list.add(new Data("name 2"));
list.add(new Data("name 3"));
list.add(new Data("name 4"));
list.add(new Data("name 5"));

return new BasePagingLoadResult<Data>(list);
}
}

sven
26 Jun 2009, 4:48 AM
This is not really an issue in the combobox. The item is not in the store, so you cant get it. You can use getRawValue which gets you the rawvalue. I am closing this isuse.

fother
26 Jun 2009, 5:37 AM
ok.. I agree if you,

but its really necessary access all data in store to get the combobox value? I think that ISNT a good ideia.. for performace..

If I have one form.. that I need edit the data.. I set all values for my inputs (incluind textfields and combobox).. and if the user will realy need change the combobox value.. he will click in trigger and choose other item.. :)

you told that The item is not in the store but if I change my code for this..



combo.setValue(new Data("name 1"));


now the item is in the store.. but continue return null..

other way.. raw value its one String and not the really value of my combo that its one ModelData

sven
26 Jun 2009, 7:18 AM
No it is not in the store as your load your data later. Use a memoryproxy for example.

fother
26 Jun 2009, 10:48 AM
ok.. I can use other ways to do this... overwrite the method.. or create my combo box but the problem is that the concept its wrong.. in all frameworks.. if you have one method get and set.. what you set should be what you get.. :)

why need iterate in store to get the value if you have one get and set? when the user choose the item in list.. call method set.. I dont see none problem..