PDA

View Full Version : code in onSuccess method not executed correctly



dark_nights
24 Aug 2009, 1:30 AM
Hello,
I'm doing a simple project showing some contacts from a database in a grid.
I have a method called setGrid(). This one constructs the grid.
In the method onRender(), I'm doing an RPC cal to get the list of contacts in the database.
The problem is that when I call the setGrid method in the onSuccess method, I get a blank page... But when I put the setGrid method before or after the RPC call, the page is shown correctly.
I need to call the setGrid method in order to use the AsyncCallback response.
Help me please, I waste my weekend on this issue.


@Override
protected void onRender(Element parent, int index) {

super.onRender(parent, index);
//setGrid() works here
page.getRpc().getAllContacts(new syncCallback<List<Person>>() {

public void onFailure(Throwable caught) {
System.out.println("pb de rpc");
}

public void onSuccess(List<Person> result) {
System.out.println("OnSuccess : " + result.size());
setGrid() doesn't work here
}
});
// setGrid() works here.
}Thank you

Arno.Nyhm
24 Aug 2009, 3:32 AM
what you do in setGrid()? did you look with firebug if there are some errors or if you debug it did you look in the second (red) gwt log window "Google Web Toolkit Hosted Mode"?

dark_nights
24 Aug 2009, 4:05 AM
Well I'll put the whole code :



package aa.client;

import java.util.ArrayList;
import java.util.List;
import com.extjs.gxt.ui.client.widget.HorizontalPanel;
import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.data.BasePagingLoader;
import com.extjs.gxt.ui.client.data.PagingLoader;
import com.extjs.gxt.ui.client.data.PagingModelMemoryProxy;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.GridEvent;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.layout.FlowLayout;
import com.extjs.gxt.ui.client.widget.toolbar.PagingToolBar;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;

@SuppressWarnings("unchecked")
public class ContactList extends LayoutContainer {

private static int nrows = 0;
private Page page;
private Grid<Person> grid;
private ContentPanel cp;
private PagingModelMemoryProxy proxy;
private PagingLoader loader;
private ListStore<Person> store;

public ContactList(Page p, int rows) {
super();
page = p;
nrows = rows;
cp = new ContentPanel();
}

public ContactList getNewContactList(int rows) {
nrows = rows;
return new ContactList(page, rows);
}


private void setGrid() {
FlowLayout flow = new FlowLayout(nrows);
setLayout(flow);
PagingToolBar toolBar = new PagingToolBar(nrows);//nb de lignes par page
HorizontalPanel hp = new HorizontalPanel();



final ListBox lb = new ListBox();
lb.addItem("10 ");
lb.addItem("20 ");
lb.addItem("30 ");
lb.addItem("40 ");
lb.addItem("50 ");
lb.setSelectedIndex(nrows/10 - 1);

lb.addChangeHandler(new ChangeHandler() {

public void onChange(ChangeEvent event) {

nrows = new Integer(lb.getValue(lb.getSelectedIndex()).trim());
page.applyChangingRowsNumber(nrows);//
}
});


hp.add(new Label(" Montrer "));
hp.add(lb);
hp.add(new Label(" éléments "));

toolBar.insert(hp, 11);

toolBar.bind(loader);

loader.load(0, nrows);

List<ColumnConfig> configs = new ArrayList<ColumnConfig>();

ColumnConfig column = new ColumnConfig();
column.setId("nom");
column.setHeader("Nom<br>");
column.setWidth(200);
configs.add(column);


column = new ColumnConfig();
column.setId("prenom");
column.setHeader("Prenom");
column.setWidth(100);
configs.add(column);

column = new ColumnConfig();
column.setId("compagnie");
column.setHeader("Compagnie");
column.setWidth(100);
configs.add(column);

column = new ColumnConfig();
column.setId("email");
column.setHeader("Email");
column.setWidth(100);
configs.add(column);

column = new ColumnConfig();
column.setId("tel1");
column.setHeader("Tel");
column.setWidth(100);
configs.add(column);

ColumnModel cm = new ColumnModel(configs);

cp.setFrame(true);
cp.setHeading("Contacts");

cp.setButtonAlign(HorizontalAlignment.CENTER);
cp.setLayout(new FitLayout());
cp.setBottomComponent(toolBar);
cp.setSize(700, 380);

grid = new Grid<Person>(store, cm);
grid.setBorders(true);

grid.addListener(Events.RowClick, new Listener<GridEvent<Person>>() {
public void handleEvent(GridEvent<Person> be) {
page.setButtonsEnabled(true);
}
});

grid.addListener(Events.RowDoubleClick, new Listener<GridEvent<Person>>() {

public void handleEvent(GridEvent<Person> be) {
String nom = grid.getView().getCell(be.getRowIndex(), 0).getInnerText();
String prenom = grid.getView().getCell(be.getRowIndex(), 1).getInnerText();
Window.alert("selected nom : " + nom + " & prenom : " + prenom);
}
});
cp.add(grid);
add(cp);
}

private void setProxy(List<Person> result) {
System.out.println("setProxy : " + result.size());
proxy = new PagingModelMemoryProxy(result);
loader = new BasePagingLoader(proxy);
loader.setRemoteSort(true);
store = new ListStore<Person>(loader);
}

@Override
protected void onRender(Element parent, int index) {

super.onRender(parent, index);

page.getRpc().getAllContacts(new AsyncCallback<List<Person>>() {

public void onFailure(Throwable caught) {
System.out.println("pb de rpc");
}

public void onSuccess(List<Person> result) {
System.out.println("OnSuccess : " + result.size());
setProxy(result);
setGrid();
}
});
}
}
setProxy is to initialize the proxy in order to get the list of contacts from the database.
setGrid is to create the grid.
Thanks a lot.

