如何使用Rails和Jquery创建可排序的行。
我在表中添加了一个名为position的列,我想要排序行。
我在我的控制器中创建了这个动作:
def sort
params[:faqs].each_with_index do |id, index|
Faq.update_all(['position=?', index+1], ['id=?', id])
end
render :nothing => true
end
并为行动创建了一条路线。但是如何创建具有可排序行的表?而不是可排序的列表:http://railscasts.com/episodes/147-sortable-lists
答案 0 :(得分:0)
您需要在表中添加新的整数数据类型列。 看看下面的jquery插件,用于排序元素。
http://jqueryui.it/demos/sortable
以下是排序元素的示例。
<!DOCTYPE html>
<html>
<head>
<link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" rel="stylesheet" type="text/css"/>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script>
</head>
<body style="font-size:62.5%;">
<ul id="sortable">
<% unless @images.blank? %>
<% @images.each do |image| %>
<li id="<%= image.id %>">Item 1</li>
<% end %>
<% end %>
</ul>
<script type"text/javascript">
$(document).ready(function() {
$('.sortable').sortable({
update: function(event, ui) {
var question_list = $(this).sortable('toArray').toString();
alert(question_list);
//this will give you the new order list from here you can fire ajax call for updating the order
}
});
});
</script>
</body>
</html>
答案 1 :(得分:0)
使用JQuery UI检查此更新的RailsCast for Sortable Lists!
http://railscasts.com/episodes/147-sortable-lists-revised(更新的剧集)