Git Product home page Git Product logo

sortable_tree_rails's Introduction

SortableTreeRails

GUI for sortable tree to manage data organized in tree with ancestry gem.

Works with Rails 5, Rails 4.

Overview

The gem uses:

Demo

See demo app in the repository (spec/dummy). Run page with the tree: http://localhost:3000/categories/manage.

Usage

Gemfile

gem 'haml-rails'

gem 'jquery-rails'
gem "jquery-ui-rails"

gem 'ancestry'
gem 'sortable_tree_rails'
  • the gem uses HAML

routes

# config/routes.rb

resources :categories do
  collection do
     post :sort
  end
end

This page (sort_categories_path) will be used by the gem to update data after drag&drop.

model

It assumes that your model has already fields in DB for ancestry.

# app/models/category.rb

class Category < ActiveRecord::Base

  has_ancestry

  # it uses column ancestry_depth
  # has_ancestry :cache_depth=>true

end

Javascript

Include js files in your assets file application.js:

//= require jquery2
//= require jquery-ui

//=require jquery.mjs.nestedSortable.js
//=require sortable_tree.js


it depends on jquery and jquery-ui which come from gems 'jquery-rails', 'jquery-ui-rails'. Change application.js to include jquery and jquery-ui (jquery-ui/sortable) if you are not using these gems.

CSS

Add CSS file to your styles.

for SCSS (app/assets/application.scss):

... your css here ..

@import "sortable_tree";


controller

class CategoriesController < ApplicationController
  include SortableTreeController::Sort
  sortable_tree 'Category', {parent_method: 'parent'}

  def manage
    # get items to show in tree
    @items = Category.all.arrange(:order => :pos)

  end

end

view

# app/views/categories/manage.html.haml

= render_sortable_tree(@items, {name_method: :name, sort_url: sort_categories_url, max_levels: 5, controls_partial: 'controls'})

# app/views/categories/_controls.html.haml

= link_to 'Edit', edit_category_url(item)
= link_to 'Delete', category_url(item), :method => :delete, :data => { :confirm => 'Are you sure?' }

Customize

Options

Options for controller

in controller:


  include SortableTreeController::Sort
  sortable_tree 'ClassName', {_options_here_}

  • ClassName - class name (camel case). For example, 'Category'.
  • :sorting_attribute - attribute used for sorting. Set to nil to disable sorting.
  • :parent_method - method used to access parent for the item. Set to nil to disable tree (not updating parent).

Examples.

  • Example. For model with ancestry. If you use ancestry in model - set :parent_method to 'parent'.
  include SortableTreeController::Sort
  sortable_tree 'ClassName', {parent_method: 'parent', sorting_attribute: 'pos'}
  • Example. Do only sorting, without tree.
  include SortableTreeController::Sort
  sortable_tree 'ClassName', {sorting_attribute: 'pos', parent_method: nil}
  • Example. No sorting, update only parent.
  include SortableTreeController::Sort
  sortable_tree 'ClassName', {sorting_attribute: nil, parent_method: 'parent'}

Options for view

= render_sortable_tree(@items, {__options_here})
  • :name_method - defined which model method (usually, a column name) will be used to show name (default: :name)

  • :sort_url - URL used to update data after item is moved to a new position

  • :max_levels - max levels to show in tree (default: 5)

  • :controls_partial - specify what partial view to use to show control links for each item in a tree. Set to nil to not show controls.

  • example.

= render_sortable_tree(@items, {name_method: :name, sort_url: sort_categories_url, max_levels: 5})
  • example of partial view with controls.

  • main view:

= render_sortable_tree(@items, {name_method: :name, sort_url: sort_categories_url, max_levels: 5, controls_partial: 'controls'})
  • partial with controls. Use local variable item in the partial view.

_controls.html.haml:

= link_to 'Edit', edit_category_url(item)
= link_to 'Delete', category_url(item), :method => :delete, :data => { :confirm => 'Are you sure?' }

Customize

customize view

  • edit file 'views/sortable/_sortable.html.haml' to access the whole layout

How it works

read Wiki

Similar gems

GUI for sortable tree with awesome_nested_set gem:

Credits

sortable_tree_rails's People

Contributors

maxivak avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

sortable_tree_rails's Issues

JS: ReferenceError: $ is not defined

In my default Rails setup, scripts are loaded after the whole page is rendered. This speeds up page load, and is in line with progressive enhancement.

This gem puts javascript in the web body of the page, assuming that jQuery was loaded. This is not the case when scripts are loaded last.
I do not see any substitution taking place in the Javascript, so I suggest to put it in a separate file, so that it can be picked up by the asset pipeline and gets injected in the right place.

Not working with rails 5

I was happy to see an earlier comment that this works with rails 5.

My Gemfile looks like this:
gem 'sortable_tree_rails', github: 'maxivak/sortable_tree_rails'

The rendering aspects work, but when I try to save to DB, I get

`
Started POST "/categories/sort" for 127.0.0.1 at 2017-05-01 11:27:43 -0700
Processing by CategoriesController#sort as /
Parameters: {"cat"=>{"1"=>"null", "2"=>"1", "4"=>"1", "3"=>"null"}}
Completed 500 Internal Server Error in 4ms (ActiveRecord: 0.0ms)

NoMethodError (undefined method `inject' for #ActionController::Parameters:0x007fe339e86ce0
Did you mean? inspect):

/Users/oystersauce8/.rvm/gems/ruby-2.3.0/bundler/gems/sortable_tree_rails-4666d477c4cc/app/controllers/sortable_tree_controller.rb:22:in block in sortable_tree'

From what I read on the interwebs (arsduo/koala#546), the inject() method is removed from rails 5.1 onwards, but this gem tries to use it.

unable to use view helper as specified in README

Showing /home/jake/RubymineProjects/correct-ir/app/views/sections/show.html.erb where line #18 raised:

Missing partial sortable_tree/_sortable_tree with {:locale=>[:en], :formats=>[:html], :variants=>[], :handlers=>[:raw, :erb, :html, :builder, :ruby, :coffee, :jbuilder]}. Searched in:
  * "/home/jake/RubymineProjects/correct-ir/app/views"
  * "/home/jake/.rvm/gems/ruby-2.3.1/bundler/gems/sortable_tree_rails-46b4edcda1b9/app/views"
  * "/home/jake/.rvm/gems/ruby-2.3.1/gems/devise-4.2.0/app/views"
Extracted source (around line #18):
16
17
18
19
20
21

</p>

<%= render_sortable_tree(@items, { name_method: :name, sort_url: sort_sections_url, controls_partial: 'controls' }) %>

<%= link_to 'Edit', edit_section_path(@section) %> |
<%= link_to 'Back', sections_path %>

Error "no implicit conversion of nil into String" without controls

When the controller option controls_partial is set to nil, rendering gives the error no implicit conversion of nil into String in SortableTreeHelper#sortable_tree_render_nested_groups.

This happens because sortable_tree_build_actions(item, opts) returns nil. My suggestion would be to either use string substitution or Ruby methods to build the HTML:

s = content_tag(:div, {class: 'item'}) do
  content_tag(:h3, title, {class: "cell left"}) \
  + content_tag(:div, sortable_tree_build_actions(item, opts), {class: "cell right controls"})
end

Make sorting optional

Ancestry does not do sorting by default, and this gem currently requires the presence of a sorting attribute.
Could the sorting become optional (e.g. by setting sorting_attribute to nil)? Then only the hierarchy would be updated but not the pos attribute (or whetever is configured).

Rails 5?

Any chance this could work in rails 5? I notice the gemspec specifies one rails version in particular.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.