Angular UI Tree Rails
Angular UI Tree is an AngularJS UI component that can sort nested lists, provides drag & drop support and doesn't depend on jQuery. If you are a user who uses angular-nestedSortable, this is How to migrate From v1.x to v2.0.
Features
- Uses the native AngularJS scope for data binding
- Sorted and move items through the entire tree
- Prevent elements from accepting child nodes
Supported browsers
The Angular UI Tree is tested with the following browsers:
- Chrome (stable)
- Firefox
- IE 8, 9 and 10
For IE8 support, make sure you do the following:
- include an ES5 shim
- make your AngularJS application compatible with Internet Explorer
- use jQuery 1.x
Installation
Add this line to your application's Gemfile:
gem 'angular_ui_tree_rails'
And then execute:
$ bundle
Or install it yourself as:
$ gem install angular_ui_tree_rails
Javascript
You will need to add the angular-ui-tree object in your application.js:
//= require angular-ui-tree
Javascript
You will need to add the angular-ui-tree object in your application.scss:
*= require angular-ui-tree
Demo
Watch the Tree component in action on the demo page.
Code
Add the sortable module as a dependency to your application module:
var myAppModule = angular.module('MyApp', ['ui.tree'])
Injecting ui.tree, ui-tree-nodes, ui-tree-node, ui-tree-handle to your html.
HTML View or Templates
<div ui-tree>
<ol ui-tree-nodes="" ng-model="list">
<li ng-repeat="item in list" ui-tree-node>
<div ui-tree-handle>
{{item.title}}
</div>
<ol ui-tree-nodes="" ng-model="item.items">
<li ng-repeat="subItem in item.items" ui-tree-node>
<div ui-tree-handle>
{{subItem.title}}
</div>
</li>
</ol>
</li>
</ol>
</div>
Developing Notes:
- Adding
ui-treeto your root element of the tree. - Adding
ui-tree-nodesto the elements which contain the nodes.ng-modelis required, and it should be an array, so that the directive knows which model to bind and update. - Adding
ui-tree-nodeto your node element, it always follows theng-repeatattribute. - Adding
ui-tree-handleto the element used to drag the object. - All
ui-tree,ui-tree-nodes,ng-model,ui-tree-nodeare necessary. And they can be nested. - If you don't add a
ui-tree-handlefor a node, the entire node can be dragged.
Unlimited nesting HTML View or Templates Example
<!-- Nested node template -->
<script type="text/ng-template" id="nodes_renderer.html">
<div ui-tree-handle>
{{node.title}}
</div>
<ol ui-tree-nodes="" ng-model="node.nodes">
<li ng-repeat="node in node.nodes" ui-tree-node ng-include="'nodes_renderer.html'">
</li>
</ol>
</script>
<div ui-tree>
<ol ui-tree-nodes="" ng-model="data" id="tree-root">
<li ng-repeat="node in data" ui-tree-node ng-include="'nodes_renderer.html'"></li>
</ol>
</div>
Structure of angular-ui-tree
ui-tree --> Root of tree
ui-tree-nodes --> Container of nodes
ui-tree-node --> One of the node of a tree
ui-tree-handle --> Handle
ui-tree-nodes --> Container of child-nodes
ui-tree-node --> Child node
ui-tree-handle --> Handle
ui-tree-node --> Child node
ui-tree-node --> Another node
ui-tree-handle --> Handle
Migrate From v1.x to v2.0
API
ui-tree
ui-tree is the root scope for a tree
Attributes
data-drag-enabled
Turn on dragging and dropping of nodes.
true(default): allow drag and dropfalse: turn off drag and drop
data-max-depth
Number of levels a nodes can be nested (default 0). 0 means no limit.
Note
If you write your own $callbacks.accept method, you have to check data-max-depth by yourself.
data-drag-delay
Number of milliseconds a click must be held to start a drag. (default 0)
data-empty-place-holder-enabled
If a tree is empty, there will be an empty place hoder which is used to drop node from other trees by default.
true(default): display an empty place holder if the tree is emptyfalse: do not display an empty place hoder
Example
- turn on/off drag and drop.
- Limit depth to 5
- 500 milliseconds delay
```html
#### Methods of scope ##### collapseAll() Collapse all it's child nodes. ##### expandAll() Expand all it's child nodes. ##### $callbacks (type: Object) `$callbacks` is a very important property for `angular-ui-tree`. When some special events trigger, the functions in `$callbacks` are called. The callbacks can be passed through the directive. Example: ```js myAppModule.controller('MyController', function($scope) { $scope.treeOptions = { accept: function(sourceNodeScope, destNodesScope, destIndex) { return true; }, }; });<div ui-tree="treeOptions"> <ol ui-tree-nodes ng-model="nodes"> <li ng-repeat="node in nodes" ui-tree-node>{{node.title}}</li> </ol> </div>Methods in $callbacks
accept(sourceNodeScope, destNodesScope, destIndex)
Check if the current dragging node can be dropped in the
ui-tree-nodes.Parameters:
sourceNodeScope: The scope of source node which is dragging.destNodesScope: The scope ofui-tree-nodeswhich you want to drop in.destIndex: The position you want to drop in.
Return If the nodes accept the current dragging node.
trueAllow it to drop.falseNot allow.
beforeDrag(sourceNodeScope)
Check if the current selected node can be dragged.
Parameters:
sourceNodeScope: The scope of source node which is selected.
Return If current node is draggable.
trueAllow it to drag.falseNot allow.
dropped(event)
If a node moves it's position after dropped, the
nodeDroppedcallback will be called.Parameters:
event: Event arguments, it's an object.source: Source objectnodeScope: The scope of source node which was dragged.nodesScope: The scope of the parent nodes of source node when it began to drag.index: The position when it began to drag.dest: Destination objectnodesScope: The scope ofui-tree-nodeswhich you just dropped in.index: The position you dropped in.elements: The dragging relative elements.placeholder: The placeholder element.dragging: The dragging element.pos: Position object.
dragStart(event)
The
dragStartfunction is called when the user starts to drag the node. Parameters: Same as Parameters of dropped.dragMove(event)
The
dragMovefunction is called when the user moves the node.Parameters: Same as Parameters of dropped.
dragStop(event)
The
dragStopfunction is called when the user stop dragging the node.Parameters: Same as Parameters of dropped.
beforeDrop(event)
The
beforeDropfunction is called before the dragging node is dropped.Parameters: Same as Parameters of dropped.
ui-tree-nodes
ui-tree-nodesis the container of nodes. Everyui-tree-nodeshould have aui-tree-nodesas it's container, aui-tree-nodescan have multiple child nodes.Attributes
data-nodrop
Turn off drop of nodes.
data-max-depth
Number of levels a nodes can be nested (default 0). 0 means no limit. It can override the
data-max-depthinui-tree. Note If you write your own $callbacks.accept method, you have to checkdata-nodropanddata-max-depthby yourself.Example: turn off drop.
<ol ui-tree-nodes ng-model="nodes" data-nodrop> <li ng-repeat="node in nodes" ui-tree-node>{{node.title}}</li> </ol>Properties of scope
$element (type: AngularElement)
The html element which bind with the
ui-tree-nodesscope.$modelValue (type: Object)
The data which bind with the scope.
$nodes (type: Array)
All it's child nodes. The type of child node is scope of
ui-tree-node.$nodeScope (type: Scope of ui-tree-node)
The scope of node which current
ui-tree-nodesbelongs to. For example:ui-tree-nodes --> nodes 1 ui-tree-node --> node 1.1 ui-tree-nodes --> nodes 1.1 ui-tree-node --> node 1.1.1 ui-tree-node --> node 1.1.2 ui-tree-node --> node 1.2The property
$nodeScope ofnodes 1.1isnode 1.1. The property$nodesofnodes 1.1is [node 1.1.1,node 1.1.2]maxDepth
Number of levels a node can be nested. It bases on the attribute data-max-depth.
nodrop
Turn off drop on nodes. It bases on the attribute data-nodrag.
Methods of scope
depth()
Get the depth.
outOfDepth(sourceNode)
Check if depth limit has reached
isParent(nodeScope)
Check if the nodes is the parent of the target node. Parameters:
nodeScope: The target node which is used to check with the current nodes.
ui-tree-node
A node of a tree. Every
ui-tree-nodeshould have aui-tree-nodesas it's container.Attributes
data-nodrag
Turn off drag of node. Example: turn off drag.
<ol ui-tree-nodes ng-model="nodes"> <li ng-repeat="node in nodes" ui-tree-node data-nodrag>{{node.title}}</li> </ol>data-collapsed
Collapse the node.
Properties of scope
$element (type: AngularElement)
The html element which bind with the
ui-tree-nodesscope.$modelValue (type: Object)
The data which bind with the scope.
collapsed (type: Bool)
If the node is collapsed
true: Current node is collapsed;false: Current node is expanded.
$parentNodeScope (type: Scope of ui-tree-node)
The scope of parent node.
$childNodesScope (type: Scope of ui-tree-nodes)
The scope of it's
ui-tree-nodes.$parentNodesScope (type: Scope of ui-tree-nodes)
The scope of it's parent
ui-tree-nodes.For example:
ui-tree-nodes --> nodes 1 ui-tree-node --> node 1.1 ui-tree-nodes --> nodes 1.1 ui-tree-node --> node 1.1.1 ui-tree-node --> node 1.1.2 ui-tree-node --> node 1.2node 1.1.1.$parentNodeScopeisnode 1.1.node 1.1.$childNodesScopeisnodes 1.1.node 1.1.$parentNodesScopeisnodes 1.
Methods of scope
collapse()
Collapse current node.
expand()
Expand current node.
toggle()
Toggle current node.
remove()
Remove current node.
depth()
Get the depth of the node.
maxSubDepth()
Get the max depth of all the child nodes. If there is no child nodes, return 0.
isSibling(targetNodeScope)
Check if the current node is sibling with the target node. Parameters:
targetNodeScope: The target node which is used to check with the current node.
isChild(targetNodeScope)
Check if the current node is a child of the target node. Parameters:
targetNodeScope: The target node which is used to check with the current node.
ui-tree-handle
Use the
ui-tree-handleto specify an element used to drag the object. If you don't add aui-tree-handlefor a node, the entire node can be dragged.NgModules Link
Thanks to the makers of Angular-UI-Tree!(I did not create the angular code, just the gem.)
Contributing
- Fork it ( https://github.com/[my-github-username]/angular_ui_tree_rails/fork )
- Create your feature branch (
git checkout -b my-new-feature) - Commit your changes (
git commit -am 'Add some feature') - Push to the branch (
git push origin my-new-feature) - Create a new Pull Request