How do I dynamically show and hide an entire TabContainer using DOJO?

50,119

Solution 1

There is solution for this. If you want to show TabContainer calll:

dijit.byId("tabContainer").domNode.style.display = 'block';
dijit.byId("tabContainer").resize();

and use 'none' if you want to hide TabContainer.

This works for me, but that is truth, it is not obvious :)

Solution 2

Old thread but I experienced this same issue and this is how I solved it. First, you cannot use display:none. Per the folks at DOJO, you have to use visibility:hidden with dijits or this will not work. So, you want this:

<div id="tabContainer" dojoType="dijit.layout.TabContainer" style="width:500px; height:100px;visibility:hidden;">

Then, to show this you do the following:

dojo.style("tabContainer", "visibility", "visible");

Now, the problem this poses is what you already found out. This reserves a invisible div in your viewport that is 500px wide. So if you are using a bordercontainer, there will be this empty 500px gap in your page. To resolve this, I had to create my dijits programatically and inject them into a empty div, rather than do what you did and do it declaratively. Hope this helps someone out there.

Solution 3

you should do

dijit.byId("tabContainer").domNode.style.display = 'block'

or perhaps

dijit.byId("tabContainer").domNode.style.visibility = 'hidden';

is even better

Solution 4

After you set display:block do this:

dijit.byId('tabContainer').resize();

dijit.layout widgets often don't lay themselves out properly if they are display:none (and sometimes even when visibility:hidden). You have to fiddle around in Firebug till you figure out what works!

Solution 5

Tested sucessfully with Dojo 1.10 . Use registry instead of "dijit.byId()". The method resize() only works on the dijit.layout.BorderContainer.

define([
    "dijit/registry" // registry
], function(registry) {

    var show = true;

    if (show) {
        domStyle.set(registry.byId("dijitLayoutContentPane").domNode, {'display': 'block'});
        registry.byId("dijitLayoutBorderContainer").resize();
    } else {
        domStyle.set(registry.byId("dijitLayoutContentPane").domNode, {'display': 'none'});
        registry.byId("dijitLayoutBorderContainer").resize();
    }

}
Share:
50,119
Admin
Author by

Admin

Updated on July 17, 2022

Comments

  • Admin
    Admin almost 2 years

    DOJO seems to have some quirks here. I specifically need to have the TabContainer hidden when the page loads, but then become visible after the user clicks a button. The first thing I tried is setting style.display = "none" to start, and then setting style.display = "block" on the click event. Unfortunately, this only partially works- the page will render an invisible box in the right location/dimensions, but not render the actual contents. The box's contents only get rendered when triggered by something else (for example, going to a different FF tab or suspending/resuming firebug will make the box render).

    If the style.display property is set to be visible on page load, everything works fine. You can toggle the display property and it shows or hides the tabcontainer properly. But if it's set to "none" on page load, it screws up.

    I tried a workaround of setting the style.display property to "" in the HTML but then immediately setting it to "none" in the Javascript, but it still fails- the change occurs too soon and it needs to happen after the tabcontainer renders (which can take a second or two).

    Some stripped sample code:

    HTML:
    <div id="tabContainer" dojoType="dijit.layout.TabContainer" style="width:500px; height:100px;display:none;">
    </div>

    and then the Javascript to show the tab on a user click:

    function initTabs()  
    {  
    var tabContainer = dojo.byId('tabContainer');  
    tabContainer.style.display = 'block';  
    }  
    

    How can I dynamically show/hide a TabContainer without having it start in the shown state?

  • Admin
    Admin over 14 years
    the first option doesn't work, either. The second option, modified to be what I assume you intended ('visible' and not 'hidden') works a little bit better, but still not right. Using the visibility property makes the TabContainer hide and show properly, but it still takes up space on the page while hidden (an invisible box).
  • Marijn
    Marijn over 14 years
    Well you could change the position to absolute. But that makes it kind of an ugly hack.
  • Sam007
    Sam007 about 10 years
    just an update for 1.8+, require(["dojo/dom-style"], function(domStyle) { domStyle.set(dijit.byId("tabContainer").domNode, {'display': 'block'}); dijit.byId("tabContainer").resize(); } p.s. - I know I should also edit dijit.byId("tabContainer") to require(["dojo/dom-attr"], function(domAttr){}); but u get the point
  • Perry
    Perry about 10 years
    @streetlight - Been a couple years since I worked with DOJO. You would just have to try it out. Sorry can't be more help.