我使用Kaminari,Masonry和無限滾動的附加組件。我用磚石進行了無限滾動,但每次進入頁面並看到項目列表時,它都會再次呈現相同的項目至少25次,當只有26個時,會給我列出650個列出的項目。我的代碼位於下方,問題我問是爲什麼會發生這種情況?使用Kaminari和Masonry時顯示的結果相同
items_controller.rb
def index
@items = Item.order("created_at DESC").page(params[:page])
end
index.html.erb
<div id='items-ctnr' class='row'>
<div class='infinite-scroll page clearfix'>
<%= render @items %>
</div>
</div>
<nav id="page-nav">
<%= paginate @items %>
</nav>
index.js.erb的
$("#items-ctnr").append("<div class='infinite-scroll page clearfix'><%= escape_javascript(render(@items)) %></div>");
_items.html.erb
<% @items.each do |item| %>
<div class='item box'>
<div class='name'>
<%= link_to item.name, item %>
</div>
</div>
<% end %>
items.js
$(function(){
var $container = $('#items-ctnr, #user-items-ctnr');
$container.imagesLoaded(function(){
$container.masonry({
itemSelector: '.box',
columnWidth: function(containerWidth) {
return containerWidth/6;
}
});
});
$container.infinitescroll({
navSelector : '#page-nav', // selector for the paged navigation
nextSelector : '#page-nav a', // selector for the NEXT link (to page 2)
itemSelector : '.box', // selector for all items you'll retrieve
loading: {
finishedMsg: 'No more items to load.',
img: '/assets/masonry/loader.gif'
}
},
// trigger Masonry as a callback
function(newElements) {
// hide new items while they are loading
var $newElems = $(newElements).css({ opacity: 0 });
// ensure that images load before adding to masonry layout
$newElems.imagesLoaded(function(){
// show elems now they're ready
$newElems.animate({ opacity: 1 });
$container.masonry('appended', $newElems, true);
});
}
);
});
感謝,
約翰
Thx但這個沒有運氣。 –