Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
Clear All
new posts

    JSON data ListGrid live updates

    Hello,

    I want to display a grid that is updated say each 5s on on user action with data that comes from a servlet. The servlet answers a JSON array that I get thru a dataSource. The JSON array contains messages that are added slowly.

    I want my grid to be updated according to the content of the json. I have filters in my page to drive the access to the servlet.

    example: I want all the messages of this kind of today. The filtering is performed in the serverside.
    The url to acces is like "my-servlet?fromDate=20101021&kind=WARN" according to the filter.

    What is the best practice to do that? below, I have put the piece of code I tried (method query is called). My grid does not change. The callback returns a com.google.gwt.core.client.JavaScriptObject, I don't know what to do with it.

    Thank's for helping me start with smartGWT

    Code:
    package com.airbus.mcctls.client;
    
    import java.util.HashMap;
    import java.util.Map;
    
    import org.apache.tools.ant.types.CommandlineJava.SysProperties;
    
    import com.smartgwt.client.data.DSCallback;
    import com.smartgwt.client.data.DSRequest;
    import com.smartgwt.client.data.DSResponse;
    import com.smartgwt.client.data.DataSource;
    import com.smartgwt.client.data.DataSourceField;
    import com.smartgwt.client.data.fields.DataSourceDateTimeField;
    import com.smartgwt.client.data.fields.DataSourceTextField;
    import com.smartgwt.client.types.Alignment;
    import com.smartgwt.client.types.DSDataFormat;
    import com.smartgwt.client.types.ListGridFieldType;
    import com.smartgwt.client.widgets.grid.ListGrid;
    import com.smartgwt.client.widgets.grid.ListGridField;
    
    public class MessageGrid extends ListGrid{
    	
    
    	private DataSource dataSource = null;
    
    	public MessageGrid() {
    		dataSource  = new DataSource();
    		dataSource.setDataFormat(DSDataFormat.JSON);
    		dataSource.setDataURL("my-servlet?fromDate=20101021");
    
    		DataSourceTextField msnSrc = new DataSourceTextField(
    				"EXT_AIRCRAFT_MSN", "MSN");
    		DataSourceTextField airlineSrc = new DataSourceTextField(
    				"EXT_AIRLINE_IDENTIFIER", "Airline");
    		DataSourceTextField acTypeSrc = new DataSourceTextField(
    				"EXT_AIRCRAFT_FAMILY", "AcType");
    		DataSourceTextField msgTypeSrc = new DataSourceTextField(
    				"TYPE_IDENTIFIER", "Type");
    		DataSourceTextField tailNumberSrc = new DataSourceTextField(
    				"MSG_AIRCRAFT_TAILNUMBER", "Id");
    		DataSourceDateTimeField timeSrc = new DataSourceDateTimeField(
    				"MSG_UTC_TRANSMISSION_DATE_TIME", "time");
    		
    		dataSource .setFields(timeSrc, airlineSrc, tailNumberSrc, msnSrc, 
    				acTypeSrc, msgTypeSrc);
    
    		
    		//airline column
    		ListGridField airlineField = defaultField(airlineSrc);
    		airlineField.setAlign(Alignment.CENTER);
    		airlineField.setWidth(40);
    		airlineField.setType(ListGridFieldType.IMAGE);
    		airlineField.setImageURLPrefix("airline/icon/");
    		airlineField.setImageURLSuffix(".gif");
    		
    		ListGridField timeField = defaultField (timeSrc);
    		timeField.setWidth(125);
    		timeField.setAlign(Alignment.CENTER);
    		
    		ListGridField acTypeField = defaultField(acTypeSrc);
    		acTypeField.setWidth(50);
    		ListGridField msnField = defaultField(msnSrc);
    		msnField.setWidth(50);
    		ListGridField tailNumberField = defaultField(tailNumberSrc);
    		tailNumberField.setWidth(50);
    		
    
    		ListGridField msgTypeField = defaultField(msgTypeSrc);
    		msgTypeField.setWidth(50);
    		setFields(timeField, airlineField, tailNumberField, msnField, acTypeField, msgTypeField);
    		
    		setDataSource(dataSource );
    		setAutoFetchData(true);
    
    	}
    	
    	private ListGridField defaultField(DataSourceField src) {
    		return new ListGridField(src.getName(), src.getTitle());
    	}
    
    	public void query() {
    		DSCallback callback = new DSCallback() {
    			
    			@Override
    			public void execute(DSResponse response, Object rawData, DSRequest request) {
    				System.out.println(rawData);
    				//what have I to do?
    			}
    		};
    		DSRequest request = new DSRequest();
    		Map<String, String> params = new HashMap<String, String>();
    		params.put("kind",//getTheKind);
    		params.put("fromDate",//getTheFromDate);
    		request.setParams(params);
    		dataSource.fetchData(null, callback, request);
    	}
    
    
    
    }
    version:
    SmartGWT 2.2 / Firefox 3.6
Working...
X