2014-11-06 52 views
0

我有以下CellTable CellTableGWT CellTable:如何更新文本框動態

當用戶點擊薪酬最小值。複選框,它應該將立即到期列的值複製到立即支付文本字段並重新計算現在支付日期列的值。

下面是CheckboxCell代碼和TextInputCell收費今天)列(收費敏):

private Column<AccountInvoice, Boolean> buildPayMin() { 

    columnPayMin = new Column<AccountInvoice, Boolean>(new CheckboxCell(true, false)) { 
     @Override 
     public Boolean getValue(AccountInvoice object) { 
      return object.isPayMinimum(); 
     } 

     @Override 
     public void onBrowserEvent(Context context, Element elem, AccountInvoice object, NativeEvent event){ 

      // Get event type 
      int eventType = Event.as(event).getTypeInt(); 

      // See if this is a 'change' event 
      if (eventType == Event.ONCHANGE) { 

       String value = columnMinDue.getValue(object); 

       // Get the cell to copy the value from     
       TextInputCell cell = (TextInputCell) columnPayToday.getCell(); 
       // Re-create the view data for the cell 
       TextInputCell.ViewData viewData = new TextInputCell.ViewData(value); 
       cell.setViewData(object, viewData); 
       // Refresh 
       cellTable.redraw(); 

       event.preventDefault(); 
       event.stopPropagation(); 
      } 
     } 
    }; 
    columnPayMin.setDataStoreName(columnPayMinHeader); 
    columnPayMin.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER); 
    columnPayMin.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE); 
    return columnPayMin; 
} 

// ----------------------------------------------------------- 

private Column<AccountInvoice, String> buildPayToday() { 
    columnPayToday = new Column<AccountInvoice, String>(new TextInputCell()) { 
     @Override 
     public String getValue(AccountInvoice object) { 
      return object.getPaymentAmount(); 
     } 
    }; 
    columnPayToday.setDataStoreName(columnPayTodayHeader); 
    columnPayToday.setFieldUpdater(new FieldUpdater<AccountInvoice, String>() { 
     @Override 
     public void update(int index, AccountInvoice object, String value) { 
      object.setPaymentAmount(value); 
      cellTable.redraw(); 
     } 
    }); 
    return columnPayToday; 
} 

我能得到的值複製過來,但總共爲今日薪酬列不刷新。它似乎只在手動輸入值爲付款日期文本字段時刷新。我甚至嘗試過:

columnPayToday.getFieldUpdater().update(context.getIndex(), object, value); 

哪一個也沒有幫助。

感謝您提供的任何幫助。

回答

2

ViewData表示TextInputCell中的臨時值。當您調用cellTable.redraw()時,TextInputCell將恢復爲原始值(該列的方法getValue())。

如果您只想修改TextInputCell的「視圖」狀態,或者調用accountInvoice.setPayToday#(或其他任何方法)來更新對象,然後調用ListDataProvider上的refresh(),則不要重繪表格與重繪相比,更有效地更新表格)。

+0

謝謝您的回覆。 請參閱我如何根據您的建議修改代碼:http://pastebin.com/Z4bQYrzf 不幸的是,它仍然無法正常工作。 ***'Window.alert' ***只會觸發其中一個,不知何故會觸發它,並殺死它,即使再次選中該複選框也不會觸發。我也決定使用*** FieldUpdater ***代替*** onBrowserEvent ***,它沒有效果,但代碼更短。 – user1576840 2014-11-06 04:52:26

+1

您仍然調用'cellTable.redraw()' - 這次是在另一列的FieldUpdater中。你可憐的桌子一直在重繪!你幾乎不應該在代碼中的任何地方調用redraw()。 – 2014-11-06 05:00:43

+0

謝謝!我解決了這個問題,現在它工作得很漂亮! – user1576840 2014-11-06 14:46:35