在jquery选项卡中加载flot图表解决方法并不能完全解决问题

时间:2009-12-29 21:09:07

标签: jquery asp.net-mvc jquery-ui-tabs flot

在jquery选项卡中加载flot图表存在已知问题,该选项卡不是此处的初始可见选项卡:

这里有人问: http://osdir.com/ml/jQuery/2009-06/msg02284.html

并回答了这个解决方案:

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

此解决方案仍存在一些问题。

假设flot图表所在的选项卡名称称为#tab-1。 jquery选项卡将其放在URL字符串中,这样在加载时最初会起作用,但是如果您尝试向链接中的某个URL带有#tab-1(或URL中的任何选项卡名称),则该图表将不会可见。有没有人看到一个总是有效的解决方案,包括选项卡也可能在网址中的情况。

3 个答案:

答案 0 :(得分:2)

或者,将选项卡内容类的css更改为...

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

...并在jscript.js文件中添加以下添加的行(在HACK !!! ...下面)..

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


});

...并提供你的aspx看起来像......

    <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>

...您的flot图表会正确显示,并且选择了相关标签后!

答案 1 :(得分:1)

对我来说,当使用#tab-1 URL直接访问特定选项卡时,图表有效,但当图表选项卡最初未激活时,图表无效。

我通过将图表生成调用包装到选项卡激活(1)中解决了问题:

$('#tabs_container').bind('tabsshow', function(event, ui) {
  if (ui.panel.id == "tab-1") {
    $.plot(...)
  }
})

其中'#tabs-container''tab-1'被替换为适当的ID。 'tabsshow'是要绑定的事件的名称。

唯一的缺点是每次显示标签时都会再次绘制图表。对我而言,这不是一个大问题,人们可能会通过例如使用一些标志变量只调用$ .plot()一次。

(1):jQuery-ui docs

中的第二个提示

答案 2 :(得分:-1)

您需要记住的主要事情是将标签js放在身体标签末尾之前。