Zagura’s blog

June 16, 2009

How to convince the SmartGWT ListGrid to automatically fetch from the server only the data it displays

Filed under: English,Java,Programming,Web — puthre @ 7:29 pm

Since I was looking all over the net how to do this and I only found pieces of information, here is an example:

JAVA:
  1. public class OperationsGrid extends ListGrid{
  2.     OperationsGrid(){
  3.         super();
  4.         TreeGridField accNoField = new TreeGridField("accountNo", 150);
  5.         TreeGridField commentField = new TreeGridField("comment"150);
  6.  
  7.         setFields(accNoField,commentField);
  8.         setHeight100();
  9.         setWidth100();
  10.         setAutoFetchData(true);
  11.         setAlternateRecordStyles(true);
  12.         setDataSource(OperationsDS.getInstance());
  13.         setShowFilterEditor(true);
  14.     }
  15. }

JAVA:
  1. public class OperationsDS extends DataSource{
  2.     static OperationsDS instance = null;
  3.      static OperationsDS getInstance(){
  4.         if(instance == null){
  5.             instance = new OperationsDS();
  6.         }
  7.          return instance;
  8.     }
  9.  
  10.     OperationsDS(){
  11.  
  12.         setDataURL(xpath);
  13.         DataSourceTextField noField = new DataSourceTextField("accountNo", "Account Number", 128);
  14.         DataSourceTextField commentField = new DataSourceTextField("comment", "Comment", 128);
  15.  
  16.         noField.setRequired(true);
  17.         noField.setPrimaryKey(true);
  18.  
  19.         setFields(noField,commentField);
  20.         setDataFormat(DSDataFormat.JSON);
  21.         setRecordXPath("response/result");
  22.         setClientOnly(false);
  23.     }
  24.      @Override
  25.      protected Object transformRequest(DSRequest dsRequest){
  26.          String url = null;
  27.  
  28.          if(dsRequest.getOperationType().equals(DSOperationType.FETCH)){
  29.              dsRequest.setActionURL(getDataURL()+"&start="+dsRequest.getStartRow()+"&end="+dsRequest.getEndRow());
  30.          }
  31.          return super.transformRequest(dsRequest);
  32.      }
  33.      @Override
  34.     protected void transformResponse(DSResponse response, DSRequest request,
  35.             Object data) {
  36.         DSOperationType operationType = request.getOperationType();
  37.  
  38.         if (operationType == DSOperationType.FETCH) {
  39.             JSONArray info = XMLTools.selectObjects(data, "/response/resultInfo/numTotalRows");
  40.             Integer total = (int)Math.round(info.get(0).isNumber().doubleValue());
  41.  
  42.             info = XMLTools.selectObjects(data, "/response/resultInfo/startRow");
  43.             Integer start = (int)Math.round(info.get(0).isNumber().doubleValue());
  44.  
  45.             info = XMLTools.selectObjects(data, "/response/resultInfo/endRow");
  46.             Integer end = (int)Math.round(info.get(0).isNumber().doubleValue());
  47.  
  48.             if(total != null)
  49.                 response.setTotalRows(total);
  50.             if(start != null)
  51.                 response.setStartRow(start);
  52.             if(end != null)
  53.                 response.setEndRow(end);
  54.         } else {
  55.             super.transformResponse(response, request, data);
  56.         }
  57.  
  58.     }
  59. }

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.

November 23, 2008

GWTpedia

Filed under: English,Linux,Programming,Web — puthre @ 7:47 pm

gwtpedialogo.png

As I started to look at GWT and its related widgets, tutorials and utilities, I thought that it might be an idea to put the information together into a Wiki site. This way, GWTpedia.com was born. Everyone is free to edit.

Powered by WordPress