cleve yeo
cleve yeo

Reputation: 103

Rails will_paginate and tabs

I have a problem with working my tab along with will_pagination. When i press Tab 2, it will show the correct content, but when i press on the pagination link, example page 2. It will bring me back to my Tab 1 view. After this, when i press back Tab 2, it will appear page 2 content of Tab 2. The problem here is why does it bring my back to my Tab 1 view when i press on the pagination link in Tab 2.

My View Code

<div>
<ul class="nav nav-tabs" id="TabLength">
    <% @biscuit.each_index do |i| %>
      <% if i == 0 %>
        <li class="active"><a data-toggle="tab" href="#<%= @biscuit[i] %>"><%= @biscuit[i] %></a></li>
      <% else %>
        <li><a data-toggle="tab" href="#<%= @biscuit[i] %>"><%= @biscuit[i] %></a></li>
      <% end %>
    <% end %>
</ul>
<div class="tab-content">
  <% @biscuit.each_index do |i| %>
    <% if i == 0 %>
      <div class="tab-pane fade in active" id="<%= @biscuit[i] %>">
        <div class="row" id="PictureSetting">
            <% @testpage.each do |i| %>
              <div class="col-md-4" id="ProductPic">
                <%= image_tag(i.name , class:"img-thumbnail", size:"180x180") %>
              </div>
          <% end %>
        </div>
        <%= will_paginate @testpage, :param_name => 'user_page' %>
      </div>
    <% elsif i == 1 %>
      <div class="tab-pane fade" id="<%= @biscuit[i] %>">
        <div class="row" id="PictureSetting">
          <% @memberpage.each do |i| %>
              <div class="col-md-4" id="ProductPic">
                <%= image_tag(i.name , class:"img-thumbnail", size:"180x180") %>
              </div>
          <% end %>
        </div>  
        <%= will_paginate @memberpage, :param_name => 'choco_page' %>
      </div>
    <% else %>
      <div class="tab-pane fade" id="<%= @biscuit[i] %>">
        <div class="row" id="PictureSetting">
          <h1>hello</h1>
        </div>  
      </div>
    <% end %>
  <% end %>
</div>
</div>

Thanks

Upvotes: 0

Views: 973

Answers (1)

dimakura
dimakura

Reputation: 7655

Based on answer from here, you can design the following code for the problem:

<script>
  // open tab on click
  $('#TabLength a').click(function (e) {
    e.preventDefault();
    $(this).tab('show');

    // getting tab id
    var id = $(e.target).attr("href").substr(1);

    // change hash value for all pages
    $('ul.pagination > li > a').each(function(i, pageAnchor) {
      pageAnchor.hash = id;
    });
  });

  // assign tab id to location hash
  $("ul.nav-tabs > li > a").on("shown.bs.tab", function (e) {
    var id = $(e.target).attr("href").substr(1);
    window.location.hash = id;
  });

  // open initial hash
  var hash = window.location.hash;
  $('#TabLength a[href="' + hash + '"]').tab('show');

  // UPDATE
  $('ul.pagination > li > a').each(function(i, pageAnchor) {
    pageAnchor.hash = hash;
  });
</script>

It saves currently selected tab into location.hash. and selects it when you navigate to a new page.

Upvotes: 2

Related Questions