对于 Ruby on Rails,当从 API 获取数据时如何使用 will_paginate 或 Kaminari? (通过网络)

发布于 2024-10-28 08:19:45 字数 238 浏览 7 评论 0原文

看起来 will_paginate 和 Kaminari 都与 ActiveRecord 紧密相关?如果通过API获取数据,那么

http://www.foo.com/fetch_data?type=products&offset=100&count=20

如何使用will_paginate或Kaminari来进行分页呢?如果不能,是否有其他分页解决方案可以在这种情况下工作?

It seems like will_paginate and Kaminari are both very tied into ActiveRecord? If the data is obtained through an API, such as

http://www.foo.com/fetch_data?type=products&offset=100&count=20

then how can you use will_paginate or Kaminari to do the pagination? If cannot, is there other pagination solution that can work in this situation?

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

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

发布评论

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

评论(2

贪恋 2024-11-04 08:19:45

我已经为我的acts_as_indexed 插件在非AR 集上进行了分页。

在您的情况下,它的工作方式如下:

def fetch(page=1, per_page=10)
  total_entries = 1000 # Or whatever method you choose to find the total entries.

  returning ::WillPaginate::Collection.new(page, per_page, total_entries) do |pager|
    url = "http://www.foo.com/fetch_data?type=products&offset=#{pager.offset}&count=#{pager.per_page}"

    results = fetch_example(url)# fetch the actual data here.

    pager.replace results
  end
end

控制器:

def index
  @records = fetch(params[:page])
end

视图:

<%= will_paginate @records %>

填写您自己的方法来获取和处理数据,并计算出总条目。

I already do pagination on a non-AR set for my acts_as_indexed plugin.

In your case it would work something like the following:

def fetch(page=1, per_page=10)
  total_entries = 1000 # Or whatever method you choose to find the total entries.

  returning ::WillPaginate::Collection.new(page, per_page, total_entries) do |pager|
    url = "http://www.foo.com/fetch_data?type=products&offset=#{pager.offset}&count=#{pager.per_page}"

    results = fetch_example(url)# fetch the actual data here.

    pager.replace results
  end
end

Controller:

def index
  @records = fetch(params[:page])
end

Views:

<%= will_paginate @records %>

Fill in your own methods for fetching and processing the data, and working out the total entries.

忆伤 2024-11-04 08:19:45

我编写了自己的函数来对对象数组进行分页。您可以修改代码以满足您自己的需要:

def array_paginate page, per_page, array
  page = page.to_i
  per_page = per_page.to_i
  total_entries = array.count

  total_pages = (total_entries / per_page.to_f).ceil
  data = format_records( array[ (page-1)*per_page..page*per_page-1] )

  {
    page: page,
    total_pages: total_pages,
    data: data,
  }
end

I wrote my own function to paginate an array of objects. You can modify the code to suit your own needs:

def array_paginate page, per_page, array
  page = page.to_i
  per_page = per_page.to_i
  total_entries = array.count

  total_pages = (total_entries / per_page.to_f).ceil
  data = format_records( array[ (page-1)*per_page..page*per_page-1] )

  {
    page: page,
    total_pages: total_pages,
    data: data,
  }
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文