Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

Find which page an item is on

#26

Joel Nylund: "Does anyone know of a pagination helper (this one or any other) that will allow me to find which page a given entity is on? So for example, if I have a list of 100 items with 20 per page, and the list is sorted by duedate, and I add a new item and I want change the page to the correct one to show the added item."

Reported by Mislav · June 7th, 2007 @ 04:44 AM

State: open
Assigned to: Mislav Mislav

Activity

  1. Chris Wanstrath
    Chris Wanstrath

    Here's how I did it once:

    def self.page_for_pagination(id, per_page = 10)
      return if (id = id.to_i) < 1
      query = "SELECT id FROM %s ORDER BY id DESC" % table_name
      find_by_sql(query).map(&:id).index(id).to_i / per_page + 1
    end
    

    If we extract pagination logic, we could slide through a window of 200 rows or something trying to find the id...

    June 7th, 2007 @ 04:44 AM

  2. Mislav
    Mislav
    • Milestone set to will_paginate v3.0 ("agnostic")
    • Tag set to will_paginate

    October 8th, 2008 @ 11:11 PM

Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.