我正在使用Ruby on Rails创建一个待办事项列表应用程序,我想知道如何通过我的标题对表格中的信息进行排序?就像我希望能够点击“标题” “客户端”或“完成”一样,它会将它们排序为ASC或DESC。如果可能的话,还可以让他们恢复原来的顺序。 Ť
以下是我正在尝试的代码:
projects_controller.rb:
class ProjectsController < ApplicationController
before_action :set_project, only: [:show, :edit, :update, :destroy]
helper_method :sort_column, :sort_direction
def index
@projects = Project.order(sort_column + " " + sort_direction)
end
private
def sort_column
Project.column_names.include?(params[:sort]) ? params[:sort] : "title"
end
def sort_direction
%w[asc desc].include?(params[:direction]) ? params[:direction] : "asc"
end
# GET /projects
# GET /projects.json
def index
@projects = Project.all
end
# GET /projects/1
# GET /projects/1.json
def show
end
# GET /projects/new
def new
@project = Project.new
end
# GET /projects/1/edit
def edit
end
# POST /projects
# POST /projects.json
def create
@project = Project.new(project_params)
respond_to do |format|
if @project.save
format.html { redirect_to @project, notice: 'Project was successfully created.' }
format.json { render :show, status: :created, location: @project }
else
format.html { render :new }
format.json { render json: @project.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /projects/1
# PATCH/PUT /projects/1.json
def update
respond_to do |format|
if @project.update(project_params)
format.html { redirect_to @project, notice: 'Project was successfully updated.' }
format.json { render :show, status: :ok, location: @project }
else
format.html { render :edit }
format.json { render json: @project.errors, status: :unprocessable_entity }
end
end
end
# DELETE /projects/1
# DELETE /projects/1.json
def destroy
@project.destroy
respond_to do |format|
format.html { redirect_to projects_url, notice: 'Project was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_project
@project = Project.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def project_params
params.require(:project).permit(:title, :description, :hours, :payrate, :done, :client)
end
end
application_helper.rb:
module ApplicationHelper
def sortable(column, title = nil)
title ||= column.titleize
css_class = column == sort_column ? "current #{sort_direction}" : nil
direction = column == sort_column && sort_direction == "asc" ? "desc" : "asc"
link_to title, {:sort => column, :direction => direction}, {:class => css_class}
end
end
index.html.erb:
<h1 id="title">Project List</h1>
<table>
<thead>
<tr id="headers">
<th><%= sortable "title" %></th>
<th><%= sortable "client" %></th>
<th>Description</th>
<th>Hours</th>
<th><%= sortable "done" %></th>
<th colspan="3"></th>
</tr>
</thead>
<tbody class="col-md-2" id="listItems">
<% @projects.each do |project| %>
<tr id="table">
<td><%= project.title %></td>
<td><%= project.client %></td>
<td ><%= project.description %></td>
<td><%= project.hours %></td>
<td><%= check_box_tag "project_#{project.id}", "#{project.done}", project.done?, disabled: "#{project.done?}" %>
</td>
<td>
<span title="Show">
<%= link_to " #{image_tag('show.png')}".html_safe, project, id:'showButton' %>
</span>
</td>
<td>
<span title="Edit">
<%= link_to " #{image_tag('edit.png')}".html_safe, edit_project_path(project), id:'editButton' %>
</span>
</td>
<td>
<span title="Delete">
<%= link_to " #{image_tag('destroy.png')}".html_safe, project, id:'destroyButton', method: :delete, data: { confirm: 'Are you sure?' } %>
</span>
</td>
</tr>
<% end %>
</tbody>
</table>
<br>
<%= link_to 'New Project', new_project_path, id:"new" %>
答案 0 :(得分:4)
有一个非常好的教程:
http://railscasts.com/episodes/228-sortable-table-columns
您应首先尝试本教程,如果您仍有疑问,请再次询问......
我希望它有所帮助。
答案 1 :(得分:1)
简单的方法是使用jQuery插件:DataTables。