smartgwt ListGridRecord以编程方式编辑问题

时间:2012-01-02 07:54:41

标签: gwt smartgwt

我正在使用smartgwt,我有一个ListGrid,其中我在ListGridRecord中有一些填充值。现在,如果我通过setAttribute(String fieldName,String value)以编程方式设置任何listGridRecord Field值并通过ListGridRecord.refreshFields()刷新字段,则值将反映到UI。但问题是如果我通过双击编辑相同的ListGridRecord。然后该值丢失或从UI中删除。

class FieldRecord extends ListGridRecord{

private int id;
private String name;

public void setID(Long id) {
  setAttribute(Constant.ID, id);
 }

public void setName(String name) {
  setAttribute(Constant.NAME, name);
 }

public Long getID() {
  return getAttribute(Constant.ID);
 }

public String getName() {
   return getAttribute(Constant.NAME); 
 }
}


class testData {
  FieldDataSource fieldDS = new FieldDataSource();

  FieldRecord fieldRec = new FieldRecord();
  //set some default value of record.

  fieldDS.addData(fieldRec);
  FieldGrid fieldGrid = new FieldGrid();
  fieldGrid.setDataSource(fieldDS);

  public void parseValue(){
  // on some condition
   fieldRec.setAttribute(Constant.NAME, "high"); 
  // programmaticaly set record value and that value is removed when i double click on 
   that record. 
 }
}

1 个答案:

答案 0 :(得分:1)

我希望FieldGrid是一个ListGrid。

您应该使用setFields将ListGridRecord附加到ListGrid

fieldGrid.setFields(fieldRec);

尝试将ListGrid / FieldGrid的autoSaveEdits设置为false。

fieldGrid.autoSaveEdits(false);

设置autoSaveEdits false会创建“批量更新”/“批量删除”交互,其中将为所有已编辑的单元格保留编辑(跨行,如果适用),直到调用ListGrid.saveEdits来保存特定内容调用行或ListGrid.saveAllEdits以保存批处理中的所有更改。

更新

对ListGrid使用addRowEditorExitHandler并显式设置新值,如下所示

       addRowEditorExitHandler(new RowEditorExitHandler() {

          @Override
          public void onRowEditorExit(final RowEditorExitEvent event) {
            if (event.getRecord() != null) {
              Record gridRecord = event.getRecord();
              //This will be an update operations
            }
            else {
              gridRecord = new Record();
              //This will be a new record creation
            }
            if (FieldGrid.this.validateRow(event.getRowNum())) {
              for (Object attribute : event.getNewValues().keySet()) {
                //Here you will be able to see all the newly edited values
                gridRecord.setAttribute(String.valueOf(attribute),  event.getNewValues().get(attribute));
              }
//Finally you will have a record with all unsaved values.Send it to server
              addData(gridRecord);
            }
          }
        });