Giter Club home page Giter Club logo

angular-treecursive's Introduction

Angular-Treecursive

Customizable recursive tree for angular.


This directive will help you to display a nested tree in your Angular project, and customize the nodes by using your own template or HTML code, without nasty configuration options. There is no limit for your trees, as long as you respect a similar JSON structure:
$scope.myTree = [
    {
        name: 'John'
    },
    {
        name: 'Marie'
    },
    {
        name: 'Jackie',
        children: [
            {
                name: 'Junior'
            },
            {
                name: 'Christie',
                children: [...]
            }
        ]
}];

Please checkout this demo

Installation

You can install it via bower by doing bower install angular-treecursive. Then, load the script file as you usually do:

<script src="bower_components/angular-treecursive/angular-treecursive.min.js"></script>

Lastly, include the module webpolis.directives in your app, like this:

var myApp = angular.module('myApp', ['webpolis.directives']);

Usage

There are no specific properties required for your nodes, other than the children property which is only needed if you want to include more nodes and the collapsed boolean attribute, in case you need to expand or collapse the children nodes. Then you can traverse into infinite levels!
So, for example:

<treecursive nodes="myTree">
    <a href="" ng-click="myCustomSelectionMethod(node)">{{node.name}}</a>
</treecursive>

As you can see, the members of your tree are represented by node, so you can do whatever you want there, like setting a collapsed property in your nodes to open/close a folder icon (See demo).

If you have a different name for the property that holds the children, you can specify that also.
For example, if the property name that holds the children is named 'books':

<treecursive nodes="myTree" children="books">
    <a href="" ng-click="myCustomSelectionMethod(node)">{{node.name}}</a>
</treecursive>

Lazy load

If you are working with very large data structures, you may want to initially collapse all nodes (by setting the collapsed attribute to false on each one), then render the tree lazily. This is done by specifying the lazy-render attribute as true as in the following example:

<treecursive nodes="myVeryLargeTree" lazy-render="true">
    <a href="" ng-click="myCustomSelectionMethod(node)">{{node.name}}</a>
</treecursive>

Essentially this will tell the directive to use ng-if for hiding collapsed branches instead of ng-show. The subtle difference is that ng-show will always render the HTML but may choose to hide it using CSS, where as ng-if postpones the rendering of the HTML until it's visible (and removes it when it is no longer visible). For large trees which are initially collapsed, the lazy-render option will greatly speed up processing.

Configuration

Configuration is simple. We just need one attribute: nodes (required), specifying the name of the array for your tree.

angular-treecursive's People

Contributors

flakron avatar mvindahl avatar webpolis avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

angular-treecursive's Issues

Allow user rename 'children' property

Will be great to enable the user customize the name of the property that holds the node's childrens. So, instead of forcing the user set a children array, this enhancement doesn't force users to modify their current setup (API, etc).

Please release bower version with support for children attr

Hi,

We've decided to depend upon your tree control for our project. It's simple, it's flexible, it's angular style, and it does exactly what it says on the tin, no bloat attached :)

However, due to the structure of our data, we need to make use of the "children" attribute. This is on the master but it's currently not included in the bower release. Our current workaround is to link directly to a github project SHA ("angular-treecursive": "https://github.com/webpolis/angular-treecursive.git#21186ec728847b6a8385d9330295e938d01909ef" in bower.json) but we'd prefer to be able to refer to the dependency in a more standard bower manner.

Thanks in advance,
/Martin

Mouse events fire twice

I've only tested the following with ng-click but I suspect that the bug would extend to wide range of event types. In any case, if I define an event listener upon a child element of the treecursive directive, the listener will fire twice. The only exception is for ng-click on the topmost element, due to a patch in the code (angular.element(clone[1]).attr('ng-click', false); in the clone function).

The problem seems to be that the elements are compiled twice; both when they are created and when a treecursive element has been added to expand to a child list. As a result, event listeners are registerd twice.

As far as I can tell, it's just a matter of compiling the nested treecursive element without recompiling the stuff which was already compiled. I.e. replace
$compile($element.contents())($scope);
by
$compile(sub[0])($scope);
and everything appears to work. The ng-click patch can then be removed.

If you'd like a PR then I can create easily create one, but it's just a couple of lines.

Poor performance for large data structures

Hi,

In our project, we are occasionally faced with having to render trees based upon very large data structures. It's an implicit requirement that these do not drag down the UI. I've experimented a bit with treecursive and large generated data sets, and above a couple thousand items the performance begins to degrade noticely (on a pretty fast MacBook). Above about 10K items it seems to kill the browser.

I have a fix, though. Replacing the ng-show="!node.collapsed" in the code by a ng-if="!node.collapsed" will cause the tree to build itself lazily instead of building itself initially and then hiding collapsed branches. This will only work if nodes are initially collapsed, i.e. for an initially expanded tree it would not be expected to make any positive difference.

I've forked the repository and expect to make the change in a non-obtrusive manner, i.e. it will use ng-show as default and ng-if if you actively specify so in an attribute. Having done so, I'd be happy to make a pull request, if you are interested.

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.