2011-12-02 62 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()); 
    } 

    ..... 

} 
相关问题