smartgwt - 由单元格编辑列表网格有一些问题

时间:2011-07-22 13:20:42

标签: smartgwt

我创建了listgid,可以通过单元格进行编辑。 为了测试,我添加了保存按钮。当我点击“保存”按钮时,弹出窗口中会出现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 个答案:

答案 0 :(得分:1)

对listgrid使用addRowEditorExitHandler。这不需要保存按钮。 进行更改并单击网格外的任何位置后,控件将自动进入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
      }
    });