-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
76 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -1,5 +1,30 @@ | |||
class HomeController < ApplicationController | class HomeController < ApplicationController | ||
|
|||
def index | def index | ||
|
|||
end | |||
|
|||
def savesort | |||
neworder = JSON.parse(params[:set]) | |||
prev_item = nil | |||
neworder.each do |item| | |||
dbitem = Category.find(item['id']) | |||
prev_item.nil? ? dbitem.move_to_root : dbitem.move_to_right_of(prev_item) | |||
sort_children(item, dbitem) unless item['children'].nil? | |||
prev_item = dbitem | |||
end | |||
Category.rebuild! | |||
render :nothing => true | |||
end | |||
|
|||
def sort_children(element,dbitem) | |||
prevchild = nil | |||
element['children'].each do |child| | |||
childitem = Category.find(child['id']) | |||
prevchild.nil? ? childitem.move_to_child_of(dbitem) : childitem.move_to_right_of(prevchild) | |||
sort_children(child, childitem) unless child['children'].nil? | |||
prevchild = childitem | |||
end | |||
end | end | ||
|
|
||
end | end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -1,3 +1,43 @@ | |||
<% content_for :head do -%> | <% content_for :head do -%> | ||
<%= javascript_include_tag 'nestedsortables.min' %> | <%= javascript_include_tag 'nestedsortables.min' %> | ||
<style type="text/css" media="screen"> | |||
#sortable{width:300px;} | |||
ul{margin-bottom:0px;} | |||
.highlight{background-color:#d6d88e;} | |||
</style> | |||
<script type="text/javascript" charset="utf-8"> | |||
$(function() { | |||
$("#sortable").nestedSortable({ | |||
listType: 'ul', | |||
items: 'li', | |||
placeholder: "highlight", | |||
forcePlaceholderSize: true, | |||
handle: 'span', | |||
helper: 'clone', | |||
opacity: .6, | |||
revert: 250, | |||
tabSize: 25, | |||
tolerance: 'pointer', | |||
toleranceElement: '> span' | |||
}); | |||
$("#sortable").disableSelection(); // make links not clickable | |||
$('#serialize').click(function (){ | |||
var c = {set : JSON.stringify($('#sortable').nestedSortable('toHierarchy', {startDepthCount: 0}))}; | |||
$.post("savesort", c, $('#output').html('<p id="flash_notice">Saved Successfully</p>')); | |||
return false; | |||
}); | |||
}); | |||
</script> | |||
<% end -%> | <% end -%> | ||
|
|||
<h1>Category Sorting Example</h1> | |||
<p>Drag and drop the categories into a nested set then hit save</p> | |||
<h3><a href='#' id='serialize'>Save Sorted Elements</a></h3> | |||
<ul id='sortable'> | |||
<% for cat in Category.roots %> | |||
<li id='category_<%= cat.id %>'> | |||
<span><%= cat.name %></span> | |||
<%= render 'shared/children', :item => cat unless cat.leaf? %> | |||
</li> | |||
<% end -%> | |||
</ul> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,8 @@ | |||
<ul> | |||
<% item.children.each do |child| %> | |||
<li id='category_<%=child.id %>'> | |||
<span><%= child.name %></span> | |||
<%= render 'shared/children', :item => child unless child.leaf? %> | |||
</li> | |||
<% end -%> | |||
</ul> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -1,3 +1,6 @@ | |||
body { | |||
color:#444; | |||
} | |||
a img { | a img { | ||
border: none; | border: none; | ||
} | } | ||
|