2011-01-19 20 views
7

Tengo un problema con el enlace del origen de datos en ListGrid con smartGWT. Tengo GWT-RPC-DataSource y he puesto como mi fuente de datosSMARTGWT DataSource (GWT-RPC-DATASource) LISTGRID

grid.setDataSource(ds); 

Por un clic de botón Tengo algunos cambios en mi fuente de datos y estoy generando nueva fuente de datos y revinculación con la red de SmartGWT. pero falla He intentado grid.redraw() función para volver a dibujar la red.

A continuación es mi clase de GWTRPCDATASOURCE

public abstract class GwtRpcDataSource extends DataSource { 

    /** 
    * Creates new data source which communicates with server by GWT RPC. It is 
    * normal server side SmartClient data source with data protocol set to 
    * <code>DSProtocol.CLIENTCUSTOM</code> ("clientCustom" - natively supported 
    * by SmartClient but should be added to smartGWT) and with data format 
    * <code>DSDataFormat.CUSTOM</code>. 
    */ 
    public GwtRpcDataSource() { 
     setDataProtocol(DSProtocol.CLIENTCUSTOM); 
     setDataFormat(DSDataFormat.CUSTOM); 
     setClientOnly(false); 
    } 

    /** 
    * Executes request to server. 
    * 
    * @param request 
    *   <code>DSRequest</code> being processed. 
    * @return <code>Object</code> data from original request. 
    */ 
    @Override 
    protected Object transformRequest(DSRequest request) { 
     String requestId = request.getRequestId(); 
     DSResponse response = new DSResponse(); 
     response.setAttribute("clientContext", 
       request.getAttributeAsObject("clientContext")); 
     // Asume success 
     response.setStatus(0); 
     switch (request.getOperationType()) { 
     case FETCH: 
      executeFetch(requestId, request, response); 
      break; 
     case ADD: 
      executeAdd(requestId, request, response); 
      break; 
     case UPDATE: 
      executeUpdate(requestId, request, response); 
      break; 
     case REMOVE: 
      executeRemove(requestId, request, response); 
      break; 
     default: 
      // Operation not implemented. 
      break; 
     } 
     return request.getData(); 
    } 

    /** 
    * Executed on <code>FETCH</code> operation. 
    * <code>processResponse (requestId, response)</code> should be called when 
    * operation completes (either successful or failure). 
    * 
    * @param requestId 
    *   <code>String</code> extracted from 
    *   <code>DSRequest.getRequestId()</code>. 
    * @param request 
    *   <code>DSRequest</code> being processed. 
    * @param response 
    *   <code>DSResponse</code>. <code>setData (list)</code> should be 
    *   called on successful execution of this method. 
    *   <code>setStatus (&lt;0)</code> should be called on failure. 
    */ 
    protected abstract void executeFetch(String requestId, DSRequest request, 
      DSResponse response); 

    /** 
    * Executed on <code>ADD</code> operation. 
    * <code>processResponse (requestId, response)</code> should be called when 
    * operation completes (either successful or failure). 
    * 
    * @param requestId 
    *   <code>String</code> extracted from 
    *   <code>DSRequest.getRequestId()</code>. 
    * @param request 
    *   <code>DSRequest</code> being processed. 
    *   <code>request.getData()</code> contains record should be 
    *   added. 
    * @param response 
    *   <code>DSResponse</code>. <code>setData (list)</code> should be 
    *   called on successful execution of this method. Array should 
    *   contain single element representing added row. 
    *   <code>setStatus (&lt;0)</code> should be called on failure. 
    */ 
    protected abstract void executeAdd(String requestId, DSRequest request, 
      DSResponse response); 

    /** 
    * Executed on <code>UPDATE</code> operation. 
    * <code>processResponse (requestId, response)</code> should be called when 
    * operation completes (either successful or failure). 
    * 
    * @param requestId 
    *   <code>String</code> extracted from 
    *   <code>DSRequest.getRequestId()</code>. 
    * @param request 
    *   <code>DSRequest</code> being processed. 
    *   <code>request.getData()</code> contains record should be 
    *   updated. 
    * @param response 
    *   <code>DSResponse</code>. <code>setData (list)</code> should be 
    *   called on successful execution of this method. Array should 
    *   contain single element representing updated row. 
    *   <code>setStatus (&lt;0)</code> should be called on failure. 
    */ 
    protected abstract void executeUpdate(String requestId, DSRequest request, 
      DSResponse response); 

