【问题标题】:How to sort table columns in ascending or descending order by clicking the header如何通过单击标题按升序或降序对表格列进行排序
【发布时间】:2020-06-13 06:04:23
【问题描述】:

我有一个store with items。每件商品都有价格。

我尝试通过价格标头的价格升序和降序对它们进行排序。当我点击价格时,所有 10 美元到 50 美元之间的商品都正确排序,但低于 10 美元和高于 50 美元的商品不会与它们一起排序。

这是我的代码:

class TeethersController < ApplicationController
  before_action :authenticate_user!,except: [:index,:show]
  helper_method :sort_column, :sort_direction
  before_action :find_teether, only: [:destroy,:edit,:update,:show]

  def index
    @teethers= Teether.all.order(sort_column + ' ' + sort_direction)
    @colors = Array.new
    @types = Array.new
    @teethers.each {|t| @types << t.types.pluck(:name) }
    @teethers.each {|t| @colors << t.colors.pluck(:name) }
    if params[:search]
      @search_term = params[:search]
      @teethers= @teethers.search_by(@search_term)
    end
    if params[:type_id]
      @types = Typation.where(type_id: params[:type_id])
      @teethers = @types.map(&:teether)
    end
  end

  def create
    @teether = Teether.new(teether_attributes)

    if @teether.save
      redirect_to teethers_path, notice: "Thank you... Your teether information was created successfully."
    else
      flash.now[:error] = "Please correct the form"
      render :new
    end
  end

  def new
    @teether=Teether.new 
  end
  private
  def find_teether
    @teether = Teether.find(params[:id])
  end

  def sort_column
    Teether.column_names.include?(params[:sort]) ? params[:sort] : "name"
  end

  def sort_direction
    %w[asc desc].include?(params[:direction]) ? params[:direction] : "asc"
  end

  def teether_attributes
    teether_attributes = params.require(:teether).permit([:name,:description,:price,:image,:keywords,:status,:quantity,:discount,:kind,{type_ids: []},:gender,:color,:theme])
  end
end

我的应用助手是:

module ApplicationHelper
   def sortable(column, title = nil)
    title ||= column.titleize
    direction = (column == sort_column && sort_direction == "asc")? "desc" : "asc"
    link_to title, :sort => column, :direction => direction
  end
end

这在我的索引中:

<h3 id="sort"><label>Sort: </label><span> | </span><span><%= sortable "price" %><span class="caret"></span></span></h3>

在数据库中

class CreateTeethers < ActiveRecord::Migration[5.2]
  def change
    create_table :teethers do |t|
      t.string :name
      t.text :description
      t.string :price

      t.timestamps
    end
  end
end


class AddDiscountToTeether < ActiveRecord::Migration[5.2]
  def change
    add_column :teethers, :discount, :text
    add_column :teethers, :kind, :text
  end
end

【问题讨论】:

  • 你能显示你的牙胶表模式吗?看起来它是按字母顺序排序的,这让我觉得“价格”是一个字符串?另外(你已经注意到了,我猜)如果有的话,你真的应该按折扣价排序。
  • if params[:search]if params[:type_id] 是问题所在,当这些 if 为真时,order 就消失了。
  • 请阅读“How to Ask”及其链接页面。花点时间想一个更好的标题,因为它目前很糟糕。
  • 我们需要更多信息。表的架构是什么,以及证明问题的最小价格样本。编辑您的问题并添加该信息,就好像它一直存在一样。不要使用“编辑”或“更新”类型标签,因为我们可以知道发生了什么变化。 “How to Ask”及其链接页面有助于解释。
  • @SteveTurczyn 我在我的问题中添加了数据库,是的,价格是字符串。如何解决这个问题?

标签: arrays ruby-on-rails ruby database sorting


【解决方案1】:

我查看了您的网站,我认为您的问题是您正在尝试对价格(字符串值)进行排序,这导致了问题。让我试着通过这个例子来解释一下。

arr = ['1', '45', '2', '100', '1001']

如果您尝试按此排序此数组

arr.sort

它会给出这个输出[“1”,“100”,“1001”,“2”,“45”]

但是如果你使用这种排序

arr.sort_by { |num| num.to_i }

它会给你这样的东西 ["1", "2", "45", "100", "1001"]

在这里,根据您的喜好,您可以使用 to_i、to_d 或 to_f 方法

--------- 迁移文件共享后

您需要在迁移/架构中将文本字段更改为十进制。

class ChangeTeethersPriceAndDiscountToDecimal < ActiveRecord::Migration[5.2]
  def change
    change_column :teethers, :price, :decimal
    change_column :teethers, :discount, :decimal
  end
end

之后,您的代码将对它们进行正确排序

【讨论】:

  • 但她不使用 Enumerable#sort。
  • 我在答案中添加了一个迁移文件。你可以使用它
  • 嗨,谢谢你的正确,但史蒂夫在你之前回答了它:)我投了赞成票:)
【解决方案2】:

将价格更改为十进制(或者整数...美分...并使用“金钱”宝石,这是一个不错的宝石!)。

我认为迁移不会让您将字符串更改为十进制,但如果这是一个尚未部署和上线的开发,那应该没关系。

所以,迁移到 remove_column 'value' 和 'discount'

然后迁移到 add_column 值作为小数域,折扣作为小数域。

【讨论】:

  • 非常感谢它解决了一半的问题。 我怎样才能在其中包含折扣钱。这样他们就可以显示折扣价
猜你喜欢
  • 1970-01-01
  • 2018-05-03
  • 1970-01-01
  • 2014-12-08
  • 2016-10-18
  • 1970-01-01
  • 2020-11-09
  • 1970-01-01
相关资源
最近更新 更多