开发者

how to convert flextable cell into editable text cell in GWT

Here is the code I tried to make the flextable's cell editable

The flex table is loaded with db values, when user clicks on the cell of flextable, it has to become editable and the user entered value has to be stored in db, after the user clicks submit button which is present at each row.

I'm using EditTextCell(), to make the cell editable but it not becoming editable when I test it. I have included all my codes below. Please let me know , if i'm missing anything.

 private List<PendingChange&开发者_开发技巧lt;?>> pendingChanges = new ArrayList<PendingChange<?>>();   
private List<AbstractEditableCell<?, ?>> editableCells = new   ArrayList<AbstractEditableCell<?, ?>>();   
CellTable cellTable= new CellTable<MessageEvent>();   
EditTextCell editCell = new EditTextCell();    
protected FlexTable flextable;
//flextable creation   
private final void createWorkflows(List<MessageEvent> theWorkflowMessageEvents, boolean isSelectAll) {   
    int row = 1;      
    if (theWorkflowMessageEvents != null) {   
        for (final MessageEvent workflowMessageEvent : theWorkflowMessageEvents) {     
             flextable.getRowFormatter().setStyleName(row,ACTIVE_COLLECTION);   
                flextable.getCellFormatter().setHorizontalAlignment(row, 0, HasHorizontalAlignment.ALIGN_LEFT);   
                flextable.getCellFormatter().setWordWrap(row, 0, false);   
                flextable.setText(row, 0, workflowMessageEvent.getTransferReceived()); 
                flextable.getCellFormatter().setHorizontalAlignment(row, 1, HasHorizontalAlignment.ALIGN_LEFT);   
                flextable.getCellFormatter().setWordWrap(row, 1, false);   
                flextable.setText(row, 1, workflowMessageEvent.getLoadReceived());   
                makeFlexTableEditable() ;
Button submitButton= new Button("Submit");   
                flextable.getCellFormatter().setHorizontalAlignment(row, 3, HasHorizontalAlignment.ALIGN_LEFT);   
                flextable.getCellFormatter().setWordWrap(row, 3, false);   
                flextable.setWidget(row, 3,submitButton );   
                submitWorklow(submitButton,row, workflowMessageEvent);   
                flextable.getRowFormatter().setVisible(row, true);   
                row++;         
              }
}
 }
     //adding flextable to main panel   
protected void displayPendingWorkflows(final List<MessageEvent> theWorkflowMessageEvents) {
this.createPendingWorkflows(theWorkflowMessageEvents, false);   
//some code   
mainPanel.add(flextable);   
mainPanel.add(cellTable);   
}
//code for making flex table editable for TransferReceived column   
 private void makeFlexTableEditable() {   
         addColumn(new EditTextCell(), new GetValue() {   
                @Override  
                public String getValue(MessageEvent workflowMessageEvent) {   
                    return workflowMessageEvent.getTransferReceived();   
                }   
            }, new FieldUpdater<MessageEvent, String>() {   
                  public void update(int index, MessageEvent workflowMessageEvent, String value) {
 try { pendingChanges.add(new TransferReceived(workflowMessageEvent, value));
  }catch (Exception e) {
}
}
 });
   }  


     private <C> Column<MessageEvent, String> addColumn(EditTextCell cell,     
         final GetValue<String> getter,FieldUpdater<MessageEvent, String> fieldUpdater) {   
        Column<MessageEvent, String> transColumn = new Column<MessageEvent, String>(cell){    
            @Override     
            public String getValue(MessageEvent object) {   
                return getter.getValue(object);    
                }    
            };   
            transColumn.setFieldUpdater(fieldUpdater);    

                if (cell instanceof AbstractEditableCell<?, ?>) {   
                  editableCells.add((AbstractEditableCell<?, ?>) cell);   
            }   
            cellTable.addColumn(transColumn);   
            return transColumn;   
    }

  /**  
   * A pending change to a {@link MessageEvent}. Changes aren't committed  
   * immediately to illustrate that cells can remember their pending changes.  
   *  
   * @param <T> the data type being changed  
   */  
  private abstract static class PendingChange<T> {   
    private final MessageEvent message;   
    private final T value;   

    public PendingChange(MessageEvent message, T value) {   
      this.message = message;   
      this.value = value;   
    }   

    /**  
     * Commit the change to the contact.  
     */  
    public void commit() {   
      doCommit(message, value);   
    }   

    /**  
     * Update the appropriate field in the .  
     *  
     * @param message to update  
     * @param value the new value  
     */  
    protected abstract void doCommit(MessageEvent message, T value);   
  }   
  /**  
   * Updates the Transfered Received.  
   */  
  private static class TransferReceived extends PendingChange<String> {   

    public TransferReceived(MessageEvent message, String value) {   
      super(message, value);   
    }   

    @Override  
    protected void doCommit(MessageEvent message, String value) {   
        message.setTransferReceived(value);   
    }   
  }   

/**  
   * Get a cell value from a record.  
   *  
   * @param <C> the cell type  
   */  
  private static interface GetValue<C> {   
    C getValue(MessageEvent message);   
  } 


I did something like this in my app. Sorry if the syntax is a bit off but the main idea is to use a clickevent and then get this events position and exchange the widget in that position.

final FlexTable flexTable = new FlexTable();
flexTable.addClickHandler(new ClickHandler() {
    public void onClick(ClickEvent event) {
        Cell cell = flexTable.getCellForClickEvent(event);
        final int row = cell.getRow();
        final int column = cell.getIndex();
        final TextBox textBox = new TextBox();
        // Get the text from the cell in some way. Maybe use flextTable.getHTML(row, column) or what ever you prefer
        // textBox.setText("Something other than this");
        textBox.addKeyDownHandler(new KeyDownHandler() {
           public void onKeyDownEvent(KeyDownEvent event) {
                int code = event.getNativeKeyCode();
                if (KeyCodes.KEY_ENTER == code) {
                   flexTable.setWidget(row, column, new Label(textBox.getText()));
                }
           }
        });
        flexTable.setWidget(row, column, textBox);
        // You may also need something like this
        textBox.setFocus(true);
    }
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