[英]How to sort columns of a table in a Ruby on Rails Application
我正在为一个学校项目构建一个“待办事项列表” Web应用程序,并且试图使这些列的标题可单击,并且当您单击它们时,它们将对信息进行排序。 我在这里和Google教程上尝试了很多解决方案,也许有人可以直接帮助我?
这是我正在尝试的:
projects_controller.rb
def index
@projects = Project.all
@products = Project.order(params[:sort])
end
projects.js
$(document).ready(function(){
$('/index.html.erb').DataTable();
});
index.html.erb
<thead>
<tr id="headers">
<th><%= link_to "Title", :sort => "title" %></th>
<th><%= link_to "Client", :sort => "client" %></th>
<th>Description</th>
<th>Hours</th>
<th><%= link_to "Done", :sort => "done" %></th>
<th colspan="3"></th>
</tr>
</thead>
好的,因为它是用于学校的,所以让我指导您进行一些调试。
首先在您的应用程序下有一个日志文件夹。 运行rails时,将填充开发日志。
查看单击“标题”链接时发生的情况。 您希望看到一个将参数排序设置为标题的GET。
那是进入服务器的物理请求。 通过上面的内容,这可能是完全可以的。
现在,我们要看看控制器是否正确,下一步是否正确。
因此,在不引入调试器的情况下执行此操作的简单方法是使用抬高。
更改控制器以提高Project.order(params [:sort])。to_sql
这应该会给您一个不错的错误消息,再次确认您做得正确。
最后我们要看一下视图
您只共享表的头部,因此我对其余代码的外观进行了以下假设。
<table id="someid">
<thead>
<tr id="headers">
<th><%= link_to "Title", :sort => "title" %></th>
<th><%= link_to "Client", :sort => "client" %></th>
<th>Description</th>
<th>Hours</th>
<th><%= link_to "Done", :sort => "done" %></th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% @products.each do |product| %>
... COLUMNS setout here ....
<% end %>
</tbody>
</table>
我的直觉是您正在渲染@projects,而不是@products,而@products是正在排序的人。
最后,您的jquery出现错误...
$(document).ready(function(){
$('/index.html.erb').DataTable();
});
应该是
$(document).ready(function(){
$('#someid').DataTable(); //uses a selector such as the elements id or the class etc
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.