当我调用 $.getJSON 时,我想重新加载 tbody 元素。换句话说,我需要删除所有元素并通过 Json 响应放置新元素。
JS
$(document).ready(function(){
$('#position').sortable({
update: function(event, ui) {
var newOrder = $(this).sortable('toArray').toString();
$.getJSON('/save_positions.json', {ids:newOrder}, function(data){
??????
});
}
});
});
我的观点
# index.html.erb
<tbody id="position">
<%= render :partial => "activities"%>
</tbody>
局部视图
#_activities.html.erb
<% @activities.each do |activity| %>
<tr id='<%= activity.id %>'>
<td><%= activity.position %></td>
<td><%= link_to activity.id, activity_path(activity) %></td>
<td><%= activity.name %></td>
<td><%= activity.estimated %></td>
<td><%= activity.used %></td>
<td><%=l activity.created_at %></td>
</tr>
我的动作(应用程序控制器)
def save_positions
@activities = Activity.all(:order => 'position')
respond_to do |format|
format.json { render :json => @activities }
end
end