2011-07-22 52 views
0

我做了可以用單元格編輯的listgid。 爲了測試,我添加了保存按鈕。當我點擊保存按鈕時,listgrid的第一條記錄(第一行上更新的第一列值)應該出現在彈出窗口中,但是它沒有顯示彈出時的更新值。smartgwt - listgrid按單元格編輯有一些問題

例如在這種情況下,有第一個listgrid記錄名稱 - > jon,我編輯jon shobhit,然後單擊保存按鈕。點擊保存按鈕後,我應該得到姓shobhit,但它顯示jon這是舊值。

請看下面我的代碼,並幫助我完成這個有趣的任務。

public void onModuleLoad() { 

    VLayout vLayout = new VLayout(10); 

    final ListGrid listGrid = new ListGrid(); 

    ListGridField nameField = new ListGridField("name","Name"); 
    nameField.setWidth(100); 
    nameField.setAlign(Alignment.CENTER); 

    ListGridField ageField = new ListGridField("age","Age"); 
    ageField.setWidth(100); 
    ageField.setAlign(Alignment.CENTER); 


    ListGridField locationField = new ListGridField("location","Location"); 
    locationField.setWidth(100); 
    locationField.setAlign(Alignment.CENTER); 

    listGrid.setFields(nameField, ageField, locationField); 
    listGrid.setDataSource(getDS()); 
    listGrid.setWidth(310); 
    listGrid.setHeight(224); 
    listGrid.setAutoFetchData(true); 
    listGrid.setCanEdit(true); 
    listGrid.setEditEvent(ListGridEditEvent.CLICK); 
    listGrid.setEditByCell(true); 

    vLayout.addMember(listGrid); 

    IButton saveButton = new IButton("Save"); 
    saveButton.addClickHandler(new ClickHandler() { 

     @Override 
     public void onClick(ClickEvent event) { 
      ListGridRecord[] record = listGrid.getRecords(); 
      Record r = record[0]; 
      SC.say(r.getAttributeAsString("name")); 
     } 
    }); 

    vLayout.addMember(saveButton); 

    RootPanel.get("gwtContent").add(vLayout); 
} 

private RestDataSource getDS() { 

    RestDataSource ds = new RestDataSource(); 

    DataSourceTextField nameField=new DataSourceTextField("name", "Name"); 
    DataSourceIntegerField ageField=new DataSourceIntegerField("age", "Age"); 
    DataSourceTextField locationField=new DataSourceTextField("location", "Location"); 

    ds.setFields(nameField, ageField, locationField); 

    ds.setDataFormat(DSDataFormat.JSON); 

    OperationBinding fetchOB = new OperationBinding(); 
    fetchOB.setOperationType(DSOperationType.FETCH); 

    OperationBinding addOB = new OperationBinding(); 
    addOB.setOperationType(DSOperationType.ADD); 
    addOB.setDataProtocol(DSProtocol.POSTPARAMS); 

    OperationBinding updateOB = new OperationBinding(); 
    updateOB.setOperationType(DSOperationType.UPDATE); 
    updateOB.setDataProtocol(DSProtocol.POSTPARAMS); 

    OperationBinding removeOB = new OperationBinding(); 
    removeOB.setOperationType(DSOperationType.REMOVE); 
    removeOB.setDataProtocol(DSProtocol.POSTPARAMS); 

    ds.setOperationBindings(fetchOB, addOB, updateOB, removeOB); 

    if (!GWT.isScript()){ 
     ds.setFetchDataURL("data/dataIntegration/json/data-fetch.js"); 
     ds.setJsonRecordXPath("response/data"); 
    }else{ 
    } 
    return ds; 
}  

JSON數據文件:

{ 
response: { 
    status: 0, 
    startRow: 0, 
    endRow: 4, 
    totalRows: 5, 
    data: [ 
      {"name":"Jon", "age":40, "location":"USA"}, 
      {"name":"Tom", "age":30, "location":"USA"}, 
      {"name":"Frank", "age":35, "location":"USA"}, 
      {"name":"Deb", "age":24, "location":"USA"}, 
      {"name":"Leroy", "age":70, "location":"USA"} 
    ] 
} 
} 

回答

1

使用addRowEditorExitHandler爲listgrid.This不需要保存按鈕。 一旦你做了修改並點擊任何位於格外的地方,控件就會自動進入addRowEditorExitHandler。

ListGrid listGrid = new ListGrid(); 
listGrid.setCanEdit(true); 
listGrid.setAutoSaveEdits(false); 
listGrid.setDataSource(getDS()); 
listGrid.addRowEditorExitHandler(new RowEditorExitHandler() { 

     @Override 
     public void onRowEditorExit(final RowEditorExitEvent event) { 
     SC.say(event.getNewValues().get("name")); 
     //event.getNewValues gives a map of unsaved edits in edited row 
     //This values u can put to a new record and save it 
     } 
    });