JquerySortableTree
This gem is totally based on the_sortable_tree gem.
tested with rails 4.1.0rc2 + haml 4
Nested Set + Drag&Drop GUI. Very fast! Best render helper! 2000 nodes/sec. Ready for rails 4 (RubyGems)
Dummy Application
Sortable tree. Drag&Drop GUI
Render tree
Render Nested Options
Render Indented Options
Render Optgroup
Expandable tree
Keywords
Awesome nested set, Nested set, Ruby, Rails, Nested set view helper, Sortable nested set, Drag&Drop GUI for nested set, View helper for nested set, render tree
Install
Gemfile (Rails 3, Rails 4)
gem 'awesome_nested_set' # or any similar gem (gem 'nested_set')
gem 'jquery_sortable_tree', '~> 3.0.0'
Console
bundle
Using
JQuery and JavaScripts
app/assets/javascripts/application.js
Sortable GUI require JQuery libs
//= require jquery
//= require jquery-ui
//= require jquery_ujs
Add next JS only for Sortable GUI
//= require sortable_tree/initializer
or Extendable GUI:
//= require expandable_tree/initializer
Stylesheets
app/assets/stylesheets/application.css
*= require tree
*= require sortable_tree
*= require expandable_tree
*= require nested_options
Extend your Routes for Sortable GUI
resources :pages do
collection do
get :manage
# required for Sortable GUI server side actions
post :rebuild
end
end
manage - just page, where you want render Sortable tree.
Extend your Model
class Page < ActiveRecord::Base
include JquerySortableTree::Scopes
# any code here
end
Extend your controller and find your tree
class PagesController < ApplicationController
include JquerySortableTreeController::Rebuild
def manage
@pages = Page.nested_set.select('id, title, content, parent_id').all
end
# any code here
end
Basic Render Method
build_server_tree(tree, options)
Render Tree
app/views/pages/manage.html.haml
%ol.tree= just_tree @pages
just_tree is just alias of build_server_tree(tree, type: :tree)
Render Sortable Tree
%ol.sortable_tree{ data: { max_levels: 5, rebuild_url: rebuild_pages_url } }
= sortable_tree @pages
sortable_tree is just alias of build_server_tree(tree, type: :sortable)
Render Nested Options Tree
= select_tag :page_id, nested_options(@pages, :selected => Page.last), class: :nested_options
nested_options is just alias of build_server_tree(tree, type: :nested_options)
This uses CSS styling, for indenting with spaces, use indented_options instead.
Render Indented Options Tree
= select_tag :page_id, indented_options(@pages, :selected => Page.last)
indented_options is just alias of build_server_tree(tree, type: :indented_options)
Render Optgroup Tree
= select_tag :page_id, optgroup(@pages, :selected => Page.last)
optgroup is just alias of build_server_tree(tree, type: :indented_options) The only difference between indented_options and optgroup is the selectable of parent nodes. Indented options allows to select parent nodes, but optgroup does not.
build_server_tree options
Client side:
Required params for sortable GUI! Must be defined at root element of tree.
- max_levels - maximum depth of nesting
- rebuild_url - URL to rebuild method on server side
%ol.sortable_tree{ data: { max_levels: 3, rebuild_url: rebuild_pages_url } }
Server side:
define
build_server_tree(pages, {:option_1 => :value_1, :option_2 => :value_2})
use
options[:NAME]
Options list:
- id - id field of node
- title - title field of node
- type - type of tree [tree|sortable|nested_options|indented_options]
- namespace - for example: :admin. [] - by default
- render_module - your own Render Tree Helper
- spacing - number of spaces per level (indented_options only)
Rendering runtime params (see code of render helpers):
You can use next options, when rendering run:
- level - level number
- root - root flag [true|false]
- klass - class name
- has_children - has children flag [true|false]
- children - array of children
Simple_form input
Using indented options with simple_form is easy:
= simple_form_for @website do |form|
= form.association :front_page, as: :indented_collection_select
Additional options can be supplied to the input:
- spacing - number of spaces to use for each level (default 3)
-
depth_method - method name to get level from object (default
:depth
)
Please note that when you're using awesome_nested_set, you need to have the
optional depth
column in your table.
Customization
Try to run next view generators:
Render helpers for HTML tree generation
bundle exec rails g jquery_sortable_tree:views tree
bundle exec rails g jquery_sortable_tree:views sortable
bundle exec rails g jquery_sortable_tree:views nested_options
bundle exec rails g jquery_sortable_tree:views indented_options
Base Render helper of gem
bundle exec rails g jquery_sortable_tree:views helper
Assets of gem
bundle exec rails g jquery_sortable_tree:views assets
I want to know more
- How to change HTML code of tree?
- How to create new tree HTML Builder helper?
- I need to render reversed tree
- Gem can't correctly define a Name of your Model
- ChangeLog
Is it fast? Yes, it is!
BANCHMARK:
tree params: 16.000 nodes, 3 levels
- Views: 7999.6ms | ActiveRecord: 79.2ms
- WebInspector full time ~ 9.64s
total: ~ 2000 nodes/sec
Looking for maintainers
Do you want to be open source contributor? There are some ideas:
Try to create view helpers for:
- Mongoid NestedSet
- acts_as_ordered_tree
- Expand tree via AJAX
- Comments Tree gem
- gem Ancestry (???)
I want to try! I need tests!
- Sorry, but I have not tests for this gem. Gem is so simple. It's easy to test only with test app.
- You can write some tests, if you need. I will be happy certainly.
- No! I know RSpec. I can write tests. But I have not reasons to write tests here.
Аcknowledgment
The MIT License (MIT)
Copyright 2009-2013 Ilya N. Zykin (the-teacher), Mikhail Dieterle (Mik-die), Matthew Clark
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION W