【问题标题】:Error with 2 pagination's on same Webpage同一网页上的 2 个分页错误
【发布时间】:2014-04-25 06:05:24
【问题描述】:

我不是 ruby​​ on rails 程序员,但我在向我的页面添加第二个结果表方面已经取得了很大进展。

我遇到的问题是分页搞砸了。

首次加载时。一切看起来都不错。但是当您单击另一个页码时,它也会更改第二个表上的页面。加上将第二个表的副本添加到页面中,就像第三个表一样。

我有截图。

原始页面加载
Orignial when page loads http://www.digihaul.com/1.png

选择页面后

Once a page is selected http://www.digihaul.com/2.png

这是我的观点

.row-fluid#load_results_table
  .span12
    - if resultss.empty?
      =render partial: 'no_results', locals: {search: search}
    - else
      %h1.page-header Premium Loads
      %p= "These are our loads we are offering. We guarentee 100% payment on these loads"
    %table.table.search_results.table-condensed.table-bordered.table-striped.sortable{:id => "#{searches.search_type.pluralize}"}
      %thead
        %tr.search_header
          %th= sortable_load "origin"
          %th= sortable_load "dest"
          %th= sortable_load "pickup"
          %th= sortable_load "delivery"
          %th= sortable_load "ltl"
          %th= sortable_load "equipment_id", "Equipment"
          %th= sortable_load "weight"
          %th= sortable_load "length"
          %th= sortable_load "rate"
          -unless @searches.origin.blank?
            %th Estimated Deadhead Miles
          %th Actions
      %tbody
        - resultss.each do |result|
          %tr{:class => "#{searches.search_type}_view", :id => "#{result['id']}" }
            %td= Location.to_cs(result.origin)
            %td= Location.to_cs(result.dest)
            %td= format_date(result.pickup)
            %td= format_date(result.delivery)
            %td= full_or_par(result.ltl)
            %td= result.equipment_id ? Equipment.to_equipment_name(result.equipment_id) : ""
            %td= result.weight
            %td= result.length
            %td= result.rate
            -unless @searches.origin.blank?
              %td= Location.distance_between(@searches.origin.coords, result['origin'])
            %td
              .btn-group
                %a.btn.btn-info{ :href => "/#{searches.search_type.pluralize}/#{result['id']}" } Show
                %a.btn{ :href => "javascript:void(0)", :class => "save", user_id: @searches.user_id, :id => result['id']} Save
                %a.btn.btn-primary{ :href => "javascript:void(0)", :class => "cover_link", :user_id=> @searches.user_id } Cover 
    = will_paginate resultss, :renderer => BootstrapPagination::Rails
.row-fluid#load_results_table
  .span12
    - if results.empty?
      =render partial: 'no_results', locals: {search: search}
    - else
      %h1.page-header Search Results
      - if results.count > 1000
        %p= "Your search yielded many #{search.search_type.pluralize}"
      - else
        %p= "Your search yielded #{results.count} #{search.search_type.pluralize}"
      =render partial: 'header_buttons', locals: {search: search}
      %br
      = will_paginate results, :renderer => BootstrapPagination::Rails, id: "first_pagination"
      %table.table.search_results.table-condensed.table-bordered.table-striped.sortable{:id => "#{search.search_type.pluralize}"}
        %thead
          %tr.search_header
            %th= sortable_load "origin"
            %th= sortable_load "dest"
            %th= sortable_load "pickup"
            %th= sortable_load "delivery"
            %th= sortable_load "ltl"
            %th= sortable_load "equipment_id", "Equipment"
            %th= sortable_load "weight"
            %th= sortable_load "length"
            %th= sortable_load "rate"
            -unless @search.origin.blank?
              %th Estimated Deadhead Miles
            %th Actions
        %tbody
          - results.each do |result|
            %tr{:class => "#{search.search_type}_view", :id => "#{result['id']}" }
              %td= Location.to_cs(result.origin)
              %td= Location.to_cs(result.dest)
              %td= format_date(result.pickup)
              %td= format_date(result.delivery)
              %td= full_or_par(result.ltl)
              %td= result.equipment_id ? Equipment.to_equipment_name(result.equipment_id) : ""
              %td= result.weight
              %td= result.length
              %td= result.rate
              -unless @search.origin.blank?
                %td= Location.distance_between(@search.origin.coords, result['origin'])
              %td
                .btn-group
                  %a.btn.btn-info{ :href => "/#{search.search_type.pluralize}/#{result['id']}" } Show
                  %a.btn{ :href => "javascript:void(0)", :class => "save", user_id: @search.user_id, :id => result['id']} Save
                  %a.btn.btn-primary{ :href => "javascript:void(0)", :class => "cover_link", :user_id=> @search.user_id } Cover 
      = will_paginate results, :renderer => BootstrapPagination::Rails

