且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

GXT:如何自定义TabPanel关闭上下文菜单

更新时间:2023-08-31 22:07:34

我不认为Sencha为这个问题提供了更清洁的解决方案。是的,就像你说的那样,如果你看到了实现,关闭每个选项卡都会触发 BeforeCloseEvent ,因此你会得到一个事件列表。但是有一个解决方案。



1.如果您检查他们如何在TabPanel实现中创建closeContextMenu,您可以看到。

  closeContextMenu.add(new MenuItem(getMessages()。closeOtherTabs(),new SelectionHandler< MenuItem>(){
@Override
public void onSelection (SelectionEvent< MenuItem> event){
List< Widget> widgets = new ArrayList< Widget>();
for(int i = 0,len = getWidgetCount(); i< len; i ++) {
widgets.add(getWidget(i));
}

for(Widget w:widgets){
TabItemConfig config = getConfig(w);
if(w!= contextMenuItem&& amp; config.isClosable()){
close(w);
}
}
}

}));

以及 closeContextMenu 是受保护的,所以如果你扩展这个 TabPanel 类,你可以设置自己的菜单,而不是使用默认的菜单。然后,您可以添加自己的 SelectionHandler 并向用户提供相关消息。例如,在上面的代码中,您可以在运行for循环删除选项卡之前显示提示消息。



2.然而,上下文菜单并不是一个好主意web上下文。你不能在标签面板旁边添加一个按钮并关闭除选定标签之外的所有标签吗? TabPanel 可以访问所有面板。


I have a GXT(3.0.1) TabPanel with many tabs.

This TabPanel has an out-of-the-box CloseContextMenu with 2 options :

  • Close this tab
  • Close all other tabs

In order to react to "close tab" events and be able to eventually cancel them, I use some BeforeCloseHandler.

What I need :

  • When the user closes one tab, be able to display a confirmation dialog for this tab.
  • When the user chooses to close all other tabs, display one unique confirmation for all tabs.

The problem :

The BeforeCloseHandler is called as many times as there are some tabs to close. So, I do not find any mean to make the distinction between unique and massive closes. I also do not find any mean to customize this menu.

Does anyone have a solution or am I trying to solve the wrong problem?

I don't think there's a cleaner solution provided by Sencha for this problem. Yes as you said if you see the implementation, BeforeCloseEvent is fired for every tab close, so you get a list of events. But there is a solution for that.

1.if you check how they create the closeContextMenu in TabPanel implementation you can see.

closeContextMenu.add(new MenuItem(getMessages().closeOtherTabs(), new SelectionHandler<MenuItem>() {
          @Override
          public void onSelection(SelectionEvent<MenuItem> event) {
            List<Widget> widgets = new ArrayList<Widget>();
            for (int i = 0, len = getWidgetCount(); i < len; i++) {
              widgets.add(getWidget(i));
            }

            for (Widget w : widgets) {
              TabItemConfig config = getConfig(w);
              if (w != contextMenuItem && config.isClosable()) {
                close(w);
              }
            }
          }

        }));

and also the closeContextMenu is protected so if you extend this TabPanel class you can set your own Menu instead of using the default one. Then you can add your own SelectionHandler and provide a relevant message to the user. For example as in the above code, you can show a prompt message before running the for loop which removes the tabs.

2.However context menus are not a great idea in web context. Can't you add a button may be next to tab panel and close all the tabs except selected one ? TabPanel has access to all the panels anyway.