簡體   English   中英

如何通過單擊標題按升序或降序對表格列進行排序

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM