Skip to content

Commit 30ebc1a

Browse files
committed
suite
1 parent 263a554 commit 30ebc1a

File tree

2 files changed

+72
-8
lines changed

2 files changed

+72
-8
lines changed

app/views/rails_admin/main/sortable.html.erb

Lines changed: 48 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
1-
<legend><%= t 'title_section' %></legend>
1+
<div class="alert-status"></div>
2+
3+
<em>
4+
<%= t 'helper_soratble' %>
5+
</em>
6+
7+
<br />
8+
<br />
29

10+
<legend><%= t 'title_section' %></legend>
311
<ul id="sortable">
4-
<% @abstract_model.all.each do |o| %>
5-
<li class="sortable-container">
12+
<% @objects.each do |o| %>
13+
<li data-id="<%= o.id %>" data-position="" class="sortable-container">
614
<span class="ref_number">
715
<% unless @sortable_section.position_method.nil? %>
816
<%= o.send(@sortable_section.position_method) %>
@@ -21,12 +29,45 @@
2129

2230
<script type="text/javascript" charset="utf-8">
2331
$(function () {
32+
handleBaseNumberJs();
33+
2434
$("#sortable").sortable({
25-
placeholder: "ui-state-highlight"
35+
axis: 'y',
36+
placeholder: "ui-state-highlight",
37+
update: function () {
38+
$.when(handleBaseNumberJs()).done(function () {
39+
$('.btn-validate').attr( "disabled", "disabled" );
40+
41+
var ordered = [];
42+
$('.sortable-container').each(function (i, elem) {
43+
ordered.push({id: $(this).attr('data-id'), position: $(this).attr('data-position')});
44+
});
45+
46+
$.ajax({
47+
type: "POST",
48+
url: '<%= sortable_path(:model_name => @abstract_model.to_param) %>',
49+
data: { 'data': ordered },
50+
success: function (data) {
51+
$('.btn-validate').removeAttr("disabled");
52+
$('.alert-status').html('<div class="alert alert-dismissible alert-success"> <button class="close" data-dismiss="alert" type="button">×</button>Modification éffectuée avec succès</div>').fadeIn().delay(3000).fadeOut();
53+
},
54+
error: function (data) {
55+
$('.alert-status').html('<div class="alert alert-dismissible alert-danger"> <button class="close" data-dismiss="alert" type="button">×</button>Erreur lors de la modification</div>').fadeIn().delay(3000).fadeOut();
56+
}
57+
});
58+
});
59+
}
2660
});
61+
2762
$("#sortable").disableSelection();
28-
$('.ref_number').each(function (i, elem) {
29-
$(this).html(i + 1);
30-
})
63+
64+
function handleBaseNumberJs() {
65+
$('.ref_number').each(function (i, elem) {
66+
$(elem).html(i + 1);
67+
});
68+
$('.sortable-container').each(function (i, elem) {
69+
$(elem).attr('data-position', i + 1)
70+
});
71+
}
3172
});
3273
</script>

lib/rails_admin_sortable.rb

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,29 @@ class Sortable < RailsAdmin::Config::Actions::Base
1515

1616
register_instance_option :controller do
1717
Proc.new do
18-
@sortable_section = model_config.sortable
18+
@sortable_section = model_config.sortable
19+
@objects = list_entries.sort { |a,b| a.send(@sortable_section.position_method) <=> b.send(@sortable_section.position_method) }
20+
21+
if request.post?
22+
original_model = @abstract_model.model
23+
24+
params[:data].each do |key,value|
25+
object = original_model.find(value[:id])
26+
object.send("#{@sortable_section.position_method}=", value[:position])
27+
object.save!
28+
end
29+
30+
respond_to do |format|
31+
format.json { render :json => { status: 'OK' } }
32+
end
33+
end
1934
end
2035
end
2136

37+
register_instance_option :visible? do
38+
!bindings[:abstract_model].config.sortable.position_method.nil?
39+
end
40+
2241
register_instance_option :object_level do
2342
false
2443
end
@@ -39,6 +58,10 @@ class Sortable < RailsAdmin::Config::Actions::Base
3958
false
4059
end
4160

61+
register_instance_option :http_methods do
62+
[:get, :post, :put]
63+
end
64+
4265
end
4366
end
4467
end

0 commit comments

Comments
 (0)