[英]How to sort table columns in ascending or descending order by clicking the header
I have a store with items .我有一家有商品的商店。 Each item has a price.
每个项目都有一个价格。
I tried to sort them by ascending prices and descending by pricing the price header.我试图通过升价和降序对价格标题进行排序。 When I click price all the items between $10 - $50 sorted correctly but anything less than $10 and above $50 doesn't sort with them.
当我点击价格时,所有 10 美元到 50 美元之间的商品都会正确排序,但任何低于 10 美元和 50 美元以上的商品都不会与它们一起排序。
Here is my code:这是我的代码:
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
My application helper is:我的应用程序助手是:
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
This is in my index:这是在我的索引中:
<h3 id="sort"><label>Sort: </label><span> | </span><span><%= sortable "price" %><span class="caret"></span></span></h3>
In database在数据库中
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
I checked out your website and I think your problem is that you are trying to sort price(string values) and this is causing problem.我查看了您的网站,我认为您的问题是您正在尝试对价格(字符串值)进行排序,这导致了问题。 Let me try to explain it via this example.
让我试着通过这个例子来解释它。
arr = ['1', '45', '2', '100', '1001']
If you try to sort this array by this如果您尝试按此对此数组进行排序
arr.sort
it will give out this output ["1", "100", "1001", "2", "45"]它会给出这个输出 ["1", "100", "1001", "2", "45"]
But if you use this kind of sort但是如果你使用这种类型
arr.sort_by { |num| num.to_i }
it will give you something like this ["1", "2", "45", "100", "1001"]它会给你这样的东西 ["1", "2", "45", "100", "1001"]
In here depending on your preference you can use to_i, to_d or to_f methods在这里,根据您的喜好,您可以使用 to_i、to_d 或 to_f 方法
--------- After migration file is shared --------- 迁移文件共享后
You need to change text fields to decimal in your migration/schema.您需要在迁移/架构中将文本字段更改为十进制。
class ChangeTeethersPriceAndDiscountToDecimal < ActiveRecord::Migration[5.2]
def change
change_column :teethers, :price, :decimal
change_column :teethers, :discount, :decimal
end
end
After that your code is going to sort them correctly之后,您的代码将对它们进行正确排序
Change the price to decimal (or maybe integer... cents... and use "money" gem, it's a nice gem!).将价格更改为十进制(或者可能是整数......美分......并使用“金钱”宝石,这是一个不错的宝石!)。
I don't think a migration will let you change string to decimal but if this is a development that's not yet deployed and live, that shouldn't matter.我认为迁移不会让您将字符串更改为十进制,但如果这是尚未部署和上线的开发,那应该无关紧要。
So, make a migration to remove_column 'value' and 'discount'因此,迁移到 remove_column 'value' 和 'discount'
Then make a migration to add_column value as a decimal field, and discount as a decimal field.然后将 add_column 值迁移为小数字段,并将折扣作为小数字段。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.