[英]How to sort table columns in ascending or descending order by clicking the header
我试图通过升价和降序对价格标题进行排序。 当我点击价格时,所有 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
我查看了您的网站,我认为您的问题是您正在尝试对价格(字符串值)进行排序,这导致了问题。 让我试着通过这个例子来解释它。
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
之后,您的代码将对它们进行正确排序
将价格更改为十进制(或者可能是整数......美分......并使用“金钱”宝石,这是一个不错的宝石!)。
我认为迁移不会让您将字符串更改为十进制,但如果这是尚未部署和上线的开发,那应该无关紧要。
因此,迁移到 remove_column 'value' 和 'discount'
然后将 add_column 值迁移为小数字段,并将折扣作为小数字段。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.