How to deselect a row in GWT CellTable without firing onSelectionChange(…)

余生长醉 提交于 2019-12-11 01:57:44

问题


I have a GWT CellTable with SingleSelectionModel enabled. Once a user clicks a row, the onSelectionChange(...) fires up my Confirmation Dialog which asks the user whether to proceed or not. The issue is when the user clicks 'Cancel', nothing happens but he is not able to select the same row (assume only 1 row in the CellTable) I know I can clear the selection once the user clicks 'Cancel', but that will fires onSelectionChange(..) again and triggers my Confirmation Dialog ..... it's an infinite loop.

The following is my code:

// 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);

}
}
});

}
});

Can someone tell me how to handle this kind of situation in CellTable? I'm open for any solutions.


回答1:


The SelectionChangeEvent is fired after the selection has changed. This is not the appropriate place to ask for confirmation: it's too late.

You'd better use a CellPreviewEvent.Handler. See https://groups.google.com/d/topic/google-web-toolkit/YMbGbejU9yg/discussion which discusses the exact same issue (confirm selection change) and provides sample code.




回答2:


Here's A Snippet With The Solution For Deselecting Rows In A 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());
    }

    .....

}


来源:https://stackoverflow.com/questions/8358325/how-to-deselect-a-row-in-gwt-celltable-without-firing-onselectionchange

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!