-2

I'm trying to figure out how to display tab content in my Rails 4 app.

I recently asked this question - the purpose of which was to figure out how to set the active tab, based on whether an attribute was set to true or false. By incorporating the solution outlined in that post (link below), I've created a new problem.

Rails 4 - how to designate active tab to first instance of a true condition

Now - when I try to render the page, if the first tab attribute is not true, the next tab has the active display on it, but the content for that tab is only revealed once I click on the the tab label.

These are my tabs:

<div class="dp-tab-1">
                                    <ul class="dp-tab-list row" id="myTab">
                                        <% if @project.package.has_background_ip == true %>
                                            <li class="col-md-2 col-xs-6 <%= 'active' if firstActiveTab == 1 %>" >
                                            <!-- <li class="col-md-2 col-xs-6 active" > -->
                                                <a href="#tab-content-first">
                                                    <span class="glyph-item" data-icon="&#xe043;"></span> 
                                                    <span>BACKGROUND INTELLECTUAL PROPERTY</span>
                                                </a>
                                            </li>
                                        <% end %>

                                        <% if @project.package.has_data == true %>    
                                            <li class="col-md-2 col-xs-6 <%= 'active' if firstActiveTab == 2 %>" >
                                            <!-- <li class="col-md-2 col-xs-6"> -->
                                                <a href="#tab-content-second">
                                                    <div class="glyph-item" data-icon="&#xe05c;"></div>
                                                    <span>DATA</span>
                                                </a>
                                            </li>
                                        <% end %>

                                        <% if @project.package.has_funding == true %>   
                                            <li class="col-md-2 col-xs-6 <%= 'active' if firstActiveTab == 3 %>" >
                                            <!-- <li class="col-md-2 col-xs-6"> -->
                                                <a href="#tab-content-third">
                                                    <div class="glyph-item" data-icon="&#xe04c;"></div>
                                                    <span>FUNDING</span>
                                                </a>
                                            </li>
                                        <% end %>

                                        <% if @project.package.has_materials == true %>   
                                            <li class="col-md-2 col-xs-6 <%= 'active' if firstActiveTab == 4 %>" >
                                            <!-- <li class="col-md-2 col-xs-6"> -->
                                                <a href="#tab-content-fourth">
                                                    <div class="glyph-item" data-icon="&#xe04c;"></div>
                                                    <span>MATERIALS</span>
                                                </a>
                                            </li>
                                        <% end %>

These are the corresponding tab contents:

<div class="tab-pane row fade in active" id="tab-content-first">
                                                <% if @project.package.has_background_ip == true %>
                                                    <div class="col-md-6 text-center">
                                                        <%= image_tag(@project.package.background_ip.bip_image, :class=>"wow fadeInLeft img-responsive", :alt=>"123") %>

                                                    </div>
                                                    <div class="col-md-6">
                                                        <div class="tab-inner">
                                                            <h4>EXISTING PROPERTY</h4>
                                                            <p class='medium-text'>
                                                                <%= render 'projects/bipparticulars' %>
                                                            </p>
                                                            <br/>

                                                            <a class='btn btn-danger btn-line'>CHECK THESE TERMS</a>

                                                        </div>
                                                    </div>
                                                <% end %>    
                                            </div>


                                        <div class="tab-pane row fade" id="tab-content-second">
                                            <div class="col-md-6">
                                                <div class="tab-inner">
                                                    <h4>DATA</h4>
                                                    <p class='medium-text'>
                                                        <%= render 'projects/dataparticulars' %>
                                                    </p>
                                                    <br/>
                                                    <% if @project.package.datum.survey_link == true %>
                                                        <a class='btn btn-danger btn-line'>SURVEY</a>
                                                    <% end %>


                                                </div>
                                            </div>

                                        </div>


                                        <div class="tab-pane row fade" id="tab-content-third">
                                            <div class="col-md-6">
                                                <div style="position: relative">


                                                    <!-- <img src="images/mockup2a.png" class='wow slideInLeft img-responsive' style='position: absolute;left: 0px; bottom: 0; max-width: 80%;' data-wow-delay='0.1s' alt=""/> -->
                                                    <!-- <img src="images/mockup2.png" class='wow slideInLeft img-responsive'  style='padding-left: 60px;' alt=""/> -->
                                                </div>
                                            </div>
                                            <div class="col-md-6">
                                                <div class="tab-inner">
                                                    <h4>FUNDING</h4>
                                                    <p class='medium-text'>
                                                        <%= render 'projects/fundingparticulars' %>
                                                    </p>
                                                    <br/>
                                                    <a class='btn btn-danger btn-line'>PROPOSED TERMS</a>
                                                    <!-- btn-primary -->
                                                </div>
                                            </div>
                                        </div>

                                        <div class="tab-pane row fade" id="tab-content-fourth">
                                            <div class="col-md-6">
                                                <div class="tab-inner">
                                                    <h4>EQUIPMENT & MATERIALS</h4>
                                                    <p class='medium-text'>
                                                        <%= render 'projects/materialsparticulars' %>
                                                    </p>
                                                    <br/>
                                                    <a class='btn btn-danger btn-line'>PROPOSED TERMS </a>
                                                </div>
                                            </div>
                                            <div class="col-md-6">
                                                <%= image_tag(@project.package.background_ip.bip_image, :class=>"wow fadeInUp img-responsive", :alt=>"123") %>
                                            </div>
                                        </div>

If background IP is not true, has_data is true, then the tab setting correctly shows the data tab as active, but the content of that tab does not display until I click on the data tab. By contrast, if the background_ip attribute is true, then the page displays correctly, with the tab content for background ip displayed.

The difference is that 'active' is included in the styling for the background ip tab content. But that's only correct as long as the attribute is set to true.

I tried adding the logic <%= 'active' if firstActiveTab == 1 %> to the tab contents panels, but it didn't work.

Can anyone see what I now need to do in order to have my tab content display for the active tab?

Community
  • 1
  • 1
Mel
  • 2,481
  • 26
  • 113
  • 273
  • what is it that opens the content when you click on it? is it a javascript function? can you run that javascript at the bottom of the page saying "find the active tab and open its contents" ? – Taryn East Jun 08 '16 at 05:59
  • @TarynEast - there is no javascript. I think it's just the tab labelling. The first tab (called background_up) has all the same styling, and when that attribute is set to true, the related tab content shows when the page renders, however, that doesnt happen for any of the other tabs (when it is the first tab). In that case, the active tab doesnt show anything in the tab panel until I click on the tab label. I think it has something to do with the active label in the css. – Mel Jun 08 '16 at 08:08

1 Answers1

0

I guess you are not activating the relevant div with the tab header. when you do

<li class="col-md-2 col-xs-6 <%= 'active' if firstActiveTab == 2 %>">
  <a href="#tab-content-second"></a>
</li>

Add the active class to div as well.

<div class="tab-pane row <%= firstActiveTab == 2 ? 'active' : 'fade' %>" id="tab-content-second">
  // Your code here
</div>
noman tayyab
  • 355
  • 3
  • 14