2011-12-02 63 views
2

我有一個啓用了SingleSelectionModel的GWT CellTable。一旦用戶點擊了一行,onSelectionChange(...)會彈出我的確認對話框,詢問用戶是否繼續。問題是當用戶點擊「取消」時,沒有任何反應,但他無法選擇相同的行(假設CellTable中只有一行)。我知道一旦用戶點擊「取消」,我可以清除選擇,但那會再次觸發SelectionChange(..)並觸發我的確認對話框......這是一個無限循環。如何取消選擇GWT CellTable中的一行而不觸發選擇更改(...)

下面是我的代碼:

// Add SelectionModel to dTable; 
final SingleSelectionModel<Driver> ssm = new SingleSelectionModel<Driver>(); 
dTable.setSelectionModel(ssm); 
ssm.addSelectionChangeHandler(new SelectionChangeEvent.Handler() { 
@ Override 
public void onSelectionChange(final SelectionChangeEvent event) 
{ 

SC.confirm("Do you want to contact the driver?", new BooleanCallback() { 
public void execute(Boolean value) { 
if (value != null && value) { 
final Driver d = ssm.getSelectedObject(); 
dataStoreService.updateDrivers(d._UUID.toString(),tripDate.getValue(), loginInfo.getEmailAddress(),destination.getText().trim(), 
new AsyncCallback<String>() { 
public void onFailure(Throwable caught) { 

caught.printStackTrace(); 
} 

public void onSuccess(String uuid) { 
Window.alert("The driver has been notified. Please keep your reference id: "+uuid); 
} 
}); 
dataStoreService.getBookings(loginInfo.getEmailAddress(), new AsyncCallback<List<Booking>>() { 
public void onFailure(Throwable caught) { 

caught.printStackTrace(); 
} 

public void onSuccess(List<Booking> myBookings) { 
ClientUtilities.populateBookings(bookingDataProvider, myBookings); 
} 
}); 
} else { 
//clear selection 
//ssm.setSelected(ssm.getSelectedObject(), false); 

} 
} 
}); 

} 
}); 

有人能告訴我如何處理這種情況在CellTable?我接受任何解決方案。

+0

我做一點點研究,只是想知道,如果我可以使用NoSelectionModel聽SelectionChangeEvent對於這種類型的情況?但我不知道如何。 – enfany

+0

你應該接受Thomas的答案。 – Splaktar

回答

3

SelectionChangeEvent被解僱之後選擇已經改變。這不是要求確認的適當地點:已經太晚了。

您最好使用CellPreviewEvent.Handler。請參閱https://groups.google.com/d/topic/google-web-toolkit/YMbGbejU9yg/discussion,其中討論完全相同的問題(確認選擇更改)並提供示例代碼。

+0

謝謝Thomas! CellPreviewEvent.Handler解決了我的詭計。 – enfany

+1

@昆溪,那麼你應該將托馬斯的答案標記爲已接受。 –

+0

示例代碼在同一問題上給出,因爲另一個已發佈此問題http://stackoverflow.com/questions/6871591/gwt-celltable-onclick-issue – Carl

0

這裏的一個片段與解決方案對於取消選擇的行DataGrid中:

public abstract class MyDataGrid<T> extends DataGrid<T> { 

    private MultiSelectionModel<T> selectionModel_; 
    private Set<T> priorSelectionSet_; 

    .... 


    /** 
    * Allows User To Deselect A DataGrid Row By Clicking A Second Time On The Prior Selection 
    */ 
    private void addDeselectMechanism(){ 
     /* 
     NOTES:  
      1. ClickHandler() fires every time the grid is clicked. 
      2. selectionModel SelectionChangeHandler() does NOT fire when clicking 
       a row that is already selected. 
      3. Generally, ClickHandler() fires and then SelectionChangeHandler() fires, 
       but testing showed some exceptions to this order and duplicate firings. 
      4. The Current SelectedSet is Updated AFTER the ClickHandler() fires, so "natural" 
       ClickHandler() timing does not permit current SelectedSet inspections. 
      5. In this case, the scheduleDeferred() code will ALWAYS fires when a grid is clicked, 
       it will fire at a PREDICTABLE time, and AFTER the current SelectedSet has been updated.     
     */  
     super.addHandler(new ClickHandler(){ 
      @Override 
      public void onClick(ClickEvent event) { 
       Scheduler.get().scheduleDeferred(new ScheduledCommand() {  
        @Override 
        public void execute() { 
         Set<T> currentSelectedSet = selectionModel_.getSelectedSet(); 
         if((currentSelectedSet.size() == 1) && 
          (priorSelectionSet_ != null)){ 
          if((currentSelectedSet.size() == priorSelectionSet_.size()) &&      
           (currentSelectedSet.containsAll(priorSelectionSet_))){       
           selectionModel_.clear(); 
          } 
         } 
         priorSelectionSet_ = new HashSet<T>(); 
         priorSelectionSet_.addAll(selectionModel_.getSelectedSet());           
        } 
       });    

      } 
     }, ClickEvent.getType()); 
    } 

    ..... 

} 
相關問題