2011-10-10 32 views
1

如何使用Rails和Jquery創建可排序的行。Rails如何創建可排序的行?

我已經添加了一個名爲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

您需要在表中添加新的整數數據類型cloumn。 看看下面的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>