2

SingleSelectionModel が有効になっている GWT CellTable があります。ユーザーが行をクリックすると、onSelectionChange(...) が確認ダイアログを起動し、続行するかどうかをユーザーに尋ねます。問題は、ユーザーが [キャンセル] をクリックすると、何も起こらないが、同じ行を選択できないことです (CellTable に 1 行しかないと仮定します)。ユーザーが [キャンセル] をクリックすると、選択をクリアできることはわかっていますが、それは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 でこの種の状況を処理する方法を教えてもらえますか? 私はどんな解決策にもオープンです。

4

2 に答える 2

3

は、選択が変更されSelectionChangeEvent後に発生します。ここは確認を求める適切な場所ではありません。手遅れです。

を使ったほうがいいですCellPreviewEvent.Handlerhttps://groups.google.com/d/topic/google-web-toolkit/YMbGbejU9yg/discussionを参照してください。まったく同じ問題 (選択の変更の確認) について説明し、サンプル コードが提供されています。

于 2011-12-02T17:23:13.490 に答える
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());
    }

    .....

}
于 2013-09-06T13:21:58.103 に答える