1

我是 Rails 新手,我想知道如何按特定列进行分页和排序。我可以在一个表中执行此操作,但是当有多个表并且排序列不在主表中时。

我试了很多次都失败了,我也在网上搜索过,也是徒劳。现在我需要你的帮助......请帮助!

虽然分页我正在使用这个:

# encoding: utf-8
  module Pagination
    class Paginator
      attr_reader :item_count, :per_page, :page, :page_param

      def initialize(*args)
        if args.first.is_a?(ActionController::Base)
          args.shift
          ActiveSupport::Deprecation.warn "Paginator no longer takes a controller instance as the first argument. Remove it from #new arguments."
        end
        item_count, per_page, page, page_param = *args

        @item_count = item_count
        @per_page = per_page
        page = (page || 1).to_i
        if page < 1
          page = 1
        end
        @page = page
        @page_param = page_param || :page
      end

      def offset
        (page - 1) * per_page
      end

      def first_page
        if item_count > 0
          1
        end
      end

      def previous_page
        if page > 1
          page - 1
        end
      end

      def next_page
        if last_item < item_count
          page + 1
        end
      end

      def last_page
        if item_count > 0
          (item_count - 1) / per_page + 1
        end
      end

      def first_item
        item_count == 0 ? 0 : (offset + 1)
      end

      def last_item
        l = first_item + per_page - 1
        l > item_count ? item_count : l
      end

      def linked_pages
        pages = []
        if item_count > 0
          pages += [first_page, page, last_page]
          pages += ((page-2)..(page+2)).to_a.select {|p| p > first_page && p < last_page}
        end
        pages = pages.compact.uniq.sort
        if pages.size > 1
          pages
        else
          []
        end
      end

      def items_per_page
        ActiveSupport::Deprecation.warn "Paginator#items_per_page will be removed. Use #per_page instead."
        per_page
      end

      def current
        ActiveSupport::Deprecation.warn "Paginator#current will be removed. Use .offset instead of .current.offset."
        self
      end
    end

    # Paginates the given scope or model. Returns a Paginator instance and
    # the collection of objects for the current page.
    #
    # Options:
    #   :parameter     name of the page parameter
    #
    # Examples:
    #   @user_pages, @users = paginate User.where(:status => 1)
    #
    def paginate(scope, options={})
      options = options.dup
      finder_options = options.extract!(
        :conditions,
        :order,
        :joins,
        :include,
        :select
      )
      if scope.is_a?(Symbol) || finder_options.values.compact.any?
        return deprecated_paginate(scope, finder_options, options)
      end

      paginator = paginator(scope.count, options)
      collection = scope.limit(paginator.per_page).offset(paginator.offset).to_a

      return paginator, collection
    end

    def deprecated_paginate(arg, finder_options, options={})
      ActiveSupport::Deprecation.warn "#paginate with a Symbol and/or find options is depreceted and will be removed. Use a scope instead."
      klass = arg.is_a?(Symbol) ? arg.to_s.classify.constantize : arg
      scope = klass.scoped(finder_options)
      paginate(scope, options)
    end

    def paginator(item_count, options={})
      options.assert_valid_keys :parameter, :per_page

      page_param = options[:parameter] || :page
      page = (params[page_param] || 1).to_i
      per_page = options[:per_page] || per_page_option
      Paginator.new(item_count, per_page, page, page_param)
    end

    module Helper
      include Redmine::I18n

      # Renders the pagination links for the given paginator.
      #
      # Options:
      #   :per_page_links    if set to false, the "Per page" links are not rendered
      #
      def pagination_links_full(*args)
        pagination_links_each(*args) do |text, parameters, options|
          if block_given?
            yield text, parameters, options
          else
            link_to text, params.merge(parameters), options
          end
        end
      end

      # Yields the given block with the text and parameters
      # for each pagination link and returns a string that represents the links
      def pagination_links_each(paginator, count=nil, options={}, &block)
        options.assert_valid_keys :per_page_links

        per_page_links = options.delete(:per_page_links)
        per_page_links = false if count.nil?
        page_param = paginator.page_param

        html = ''
        if paginator.previous_page
          # \xc2\xab(utf-8) = &#171;
          text = "\xc2\xab " + l(:label_previous)
          html << yield(text, {page_param => paginator.previous_page}, :class => 'previous') + ' '
        end

        previous = nil
        paginator.linked_pages.each do |page|
          if previous && previous != page - 1
            html << content_tag('span', '...', :class => 'spacer') + ' '
          end
          if page == paginator.page
            html << content_tag('span', page.to_s, :class => 'current page')
          else
            html << yield(page.to_s, {page_param => page}, :class => 'page')
          end
          html << ' '
          previous = page
        end

        if paginator.next_page
          # \xc2\xbb(utf-8) = &#187;
          text = l(:label_next) + " \xc2\xbb"
          html << yield(text, {page_param => paginator.next_page}, :class => 'next') + ' '
        end

        html << content_tag('span', "(#{paginator.first_item}-#{paginator.last_item}/#{paginator.item_count})", :class => 'items') + ' '

        if per_page_links != false && links = per_page_links(paginator, &block)
          html << content_tag('span', links.to_s, :class => 'per-page')
        end

        html.html_safe
      end

      # Renders the "Per page" links.
      def per_page_links(paginator, &block)
        values = per_page_options(paginator.per_page, paginator.item_count)
        if values.any?
          links = values.collect do |n|
            if n == paginator.per_page
              content_tag('span', n.to_s)
            else
              yield(n, :per_page => n, paginator.page_param => nil)
            end
          end
          l(:label_display_per_page, links.join(', ')).html_safe
        end
      end

      def per_page_options(selected=nil, item_count=nil)
        options = Setting.per_page_options_array
        if item_count && options.any?
          if item_count > options.first
            max = options.detect {|value| value >= item_count} || item_count
          else
            max = item_count
          end
          options = options.select {|value| value <= max || value == selected}
        end
        if options.empty? || (options.size == 1 && options.first == selected)
          []
        else
          options
        end
      end
    end
  end
end
4

3 回答 3

1

我认为Kaminari gem现在比 will_paginate 好一点。也看看那个。

于 2013-08-21T11:03:13.923 回答
0

你没试过'will_paginate' gem 吗?对我来说,这似乎比您的代码中的任何内容都更清晰,并且有据可查。

在这里,看看:https ://github.com/mislav/will_paginate

于 2013-08-21T08:37:25.180 回答
0

如果你想按数量对元素进行分页,比如“每页 100 篇文章” ,我认为will_paginate是最好的解决方案。

但是问题出现了,如果你尝试按日期等对元素进行分页,当你无法预测时,页面上会有多少元素。在这种情况下,我使用这个解决方案will_paginate can it order by day。对我来说效果很好。

于 2013-08-21T08:48:44.960 回答