PDA

View Full Version : Populate a Grid using RequestFactory.



Oscar Porto
28 Mar 2011, 12:45 PM
Hi,

I want to populate a simple grid(like the Basic grid found in examples) with record from a database, using the RequestFactory, but with less effort.

Here is my code:



public class GridExample extends LayoutContainer {

private static final Logger log = Logger
.getLogger(Scaffold.class.getName());

private ColumnModel cm;
private final ApplicationRequestFactory requestFactory;
private List<ProfessorProxy> dados;

public GridExample(ApplicationRequestFactory requestFactory) {
this.requestFactory = requestFactory;
// TODO Auto-generated constructor stub
}

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

setLayout(new FlowLayout(10));
getAriaSupport().setPresentation(true);
List<ColumnConfig> configs = new ArrayList<ColumnConfig>();

ColumnConfig column = new ColumnConfig();
column.setId("name");
column.setHeader("Company");
column.setWidth(200);
column.setRowHeader(true);
configs.add(column);

column = new ColumnConfig();
column.setId("symbol");
column.setHeader("Symbol");
column.setWidth(100);
configs.add(column);

column = new ColumnConfig();
column.setId("last");
column.setHeader("Last");
column.setAlignment(HorizontalAlignment.RIGHT);
column.setWidth(75);
configs.add(column);

column = new ColumnConfig("change", "Change", 100);
column.setAlignment(HorizontalAlignment.RIGHT);
configs.add(column);

column = new ColumnConfig("date", "Last Updated", 100);
column.setAlignment(HorizontalAlignment.RIGHT);
column.setDateTimeFormat(DateTimeFormat.getFormat("MM/dd/yyyy"));
configs.add(column);

cm = new ColumnModel(configs);

ContentPanel cp = new ContentPanel();
cp.setBodyBorder(true);
cp.setHeading("Basic Grid");
cp.setButtonAlign(HorizontalAlignment.CENTER);
cp.setLayout(new FitLayout());
cp.getHeader().setIconAltText("Grid Icon");
cp.setSize(600, 300);
requestFactory.professorRequest();
MemoryProxy<ModelData> proxy = new MemoryProxy<ModelData>(null);
ModelReader reader = new ModelReader();
ListLoader<ModelData> loader = new BaseListLoader(proxy, reader);
final ListStore<ModelData> store = new ListStore<ModelData>(loader);
final Grid<ModelData> grid = new Grid<ModelData>(store, cm);
grid.setStyleAttribute("borderTop", "none");
grid.setAutoExpandColumn("name");
grid.setBorders(false);
grid.setStripeRows(true);
grid.setColumnLines(true);
grid.setColumnReordering(true);
grid.getAriaSupport().setLabelledBy(cp.getHeader().getId() + "-label");
cp.add(grid);

grid.setStateful(true);
grid.setStateId("gridtest");

ToolBar toolBar = new ToolBar();
toolBar.getAriaSupport().setLabel("Grid Options");

toolBar.add(new LabelToolItem("Selection Mode: "));
final SimpleComboBox<String> type = new SimpleComboBox<String>();
type.getAriaSupport().setLabelledBy(toolBar.getItem(0).getId());
type.setTriggerAction(TriggerAction.ALL);
type.setEditable(false);
type.setFireChangeEventOnSetValue(true);
type.setWidth(100);
type.add("Row");
type.add("Cell");
type.setSimpleValue("Row");
toolBar.add(type);
cp.setTopComponent(toolBar);
requestFactory.professorRequest().findAllProfessors().fire(new Receiver<List<ProfessorProxy>>() {
@Override
public void onSuccess(List<ProfessorProxy> response) {
store.add(response);
}
});
add(cp);

// needed to enable quicktips (qtitle for the heading and qtip for the
// content) that are setup in the change GridCellRenderer
new QuickTip(grid);
}

}