will_paginate - 如果集合为空或小于每页参数,则不显示页面条目信息

发布于 2024-12-03 17:34:49 字数 513 浏览 0 评论 0原文

假设我有一个创建 WillPaginate 集合的控制器操作:

@comments = WillPaginate::Collection.new(@page_num, 15, @comments.length).concat(comments_to_paginate)

那么在我看来:

<div class="pag">
    <div clas="page_info">
      <%= page_entries_info @comments %>
    </div>
    <%= will_paginate @comments, :container => false %>
</div>

现在我想做的是不显示 page_entries_info 输出,如果 (1) 没有注释,并且 (2) 如果条目数(例如, 7) 小于每页限制 (15)。

你会如何处理这个问题?

Let's say I have a controller action that creates the WillPaginate collection:

@comments = WillPaginate::Collection.new(@page_num, 15, @comments.length).concat(comments_to_paginate)

Then in my view:

<div class="pag">
    <div clas="page_info">
      <%= page_entries_info @comments %>
    </div>
    <%= will_paginate @comments, :container => false %>
</div>

Now what I want to do is to NOT show the page_entries_info output if (1) there are no comments and (2) if the number of entries (eg, 7) is less than the per page limit (15).

How would you go about handling this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

抱着落日 2024-12-10 17:34:49

您只需要使用您希望的条件来保护您的 page_entries_info

例如

<div class="pag">
  <% if @comments.length > 0 && @comments.total_pages > 1 %>
    <div class="page_info">
      <%= page_entries_info @comments %>
    </div>
  <% end %>
  <%= will_paginate @comments, :container => false %>
</div>

,或者您可以将其放入您的控制器中并保持您的视图更清晰,如果您需要相同的变量,也可以让您有一个可以重用的变量保护视图代码的其他部分。

@comments = WillPaginate::Collection.new(@page_num, 15, @comments.length).concat(comments_to_paginate)
@show_pagination = @comments.length > 0 && @comments.total_pages > 1

然后在视图中:

<div class="pag">
  <% if @show_pagination %>
    <div class="page_info">
      <%= page_entries_info @comments %>
    </div>
  <% end %>
  <%= will_paginate @comments, :container => false %>
</div>

如果你可以处理额外的div,那么这也应该有效

<div class="pag">
  <div class="page_info">
    <%= page_entries_info(@comments) if @show_pagination %>
  </div>
  <%= will_paginate @comments, :container => false %>
</div>

You just need to guard your page_entries_info with the conditions you wish

For example

<div class="pag">
  <% if @comments.length > 0 && @comments.total_pages > 1 %>
    <div class="page_info">
      <%= page_entries_info @comments %>
    </div>
  <% end %>
  <%= will_paginate @comments, :container => false %>
</div>

Or you can put this in your controller and keep your view a little cleaner, also letting you have one variable to reuse if you need the same guard around other parts of view code.

@comments = WillPaginate::Collection.new(@page_num, 15, @comments.length).concat(comments_to_paginate)
@show_pagination = @comments.length > 0 && @comments.total_pages > 1

Then in the view:

<div class="pag">
  <% if @show_pagination %>
    <div class="page_info">
      <%= page_entries_info @comments %>
    </div>
  <% end %>
  <%= will_paginate @comments, :container => false %>
</div>

If you can deal with the extra div, then this should also work

<div class="pag">
  <div class="page_info">
    <%= page_entries_info(@comments) if @show_pagination %>
  </div>
  <%= will_paginate @comments, :container => false %>
</div>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文