Arno.Nyhm
24 Aug 2009, 4:21 AM
just for understandig: why you will create the grid after the rpc call?

other examples work like this:

create the grid and attach the grid to the window.
assign all loaders and stores.
after this call the rpc

Arno.Nyhm
24 Aug 2009, 4:22 AM
did you look out for some errors in the 2 places?

dark_nights
24 Aug 2009, 4:29 AM
It doesn't matter if I create the grid before or after. Even if I create the grid before the RPC call, I get the same result.
ButI'm wondering why it doesn't work if I out the call to setProxy and setGrid in the onSuccess method, it dosen't work anymore :(

sudhaChandrasekar
7 Jan 2015, 3:14 AM
Hi,
I am facing issue in Setting the Value of Select Box in Grid .


Scenario: in the ListGrid i am having two Combo box .
For One combo box i am getting the value in the list during the onload and displaying .
On change value of first combo i need to fetch data from a list with input value from frst combo. i am getting the value from list but when i try to set in the second combo its not loading


Please find my code below.




//Properly Loading Combo
TaskName=new ListGridField("task_name","task_label);
SelectItem taskItem=new SelectItem("task_name","task_label);
taskNameList=populateTaskName();//Getting while onload
taskItem.setValueMap(taskNameList);
taskItem.addChangedHandler(...);
TaskName.setEditorType(taskItem);
TaskName.setValueMap(taskNameList);
TaskName.setRequired(true);
TaskName.setCanEdit(true);




//Not Loading Combo
SubTaskName=new ListGridField("Sub_task_name","Sub_task_label);
SelectItem subTaskItem=new SelectItem("Sub_task_name","Sub_task_label);


subTaskItem.addChangedHandler(...);
SubTaskName.setEditorType(taskItem);

TaskName.setRequired(true);
TaskName.setCanEdit(true);


//ChangeHandlerFor taskItem-FirstCombo
onChange(){
Form item=event.getItem();
item.setTooltip(item.getDisplayValue());
String taskId=String.valueOf(item.getValue());
populateSubTaskname(taskId);
subTaskItem.setValueMap(subtaskNameList);//getting the subtaskname List from RPC Call
SubTaskName.setValueMap(subtaskNameList);


populateSubTaskname(){

getting the value and loads in List
//subtaskNameList


subTaskItem.setValueMap(subtaskNameList);//getting the subtaskname List
SubTaskName.setValueMap(subtaskNameList);
}




//ChangeHandlerFor taskItem-Second Combo
onChange(){
Form item=event.getItem();
item.setTooltip(item.getDisplayValue());

}//Here also Name in the Map is not setting instead setting the value alone sometimes




Please help me where im wrong.