简体   繁体   English

关闭选项卡以关注上一个选项卡而不是第一个选项卡时如何更改 dojo 内容选项卡焦点?

[英]How to change dojo content tab focus when closing a tab to focus on previous tab instead of first tab?

I'm using Dojo 's Dijit Layout for generating content tab-panes similar to Dijit Theme Tester Demo .我正在使用DojoDijit Layout来生成类似于Dijit Theme Tester Demo的内容选项卡窗格。 All of the tabs here are closeable.这里的所有选项卡都是可关闭的。

The issue is: when I close a tab it goes back to the first tab in the list instead of the previous tab (available next to it).问题是:当我关闭一个选项卡时,它会返回到列表中的第一个选项卡,而不是前一个选项卡(在它旁边可用)。

You can think of it like opening a new tab in Firefox or Chrome and try closing the last tab.... on closing tab, it changes the focus to the previous tab which is a predictable behavior for working with tabs.您可以将其想象成在 Firefox 或 Chrome 中打开一个新标签并尝试关闭最后一个标签....在关闭标签时,它会将焦点更改为上一个标签,这是使用标签的可预测行为。 But with dijit.TabContainer s, by default it goes back to the very first tab instead of previous one.但是对于dijit.TabContainer s,默认情况下它会返回到第一个选项卡而不是前一个选项卡。 This is a serious flaw when you consider the UI basics.当您考虑 UI 基础知识时,这是一个严重的缺陷。

I have checked with dojo docs, but don't found any hint on this.我已经检查过dojo文档,但没有发现任何提示。 Any idea how to it?知道怎么做吗?

Ok so when the [X] button on the dijit.layout.ContentPane (tab) is clicked an event onClose is generated, the dijit.layout.TabContainer is listening to this event, so when this happens, it executes the callback closeChild() then the function removeChild() is executed, this last one is the one you should override.好的,当单击dijit.layout.ContentPane (选项卡)上的 [X] 按钮时,会生成一个onClose事件, dijit.layout.TabContainer正在侦听此事件,因此当发生这种情况时,它会执行回调closeChild()然后执行 function removeChild() ,最后一个是您应该覆盖的那个。

The tabContainer inherits this two functions from dijit.layout.StackContainer you should check the API documentation. tabContainer 从dijit.layout.StackContainer继承了这两个函数,您应该查看 API 文档。

So for being able to modify the default behavior of the closing tabs, you should override the default functionality, in the example below i do this.因此,为了能够修改关闭选项卡的默认行为,您应该覆盖默认功能,在下面的示例中我这样做了。 Read the comments for info on where to add your logic.阅读评论以获取有关在何处添加逻辑的信息。

Eg例如

<script>
    require([
        "dojo/parser",

        "dojo/_base/lang", //this is the one that has the extend function
        "dojo/topic", //this is needed inside the overrided function

        "dijit/layout/TabContainer",
        "dijit/layout/ContentPane", 

        "dojo/domReady!"
    ], function(Parser, lang, topic, tabContainer, contentPane){
        Parser.parse();

        // this will extend the tabContainer class and we will override the method in question
        lang.extend(tabContainer, {

            // this function, i copied it from the dijit.layout.StackContainer class
            removeChild: function(/*dijit._Widget*/ page){

                // for this to work i had to add as first argument the string "startup"
                this.inherited("startup", arguments);

                if(this._started){
                    // also had to call the dojo.topic class in the require statement
                    topic.publish(this.id + "-removeChild", page);  
                }

                if(this._descendantsBeingDestroyed){ return; }

                if(this.selectedChildWidget === page){
                    this.selectedChildWidget = undefined;
                    if(this._started){
                        var children = this.getChildren();
                        if(children.length){

                            // this is what you want to add your logic
                            // the var children (array) contains a list of all the tabs
                            // the index selects the tab starting from left to right 
                            // left most being the 0 index   

                            this.selectChild(children[0]);
                        }
                    }
                }

                if(this._started){
                    this.layout();
                }
            }
        });

        // now you can use your modified tabContainer WALAAAAAAA!
        // from here on, normal programmatic tab creation
        var tc = new tabContainer({
            style: "height: 100%; width: 100%;",
        }, "tab-container-div-id");

        var cp1 = new contentPane({
            title: "First Tab",
            closable: true
        });
        tc.addChild(cp1);

        var cp2 = new contentPane({
            title: "Second Tab",
            closable: true
        });
        tc.addChild(cp2);

        var cp3 = new contentPane({
            title: "Third Tab",
            selected: true,
            closable: true
        });
        tc.addChild(cp3);

        tc.startup();
    });
</script>

In Dojo 1.10, reverting to the previous tab is the normal behaviour for TabContainers (instead of reverting to the first tab).在 Dojo 1.10 中,恢复到上一个选项卡是 TabContainers 的正常行为(而不是恢复到第一个选项卡)。

Presumably, you could use dojo/aspect to get the old behaviour (warning: not tested):据推测,您可以使用dojo/aspect来获得旧的行为(警告:未测试):

require( [ 'dijit/registry', 'dojo/aspect', 'dojo/_base/lang' ],
    function( registry, aspect, lang )
    {
        var tabContainer = registry.byId( 'tab_container');
        aspect.before( tabContainer, 'removeChild', lang.hitch( tabContainer, function( page )
        {
            if(this.selectedChildWidget === page)
            {
                this.selectedChildWidget = undefined;
                if(this._started)
                {
                    var children = this.getChildren();
                    this.selectChild( children[0] );
                }
            }

            return page;
        } ) );
    }
);

Or, alternatively, you could use the onClose extension point on a tab's ContentPane :或者,您可以在选项卡的ContentPane上使用onClose扩展点:

require( [ 'dijit/registry', 'dojo/_base/lang' ],
    function( registry, lang ) {
        newTabPane.onClose = lang.hitch(this, function () {
            // select first
            var tabContainer = registry.byId('tab_container'),
                all_tabs = tabContainer.getChildren();
            tabContainer.selectChild( all_tabs[0] );

            // allow save to go ahead
            return true;
        });
    }
);

Obviously, both these approaches would allow you to select a specific different pane on a tab being closed instead with a little tweaking...显然,这两种方法都允许您 select 关闭选项卡上的特定不同窗格,而不是稍作调整......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM