如何在不触发onSelectionChange(...)的情况下取消选择GWT CellTable中的行

时间:2011-12-02 15:07:07

标签: google-app-engine gwt celltable

我有一个启用了SingleSelectionModel的GWT CellTable。一旦用户点击一行,onSelectionChange(...)将激活我的确认对话框,询问用户是否继续。问题是当用户点击“取消”时,没有任何反应,但他无法选择同一行(假设CellTable中只有一行)我知道我可以在用户点击“取消”后清除选择,但这样做再次触发onSelectionChange(..)并触发我的确认对话.....这是一个无限循环。

以下是我的代码:

// 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中的这种情况吗?我愿意接受任何解决方案。

2 个答案:

答案 0 :(得分:3)

选择发生更改后,SelectionChangeEvent被解雇。这不是要求确认的合适地方:现在为时已晚。

你最好使用CellPreviewEvent.Handler。请参阅https://groups.google.com/d/topic/google-web-toolkit/YMbGbejU9yg/discussion,其中讨论完全相同的问题(确认选择更改)并提供示例代码。

答案 1 :(得分: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());
    }

    .....

}