Rails will_paginate 和制表符
Rails will_paginate and tabs
我的选项卡与 will_pagination 一起使用时遇到问题。当我按下 Tab 2 时,它会显示正确的内容,但是当我按下分页 link,例如第 2 页时。它会让我回到 Tab 1 视图。在此之后,当我按下 Tab 2 时,它会出现 Tab 2 的第 2 页内容。这里的问题是为什么当我按下 Tab 2 中的分页 link 时它会带我回到我的 Tab 1 视图。
我的查看代码
<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>
谢谢
根据here的回答,你可以针对问题设计如下代码:
<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>
它将当前选择的标签保存到location.hash
。并在您导航到新页面时选择它。
我的选项卡与 will_pagination 一起使用时遇到问题。当我按下 Tab 2 时,它会显示正确的内容,但是当我按下分页 link,例如第 2 页时。它会让我回到 Tab 1 视图。在此之后,当我按下 Tab 2 时,它会出现 Tab 2 的第 2 页内容。这里的问题是为什么当我按下 Tab 2 中的分页 link 时它会带我回到我的 Tab 1 视图。
我的查看代码
<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>
谢谢
根据here的回答,你可以针对问题设计如下代码:
<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>
它将当前选择的标签保存到location.hash
。并在您导航到新页面时选择它。