Search code examples
gwtsmartgwt

smartgwt ListGridRecord programmatically editing issue


I am using smartgwt and I have a ListGrid in which I have some populated values in ListGridRecord. now if I am setting any listGridRecord Field value programmatically through setAttribute(String fieldName, String value) and refreshing the field through ListGridRecord.refreshFields(), then values get reflected to the UI. but the problem is if i am editing that same ListGridRecord by double click. then the value get lost or removed from the 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. 
 }
}

Solution

  • I hope the FieldGrid is a ListGrid.

    You shoud use setFields to attach ListGridRecord to ListGrid

    fieldGrid.setFields(fieldRec);
    

    Try setting the ListGrid/FieldGrid's autoSaveEdits to false.

    fieldGrid.autoSaveEdits(false);
    

    Setting autoSaveEdits false creates a "mass update" / "mass delete" interaction where edits will be retained for all edited cells (across rows if appropriate) until ListGrid.saveEdits is called to save a particular row, or ListGrid.saveAllEdits is called to save all changes in a batch.

    Update

    Use addRowEditorExitHandler for ListGrid and explicitly set the new values like below

           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);
                }
              }
            });