    /** 
    * Executed on <code>REMOVE</code> operation. 
    * <code>processResponse (requestId, response)</code> should be called when 
    * operation completes (either successful or failure). 
    * 
    * @param requestId 
    *   <code>String</code> extracted from 
    *   <code>DSRequest.getRequestId()</code>. 
    * @param request 
    *   <code>DSRequest</code> being processed. 
    *   <code>request.getData()</code> contains record should be 
    *   removed. 
    * @param response 
    *   <code>DSResponse</code>. <code>setData (list)</code> should be 
    *   called on successful execution of this method. Array should 
    *   contain single element representing removed row. 
    *   <code>setStatus (&lt;0)</code> should be called on failure. 
    */ 
    protected abstract void executeRemove(String requestId, DSRequest request, 
      DSResponse response); 

    private ListGridRecord getEditedRecord(DSRequest request) { 
     // Retrieving values before edit 
     JavaScriptObject oldValues = request 
       .getAttributeAsJavaScriptObject("oldValues"); 
     // Creating new record for combining old values with changes 
     ListGridRecord newRecord = new ListGridRecord(); 
     // Copying properties from old record 
     JSOHelper.apply(oldValues, newRecord.getJsObj()); 
     // Retrieving changed values 
     JavaScriptObject data = request.getData(); 
     // Apply changes 
     JSOHelper.apply(data, newRecord.getJsObj()); 
     return newRecord; 
    } 

} 

he implementado esta clase abstracta a mi propia clase de fuente de datos llamado NTDatasource.

public class NTDataSource extends GwtRpcDataSource { 

    public static int total = 991; 
    Record[] records; 
    public NTDataSource() {    
    } 

    public void setData(List<NTListGridField> lstFields, Record[] records) { 
//  setTestData(records); 
     for (NTListGridField lstField : lstFields) { 
      if (lstField.getType() == ListGridFieldType.DATE) { 
       DataSourceDateField dateField = new DataSourceDateField(
         lstField.getName()); 
       dateField.setHidden(lstField.getAttributeAsBoolean("visible")); 
       if (lstField.getName().equals("id")) { 
        dateField.setHidden(true); 
       } 

       addField(dateField); 

      } else { 
       DataSourceTextField textField = new DataSourceTextField(
         lstField.getName()); 
       textField.setHidden(lstField.getAttributeAsBoolean("visible")); 
       if (lstField.getName().equals("id")) { 
        textField.setHidden(true); 
        textField.setPrimaryKey(true); 
       } 
       addField(textField); 
      } 
     } 
     total = records.length; 
     this.records = records; 
    } 

    @Override 
    protected void executeFetch(String requestId, DSRequest request, 
      DSResponse response) { 
     // assume we have 1000 items. 
     response.setTotalRows(total); 
     int end = request.getEndRow(); 
     if (end > total) { 
      end = total; 
     }  
     Record returnRecords[] = new Record[end 
       - request.getStartRow()]; 
     for (int i = request.getStartRow(); i < end; i++) { 
      ListGridRecord r = new ListGridRecord();  
      r = (ListGridRecord) records[i]; 
      returnRecords[i - request.getStartRow()] = r; 
     } 
     GWT.log(" called from " + request.getStartRow() + " to " 
       + request.getEndRow() + " result " + returnRecords.length, null); 
     response.setData(returnRecords); 
     processResponse(requestId, response); 
    } 

    @Override 
    protected void executeAdd(String requestId, DSRequest request, 
      DSResponse response) { 
     // TODO Auto-generated method stub 

    } 

    @Override 
    protected void executeUpdate(String requestId, DSRequest request, 
      DSResponse response) { 
     // TODO Auto-generated method stub 

    } 

    @Override 
    protected void executeRemove(String requestId, DSRequest request, 
      DSResponse response) { 
     // TODO Auto-generated method stub 

    } 
} 
+0

esto se ve muy bien, ¿construiste el gwtRpcDataSource de algún tutorial o algo así? – javaNoober

Respuesta

7

Tengo que resolver esta pregunta yo mismo.

¡La respuesta es que necesito usar el método grid.fetchData() y vincular el origen de datos una vez más para usarlo ... !! Espero que pueda ayudar a alguien más.

0

Trate grid.invalidateCache() .Esta llamada va a borrar los datos actuales en la red y ejecuta el método NTDataSource.executeFetch.

Cuestiones relacionadas