控制器:

  def show
    @search = Search.find(params[:id])
    @searches = Search.find(params[:id])
    @results = @search.search(params[:page])
    @resultss = @searches.searches(params[:page])
    @search.update_attribute(:results, @results.count)
    @searches.update_attribute(:resultss, @resultss.count)
    respond_to do |format|
      format.html 
      format.js {render "results"}
      format.js {render "resultss"}
    end
  end

模型(最小化查询)

  def search(page)
    where = []
    where << PrepareSearch.states("dest", self.dest_states) unless self.dest_states.blank?
    if self.search_type == 'load'
      select = "loads.id, origin, dest, pickup, delivery, ltl, equipment_id, weight, length, rate"
      where << PrepareSearch.date('pickup', self.pickup, self.pickup_operator) unless self.pickup.blank?
    elsif self.search_type == 'truck'
      select = "trucks.id, origin, dest, available, expiration, equipment_id, comments"
      where << PrepareSearch.date('available',self.available,self.available_operator) unless self.available.blank?
    end
    where = where.join(' AND ')
    order = self.order_by ? self.order_by + " desc" : ""
    limit = "LIMIT=200"
      Module.const_get(self.search_type.capitalize).where(where).select(select).limit(limit).order(order).page(page).per_page(20)
  end


  def searches(page)
    where = []
    where << PrepareSearch.states("dest", self.dest_states) unless self.dest_states.blank?
    if self.search_type == 'load'
      select = "loads.id, origin, dest, pickup, delivery, ltl, equipment_id, weight, length, rate"
      where << PrepareSearch.date('pickup', self.pickup, self.pickup_operator) unless self.pickup.blank?
    elsif self.search_type == 'truck'
      select = "trucks.id, origin, dest, available, expiration, equipment_id, comments"
      where << PrepareSearch.date('available',self.available,self.available_operator) unless self.available.blank?
    end
    where = where.join(' AND ')
    order = self.order_by ? self.order_by + " desc" : ""
    limit = "LIMIT=200"
      Hotload.where(where).select(select).limit(limit).order(order).page(page).per_page(5)
  end

【问题讨论】:

  • 它是否与:id:“first_pagination”相同。如果我改变它会破坏任何我需要在 javascript 或任何地方修复的东西
  • 我想我得调查一下。
  • 您要么必须为另一个表创建和使用单独的页面键,要么使用 ajax。

标签: ruby-on-rails ruby pagination will-paginate


【解决方案1】:

Ajax 分页是一种方法,如 cmets 中所述,但处理它的最佳方法是更改​​每个分页块的 page 参数


参数名称

您可以使用 param_name 参数对 Kaminari 执行此操作(我知道您使用的是 will_paginate)。这基本上设置了每个分页块读取的param,允许您以不同的方式调用它们,从而控制不同的数据集:

<%= paginate collection, param_name: model.to_s %>

研究了如何使用will_paginate 做到这一点,看来you're able to use the param_name argument too:

<%= will_paginate @products, param_name: 'products_page' %>

【讨论】:

  • 这会修复我得到的重复吗?
  • 当我在分页栏上选择页面 # 时。如上图所示,它复制了第二个表格...
  • 嗯,我明白了!让我帮你看看
  • 我添加了我的模型和控制器。我仍然在合并此解决方案时遇到问题。如果你能看一下并给我一个关于我需要改变什么才能让它正常工作的指针会很棒
猜你喜欢
  • 1970-01-01
  • 2021-03-27
  • 2015-02-12
  • 1970-01-01
  • 2014-07-03
  • 2018-01-17
  • 1970-01-01
  • 2013-10-22
  • 2013-04-24
相关资源
最近更新 更多