Monday, February 24, 2014

GitHub repository for treeview directive.

This has been pending from a long time. I was putting off making excuses to clean up source code, structure it up, or thinking let me do it after exams. But then realized its not going to happen until its in some place. So finally its in github!!! Here's link.

https://github.com/kavithakgowd/AngularTreeview

Few features I have in mind for future are user templating of nodes, tristate, drag and drop etc. Hope I will get started soon on these. I request developers who are interested to contribute for this plugin.

Happy coding.

Monday, September 23, 2013

Angular treeview directive 0.5v (ui.treeview 0.5v).

Hi,

With this post, the new treeview directive version changes(ui.treeview.js v0.5) will be explained. The new plugin can be downloaded from this link.

The first and foremost change is in the implementation of search functionality of the treeview.

I noticed that using the default angular's filter is not suitable for the control like treeview. Hence with this release there's new custom filter in place which will be upgraded in coming versions.

Here's list of options and their usage description of this version.

AttributesDataTypeDescription
treeview-data (Required)Array (Binding)bind the data variable from your controller for which you wish to display treeview.
unique-id (Optional)Stringoptional attribute for now. Bind the attribute which is unique for the node.
display-attr (Required)String bind the attribute of the data which has to be displayed at each node.
data-check (Optional)booleanAttribute to specify whether to display check box for the nodes or not. Default is false.
data-check-rec (Optional)booleanAttribute to specify whether to select all children nodes on selection of a single node. Default is true.
expand-all (Optional)booleanAttribute to specify whether the tree should be expanded or not. Default is collapse
searchModel (Optional)String (Binding)Attribute for searching tree.

Check out the plugin and please leave your comments for improving this.

Happy Coding.

Monday, September 16, 2013

AngularJS - Accessing parent scope's variables/model inside nested ng-repeat


Hi,

Working with nested ng-repeat's can be tricky, here's a issue I ran into while I was designing html with nested ng-repeat.

Problem: Say I have scenario in which I have to design the template in such a way that the template is nested in itself.

<script type="text/ng-template" id="template.html">
 <div>{{data.name}}</div>
    <ul>
        <li ng-repeat="data in data.Children" ng-include="'template.html'" ></li>
    </ul>
</script>

This is our data which is bound to the template.

[{
  "id": "6000", "Name": "All Categories",
    "Children": [{
      "id": "6100", "Name": "Juice",
        "Children": [{
          "id": "6200", "Name": "Mom's Brands",
            "Children": [{ "id": "6454", "Name": "Mom's Apple Jug" },
              { "id": "6456", "Name": "Mom's Apple Juice" },
              { "id": "6458", "Name": "Mom's Grape Juice" },
              { "id": "6462", "Name": "Mom's Apple Grape Jug" },
              { "id": "6463", "Name": "Mom's Pear Blend" },
              { "id": "6464", "Name": "Mom's Juice Cocktail Lite" }
        ]
      }]
    }]
  }]

Now I have a model(say $scope.dontShowID = "6454") in controller based on which I need to check some condition to display or not to display the "<li>". And hence I modified the template code as follows.

<li ng-show="data.ID!=dontShowID" ng-repeat="data in data.Children" ng-include="'template.html'" ></li>

The above code doesn't work because ng-repeat creates its own scope and hence we might have to access parent's scope to access dontShowID. So to access parent's scopes the code will change as.

<li ng-show="data.ID!=$parent.dontShowID" ng-repeat="data in data.Children" ng-include="'template.html'" ></li>

Since this is a nested template our data will also be in nested form. Everytime there's a ng-repeat a new scope is created, in such a case the above code will not work as $parent will have reference to the parent ng-repeat's scope.

Now here's the problem to access dontShowID at each level of data. Even if we know the level at which data is it is difficult to access controller scope's model.

But somehow this rule doesn't apply to methods.

Solution: Nearest decent solution I have been able to find is to add a init method just before ng-repeat i.e, at {{data.name}} and pass data as argument to the init method. Inside the init method assign the dontShowID to data.dontShowID.

So the code will change as.

<script type="text/ng-template" id="template.html">
 <div ng-init="initData(data)">{{data.name}}</div>
    <ul>
        <li ng-show="data.dontShowID!=data.ID" ng-repeat="data in data.Children" ng-include="'template.html'" ></li>
    </ul>
</script>

And the code of initData method will be...

$scope.initData = function (data) {
    data.dontShowID = $scope.dontShowID;
};

Once we assign the model at the data level, the condition checking is easy as we no longer need to access parent scope. Hope this helps in solving ng-repeat scope issues. This is the solution I have found so far, any thoughts on how we can improve this?

Tuesday, July 2, 2013

Angular treeview directive 0.1v

Hi,

UPDATE: The latest plugin is in this blog.

In continuation to my previous blog here, I am releasing new version of the treeview directive.

You can download the plugin from this link.

This version supports searching and has place for checking nodes.

For searching you have to bind a model which has search text in the current scope to the attribute search-model of treeview directive, as shown below.

<input type="text" width="50px" ng-model="searchText" placeholder="Search products"/>
<treeview style="width: 300px;" search-model="searchText" treeview-data="productData" unique-id="id" display-attr="Name">
</treeview>

The same model can be bound to some other control also, for example input text control. In this way when there's any update to the model the tree nodes will be sought and displayed.

Tri-state support, retrieving checked nodes, user template etc. will be following in next blog posts.

Happy Coding.

Friday, June 28, 2013

Treeview directive for Angular

HI,

UPDATE: The latest plugin is in this blog.

Here's a treeview directive developed for and with angular framework. I am releasing beta version in this blog, which has just treeview control display feature.

You can download the treeview.js from this link angular treeview download. The plugin is compatible with angular1.0.5. It also has a css file to be included along with the plugin in your html pages.

So here's how you can start using this directive.

  1. Download the plugin and include ui.treeview.min.js and treeview.css files in your html page.
  2. You have to inject 'ui.treeview' in your application module.
    angular.module("demo", ['ui.treeview']);
  3. The model which contains data to be displayed should be bound to "treeview-data" attribute of the directive.

    Sample directive code looks like this.

    <treeview treeview-data="productData" unique-id="id" display-attr="Name"></treeview>
As of now 3 attributes are present.
  • treeview-data: bind the data variable from your controller for which you wish to display treeview.
  • unique-id: optional attribute for now. Bind the attribute which is unique for the node.
  • display-attr: bind the attribute of the data which has to be displayed at each node.

Look forward for these features in future releases

  • check box option at nodes
  • Searching
  • Tri state implementation
  • Allowing user templates
  • and more...

Happy Coding.

Wednesday, June 26, 2013

Google chrome frame for old browers

Google has released a plugin for all old browsers to be able to render HTML 5 pages.

Check out the usage of plugin here

Its obviously a boon for HTML 5 developers, who are plucking their hair to support the HTML 5 web application in old browsers for various reasons.

There's one thing to be taken care by developer while using google chrome frame. Thats resetting of sessionStorage value on navigation.

So instead of sessionStorage you might have to use localStorage or cookie for client side data storage.

Happy coding