Since I was looking all over the net how to do this and I only found pieces of information, here is an example:
[java]
public class OperationsGrid extends ListGrid{
OperationsGrid(){
super();
TreeGridField accNoField = new TreeGridField(“accountNo”, 150);
TreeGridField commentField = new TreeGridField(“comment”, 150);
setFields(accNoField,commentField);
setHeight100();
setWidth100();
setAutoFetchData(true);
setAlternateRecordStyles(true);
setDataSource(OperationsDS.getInstance());
setShowFilterEditor(true);
}
}
[/java]
[java]
public class OperationsDS extends DataSource{
static OperationsDS instance = null;
static OperationsDS getInstance(){
if(instance == null){
instance = new OperationsDS();
}
return instance;
}
OperationsDS(){
setDataURL(xpath);
DataSourceTextField noField = new DataSourceTextField(“accountNo”, “Account Number”, 128);
DataSourceTextField commentField = new DataSourceTextField(“comment”, “Comment”, 128);
noField.setRequired(true);
noField.setPrimaryKey(true);
setFields(noField,commentField);
setDataFormat(DSDataFormat.JSON);
setRecordXPath(“response/result”);
setClientOnly(false);
}
@Override
protected Object transformRequest(DSRequest dsRequest){
String url = null;
if(dsRequest.getOperationType().equals(DSOperationType.FETCH)){
dsRequest.setActionURL(getDataURL()+”&start=”+dsRequest.getStartRow()+”&end=”+dsRequest.getEndRow());
}
return super.transformRequest(dsRequest);
}
@Override
protected void transformResponse(DSResponse response, DSRequest request,
Object data) {
DSOperationType operationType = request.getOperationType();
if (operationType == DSOperationType.FETCH) {
JSONArray info = XMLTools.selectObjects(data, “/response/resultInfo/numTotalRows”);
Integer total = (int)Math.round(info.get(0).isNumber().doubleValue());
info = XMLTools.selectObjects(data, “/response/resultInfo/startRow”);
Integer start = (int)Math.round(info.get(0).isNumber().doubleValue());
info = XMLTools.selectObjects(data, “/response/resultInfo/endRow”);
Integer end = (int)Math.round(info.get(0).isNumber().doubleValue());
if(total != null)
response.setTotalRows(total);
if(start != null)
response.setStartRow(start);
if(end != null)
response.setEndRow(end);
} else {
super.transformResponse(response, request, data);
}
}
}
[/java]
In this example the server outputs in JSON format and returns the the current start row, current end row and total number of rows in response/resultInfo.
Basically you need to override transformRequest and transformResponse. In transformRequest you should add the start and end row to the requestURL and in transformResponse you should set the startRow, endRow, and totalRows, provided that you receive this information from your server. Also don’t forget to setAutoFetchData(true) on the grid.
Hope this helps somebody.
Hi Zagura,
Actually none of that transformRequest/Response implementation is necessary, just use RestDataSource:
http://www.smartclient.com/smartgwt/javadoc/com/smartgwt/client/data/RestDataSource.html
It gives you a JSON format that your server should return that supports not only data paging, but also sort direction, filter criteria, the other 3 operation types (updates, removes and adds) along with validation error handling, unrecoverable error handling, all without writing any client-side code.
The commercial versions go a step further where there’s no need write any code at all to get immediate an immediate full-CRUD connection to a SQL Database or Hibernate bean.
http://www.smartclient.com/smartgwtee/showcase/
Good stuff, thank you! That’s what I was looking for.
I think if someone (maybe me?) would provide a good sample for full data integration, not just fetch, and maybe to use it with Spring that would cover most important data integration business cases.