Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
636 views
in Technique[技术] by (71.8m points)

asp.net mvc - loading flot chart in jquery tab workaround doesn't completely fix the issue

there is known issue with loading a flot chart in a jquery tab that is not the initial visible tab here:

this was asked here: http://osdir.com/ml/jQuery/2009-06/msg02284.html

and answered with this solution:

 .tabs-hide {
 /*display: none;*/
  position: absolute;
 left: -10000px;
 }

there is still some issues with this solution.

Lets say that the tab name that the flot chart is on is called #tab-1. jquery tabs put this in the URL string so this will work initially when you load up but if you try to send someone the URL with the #tab-1 (or any tab name in the URL) in the link, the chart will not be visible. has anyone seen a solution that always works including the case where the tab may be in the url as well.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Alternatively, change the css of the tab content class to...

.tab_content {
 display:block;
    visibility:hidden;
}

... and add the following added lines (under the //HACK!!! ... ) to your jscript.js file..

    $(document).ready(function() {
        //  When user clicks on tab, this code will be executed
        $("#tabs li").click(function() {
            //  First remove class "active" from currently active tab
        $("#tabs li").removeClass('active');

        //HACK!!! As the tabs_content HAS to initially be set to display:block in order for the flot graphs to be plotted correctly,
        // we need to manually change the visibility attribute for the tabs_content each time another tab is selected as active.
        //This assigns a variable to the tab_content of the currently active tab and changes the css visibility attribute to hidden.
            var old_tab = $("#tabs li").find("a").attr("href");
            $(old_tab).css('visibility', 'hidden');

            //  Now add class "active" to the selected/clicked tab
            $(this).addClass("active");

            //  Hide all tab content
            $(".tab_content").hide();

            //  Here we get the href value of the selected tab
            var selected_tab = $(this).find("a").attr("href");
//HACK!!! Change the css visibility attribute of the newly selected tab to visible
            $(selected_tab).css('visibility', 'visible');

            $(selected_tab).fadeIn();

            return false;
        });


});

... and providing your aspx looks like ...

    <div id="tabs" >
                        <ul id="Ul1" >
                                <li class="active"><a href="#tab1">Main</a></li>
                                <li><a href="#tab2">tab2</a></li>
                                <li><a href="#tab3">tab3</a></li>
                                <li><a href="#tab4">tab4</a></li>
                                <li><a href="#tab5">tab5</a></li>
                            </ul>
         </div>

         <div style="width:100%;float:left;">     
                  <div id="tabs_content_container">

                           <div id="tab1" class="tab_content" style="visibility:visible">
                           content for tab 1
                            </div>
                            <div id="tab2" class="tab_content"  >
                             </div>
                  </div>
         </div>

... your flot graphs will display correctly and when the relevant tab is selected!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...