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

发布于 2024-12-19 03:35:07 字数 1582 浏览 1 评论 0原文

我有一个启用了 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 中处理这种情况吗?我愿意接受任何解决方案。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

美煞众生 2024-12-26 03:35:07

SelectionChangeEvent 在选择更改后被触发。这不是要求确认的合适地点:已经太晚了。

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

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.

听不够的曲调 2024-12-26 03:35:07

以下是取消选择 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());
    }

    .....

}

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());
    }

    .....

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文