Announcement

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

    New record added to ListGrid with Datasource not being displayed

    Be sure your post includes:

    I have a ListGrid which is bound to Datasource with JSON format. The data to be displayed in the ListGrid is fetched from a servlet which writes the JSON string to the HTTPResponse.

    We use the server push technology provided by SmartGWT.
    When a data is inserted in the back end, the new data is sent to the UI. We create a new ListGridRecord with the data and insert the record to the ListGrid.
    We have tried the following approaches, but is not working
    - ListGrid.addData(record)
    - ListGrid.getDataSource().add(record)
    - ListGrid.getResultSet().add(record)
    - ListGrid.setData(record[]) (this removes existing records and inserts new ones)

    I do not want to query server again and fetch the data. So the new record insertion has to be done on client side.

    Can anyone please suggest a solution for the problem i am facing..

    Thanks in advance

    #2
    DataSource.updateCaches().

    Comment


      #3
      Thanks for the reply.

      But I have followed the same thing, but the ListGrid is not getting refreshed. Please find the code below.
      We have purchased the SmartGWT POWER edition(3.1) and the Messaging license.
      Let me know if I have done anything wrong

      ----------------------------------------------------------------------------------------------
      Code:
      int i = 0;
      for(Map<String, String> alarm : alarmList) {
      	ListGridRecord record = new ListGridRecord();
      	for(String column : alarm.keySet()) {
      	record.setAttribute(column, alarm.get(column));
      }
      recordArr[i] = record;
      i++;
      }
      DSResponse dsResponse = new DSResponse();  
      dsResponse.setOperationType(DSOperationType.ADD);
      dsResponse.setData(recordArr);
      	  
      faultGrid.updateCaches(dsResponse);
      Following is the DataSource we are using:

      Code:
      public class JsonDatasource extends RestDataSource {
      	
      	private int startRow;
      	private int endRow;
      	private String operation;
      	private Criteria previousCriteria;
      	
      	public JsonDatasource() {
      		super();
      		setDataFormat(DSDataFormat.JSON);
      	}
      	
      	public void setDataSourceFields(Map<String, FieldType> gridFields) {
      		DataSourceField[] dsFields = new DataSourceField[gridFields.size()];
      		int cnt = 0;
      		for(String fieldName : gridFields.keySet()) {
      			DataSourceField dsField = new DataSourceField(fieldName, gridFields.get(fieldName), fieldName);  
      			dsFields[cnt] = dsField;
      			cnt++;
      		}
      		setFields(dsFields);
      	}	
      	
      	public void setRequestParameters(Map<String, String> criteriaMap) {
      		if(criteriaMap == null) {
      			return;
      		}
      		DSRequest request = new DSRequest();
      		request.setParams(criteriaMap);
      		setRequestProperties(request);
      	}
      	
      	public void clearRequestParameters() {
      		getRequestProperties().setParams(null);
      	}
      }
      Our ListGrid class is as follows:
      Code:
      public class JsonGrid {
      	protected ListGrid dataGrid;
      	protected JsonDatasource jsonDS;
      	private Map<String, Boolean> filterFields;
      	private HLayout rollOverCanvas;
      	private ListGridRecord rollOverRecord;
      	
      	public JsonGrid() {
      		dataGrid = new ListGrid();
      		dataGrid.setWidth100();
      		setDataSource();
      	}
      	
      	private void setDataSource() {
      		jsonDS = new JsonDatasource();
      		jsonDS.setDataFormat(DSDataFormat.JSON);
      	}
      	
      	public ListGrid getDataGrid() {
      		return dataGrid;
      	}
      	
      	public void setFilter(String fieldName, boolean canFilter) {
      		getField(fieldName).setCanFilter(canFilter);
      	}
      	
      	public void setGridFields(Map<String, GridFieldType> gridFields) {
      		ListGridField[] dsFields = new ListGridField[gridFields.size()];
      		int cnt = 0;
      		Iterator<String> itr = gridFields.keySet().iterator();
      		while(itr.hasNext()) {
      			String fieldName = itr.next();
      			ListGridField dsField = new ListGridField(fieldName); 
      			dsField.setType(ListGridFieldType.valueOf(gridFields.get(fieldName).toString()));
      			if(filterFields != null && !filterFields.isEmpty()) {
      				if(filterFields.containsKey(fieldName)) {
      					dsField.setCanFilter(filterFields.get(fieldName));
      				}
      			}
      			dsFields[cnt] = dsField;
      			cnt++;
      		}
      		dataGrid.setFields(dsFields);
      	}
      	
      	public void setDataSourceFields(Map<String, GridFieldType> gridFields) {
      		DataSourceField[] dsFields = new DataSourceField[gridFields.size()];
      		int cnt = 0;
      		for(String fieldName : gridFields.keySet()) {
      			DataSourceField dsField = new DataSourceField(fieldName, 
      					FieldType.valueOf(gridFields.get(fieldName).toString()), fieldName);  
      			if(filterFields != null && !filterFields.isEmpty()) {
      				if(filterFields.containsKey(fieldName)) {
      					dsField.setCanFilter(filterFields.get(fieldName));
      				}
      			}
      			dsFields[cnt] = dsField;
      			cnt++;
      		}
      		jsonDS.setFields(dsFields);
      		dataGrid.setDataSource(jsonDS);
      	}
      	
      	public void setDefaultParams(Map params) {
      		jsonDS.setDefaultParams(params);
      	}
      	
      	public String getDataSourceURL() {
      		return dataGrid.getDataSource().getDataURL();
      	}
      	
      	public void refresh() {
      		jsonDS.fetchData(null, null);
      	}
      	
      	public void fetchData(Map<String, String> filterMap) {
      		if(filterMap == null || filterMap.isEmpty()) {
      			jsonDS.fetchData(null, null);
      			return;
      		}
      		Criteria filterCriteria = new Criteria();
      		for(String key : filterMap.keySet()) {
      			filterCriteria.addCriteria(key, filterMap.get(key));
      		}
      		jsonDS.fetchData(filterCriteria, null);
      	}
      	
      	public void setDataURL(String dataURL) {
      		jsonDS.setDataURL(dataURL);
      	}
      	
      	public Canvas getJsonGrid() {
      		return dataGrid;
      	}
      	
      	public void fetchData() {
      		dataGrid.fetchData();
      	}
      	
      	public void fetchData(Criteria criteria) {
      		dataGrid.fetchData(criteria);
      	}
      	
      	public void setRequestParameters(Map<String, String> criteriaMap) {
      		if(criteriaMap == null) {
      			return;
      		}
      		DSRequest request = new DSRequest();
      		request.setParams(criteriaMap);
      		jsonDS.setRequestProperties(request);
      	}
      	
      	public void updateCaches(DSResponse dsResponse) {
      		dataGrid.getDataSource().updateCaches(dsResponse);
      	}
      }
      Can you please let me know if there is anything wrong with the above code?
      Last edited by Isomorphic; 13 May 2013, 09:01. Reason: added code tags

      Comment


        #4
        We can't tell from this code, but one possible problem is that, if you have not called fetchData() on the ListGrid before you start calling updateCaches(), then it does not yet have a ResultSet and will not automatically update cache.

        If you want to be able to start getting cache updates without calling fetchData(), you can directly create a ResultSet and pass it to listgrid.setData()

        Comment

        Working...
        X