【问题标题】:RoR: Sorting paginated products on an endless pageRoR:在无穷无尽的页面上对分页产品进行排序
【发布时间】:2014-06-07 18:57:19
【问题描述】:

我正在使用 will_paginate gem 在无限/无限滚动页面上对产品进行分页。分页工作正常。

但是为什么我尝试对该页面上列出的产品进行排序,只有到该点已加载的产品才会被排序,而其余产品在加载后以随机顺序出现。

下面是我的控制器,根据order参数进行排序:

def index
    @order = params[:order]                                                 
    @query = params[:tag]                                                   
    @taxon = params[:taxon]                                                 
    if @order.nil?
        @order = "relevance"                                                        
    end
    @products = queryTags(params, @taxon)                                       
    if ! @products.nil?
        if @order == "pricelow"
            @products = @products.sort_by{ |k| k['variants'][0]['Price'] }
        elsif @order == "pricehigh"
            @products = @products.sort_by{ |k| -k['variants'][0]['Price'] }
        elsif @order == "relevance"
            @products = @products.sort_by{ |k| -k['productRelevance'] }
        elsif @order == "pop"
            @product_group = @products.map{|product| product['id']}
            @result = Product.where(id: @product_group).descend_by_popularity
            @products = @result.map{|r| @products.find{|p| p['id'] == r['id']}}
        end
    end
    @productsProxy = Array.new
    if @products != nil
        @products.each do |p|
            tempProduct = ProductProxy.new(p)
            @productsProxy.push(tempProduct)
        end
    else
        @productProxy = []
    end
    if @order == "new"
        @productsProxy.sort!{ |a,b| b.productAvailableDateTs <=> a.productAvailableDateTs }
    end
    @productsProxy = @productsProxy.paginate(page: params[:page], per_page: 20)
    respond_with("Search Results") do |format|
        format.js
        format.html
        format.xml { render :xml => @productsProxy, :only => [:name, :permalink, :description, :mrp], :methods => [:designer, :taxons, :price] }
    end
end

为什么即使我对整个 @productsProxy 数组进行排序,也只对加载的产品进行排序。

谢谢

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 pagination


    【解决方案1】:

    确保对下一页的请求包含参数 {order: 'new'}。对于常规分页链接使用

    will_paginate(@productProxy, :params => { :order => params[:order] || 'new' })
    

    【讨论】:

      猜你喜欢
      • 2014-05-12
      • 1970-01-01
      • 2013-01-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多