DEVHIDE
  • Home (current)
  • About
  • Contact
  • Cookie
  • Home (current)
  • About
  • Contact
  • Cookie
  • Disclaimer
  • Privacy
  • TOS
Login Or Sign up

How to Expand a Default Angular UI Tree Node

1.3k Views Asked by crackedcornjimmy At 21 December 2017 at 21:50 2025-12-10T10:20:59.658649

I have the following markup:

<div class="modal fade" id="locationSearchModal" tabindex="-1" role="dialog">
    <div class="modal-dialog narrow-modal" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title" id="myModalLabel">Search for Locations</h4>
            </div>
            <div class="modal-body">
                <div class="tree-container">                  
                    <section>
                        <h2>Browse for Locations</h2>
                        <div ui-tree="" data-drag-enabled="false" id="tree-root">
                            <ul ui-tree-nodes="" ng-model="data">
                                <li ng-repeat="item in data" ui-tree-node="" collapsed="!item.ItemsRetrieved" ng-include="item.Place || item.$hashkey == undefined ? 'parent_items_renderer' : 'terminal_item_renderer' " ></li>
                            </ul>
                        </div>
                        <script type="text/ng-template" id="parent_items_renderer">
                            <div ui-tree-handle class="tree-node tree-node-content" ng-class="{'tree-node-open': !collapsed}" ng-click="toggle(item); convertObjs(item)">
                                <i class="fa fa-caret-right" ng-class="{'fa-caret-right': collapsed, 'fa-caret-down': !collapsed}"></i>
                                <i class="fa fa-map-marker" ng-class="{'text-blue': !collapsed}"></i>
                                <span class="" ng-bind-html="item.PlaceName"></span>
                            </div>
                            <ul ng-if="item.Place != null" ui-tree-nodes ng-model="item.Place" ng-class="{hidden: collapsed}">
                                <li ng-repeat="item in item.Place" ui-tree-node collapsed="!item.ItemsRetrieved" ng-include="item.Place ? 'parent_items_renderer' :  'terminal_item_renderer' "  on-finish-render="ngRepeatFinished"> </li>
                            </ul>

                        </script>
                        <script type="text/ng-template" id="terminal_item_renderer">
                            <div ui-tree-handle class="tree-node tree-node-content" ng-class="{'tree-node-open': !collapsed}" ng-click="addLocation(item)">
                                <a href title="Add Location"><span class="" ng-bind-html="item.PlaceName"></span></a>
                            </div>
                        </script>
                    </section>
                </div>
            </div>
        </div>
    </div>
</div>

The JSON data object that contains the location data is an hierarchical collection of Places:

{  
   "PlaceHierarchy":{  
      "Places":{  
         "Place":{  
            "PlaceID":"1000",
            "PlaceTypeID":"5",
            "PlaceName":"Company",
            "AbbrName":"Company",
            "Place":[  
               {  
                  "PlaceID":"2000",
                  "PlaceTypeID":"4",
                  "PlaceName":"Region",
                  "AbbrName":"ThePlace",
                  "Place":[  
                     {  
                        "PlaceID":"3000",
                        "PlaceTypeID":"3",
                        "PlaceName":"SubRegion",
                        "AbbrName":"TheSubPlace",
                        "Place":[  
                           {  
                              "PlaceID":"4000",
                              "PlaceTypeID":"2",
                              "PlaceName":"SubSubRegion",
                              "AbbrName":"TheSubSubPlace",
                              "Place":[  
                                 {  
                                    "PlaceID":"5000",
                                    "PlaceTypeID":"1",
                                    "PlaceName":"Building",
                                    "AbbrName":"Building",
                                    "Place":[  
                                       {  
                                          "PlaceID":"5001",
                                          "PlaceTypeID":"6",
                                          "PlaceName":"Lobby",
                                          "AbbrName":"Lobby"
                                       },
                                       {  
                                          "PlaceID":"5002",
                                          "PlaceTypeID":"6",
                                          "PlaceName":"Lobby 2",
                                          "AbbrName":"Lobby2"
                                       }
                                    ]
                                 }
                              ]
                           }
                        ]
                     }
                  ]
               }
            ]
         }
      }
   }
}

When I get that JSON back from the API, I need to process the data to make sure that all nodes are arrays. I do it like this:

$scope.processLocationNodes = function (nodes) {
        for (var node in nodes) {
            if (angular.isArray(node)) {
                $scope.processLocationNodes(node);
            } else {
                $scope.convertObjs(node);
            };
        }
    };

$scope.convertObjs = function (item) {

        angular.forEach(item.Place, function (items) {
            if (items != undefined && !angular.isString(items)) {
                if (items.Place && !angular.isArray(items.Place)) {
                    var PlaceObj = items.Place;
                    items.Place = [];
                    items.Place.push(PlaceObj);
                }
            }
        });
    };

Now, when the modal is shown, the data properly displays and the tree works as expected. The only problem is, I want to default the tree to expand to the node of the Default Place of the user. I do that by the following logic:

The onFinishRender directive (debugging the code shows that this is hit):

app.directive('onFinishRender', function ($timeout) {
    return {
        link: function (scope, element, attr) {
            if (scope.$last === true) {
                $timeout(function () {
                    scope.$emit(attr.onFinishRender);
                });
            }
        }
    }
});

The ngRepeatFinished function is as follows:

$scope.$on('ngRepeatFinished', function (ngRepeatFinishedEvent) {
        var rootScope = $scope.getRootNodesScope();

        if (rootScope != undefined) {
            rootScope.collapseAll();
            $scope.expandNode($scope.defaultPlace);
        }
    });

$scope.getRootNodesScope = function() {
        return angular.element(document.getElementById("tree-root")).scope().$nodesScope.childNodes()[0];
    }

$scope.expandNode = function(nodeId) {

        // We need to get the whole path to the node to open all the nodes on the path
        var parentScopes = $scope.getScopePath(nodeId);

        for (var i = 0; i < parentScopes.length; i++) {
            parentScopes[i].expand();
        }

    };

$scope.getScopePath = function (nodeId) {
    return $scope.getScopePathIter(nodeId, $scope.getRootNodesScope(), []);
};

$scope.getScopePathIter = function(nodeId, scope, parentScopeList) {

        if (!scope) return null;

        var newParentScopeList = parentScopeList.slice();
        newParentScopeList.push(scope);

        if (scope.$modelValue && scope.$modelValue.id === nodeId) return newParentScopeList;

        var foundScopesPath = null;
        var childNodes = scope.childNodes();

        for (var i = 0; foundScopesPath === null && i < childNodes.length; i++) {
            foundScopesPath = $scope.getScopePathIter(nodeId, childNodes[i], newParentScopeList);
        }

        return foundScopesPath;
    };

Now, here's what my problem is:

First of all at the angular.element(document.getElementById("tree-root")).scope().$nodesScope.childNodes()[0] code, "childNodes()" is empty. No childNodes exist at all. Thus, the code has nothing to collapse or expand. I don't know why the childNodes collection is empty.

Second, once I figure that out, I can see what the actual NodeId is for a specific node and then be able to expand the tree to that node using the $scope.defaultPlace object.

Essentially, I just need to know why the childNodes collection is empty.

angularjs twitter-bootstrap nodes angular-ui-tree
Original Q&A
0

There are 0 best solutions below

Related Questions in ANGULARJS

  • Angular Show All When No Filter Is Supplied
  • Using pagination on a table in AngularJS
  • State with different subviews
  • Getting and passing MVC Model data to AngularJS controller
  • Implementing prerender.io middleware in sails.js
  • Token based authorization in nodejs/ExpressJs and Angular(Single Page Application)
  • AngularJS, Google App Engine and URLrewrite
  • send data from table to another page into forms
  • How to write tests for classes with inheritance
  • angularJS sending OPTIONS instead of POST
  • Receiving POST from external application in AngularJS
  • Metaprogramming AngularJS Filters
  • Reload List after Closing Modal
  • Why is my angularjs site not completely crawlable?
  • Why is separation of JavaScript and HTML a good practice?

Related Questions in TWITTER-BOOTSTRAP

  • UIWebView Screen Fitting Issue
  • Website zoomed out on Android default browser
  • Twitter Bootstrap horizontal form elements on a line
  • My navbar is not expanding after collapse
  • Reload List after Closing Modal
  • Overwrite Bootstrap style for a single page
  • UI.Bootstrap Angular Typeahead and Firebase Array
  • Bootstrap - Fixed navbar doesn't do what's expected?
  • Bootstrap NavBar with no content on small screens
  • Bootstrap Dropdown Button Problems
  • ScrollBar not showing up - BootStrap
  • Bootstrap flip card with css3 transform
  • bootstrap grid layout questions
  • How do I make jquery events work in a bootstrap modal with dynamic content and selectors?
  • Using GLYPHICONS's free pngs with classes

Related Questions in NODES

  • Issues with reversing the linkedlist
  • C++ Unable to Print Pointer Data of a Linked List
  • Send information to Maya node instance in cpp
  • Couting nodes with onyl one child in BST
  • Find the parent node of a node in binary search tree
  • Tree implementation in C++: Cannot convert Node to Node*
  • From 2 column csv to 2 color NetworkX graph
  • SQL Tree Structure Table
  • Multiple Node Styles
  • Javax JCR Node getProperties and Titles
  • Check if item exists in XML with AS3
  • Constructor and const reference
  • How to add children nodes to the last parent node
  • Show nodes with more than one relationship using NEO4j
  • How do I go about the traversal of Binary Search Trees?

Related Questions in ANGULAR-UI-TREE

  • How to prevent Bower from hanging up when installing angular-ui-tree?
  • Adding labels to the trees in angular-ui-tree
  • Angular JS (angular-ui-tree) ng-click conflict vs drag start event
  • Wrong item scrolled on Mozilla Firefox
  • angular-ui-tree doesn't work with more than 9 levels
  • How to limit drag and drop capability to only sibling-level elements in angular-ui-tree?
  • How to access a particular node in angular UI tree?
  • How to delete a node in Angular Tree
  • angular-ui-tree two events instead of one
  • Angular (ui tree) ng-repeat: infinite loop
  • Angular ui-tree detect drag event between connected trees
  • angular-ui-tree: How to get html element of last added node
  • Angular UI Tree: Get old and new parent of node
  • Angular UI Tree - Allow only drag & drop into second level (children nodes)
  • How to change cursor?

Trending Questions

  • UIImageView Frame Doesn't Reflect Constraints
  • Is it possible to use adb commands to click on a view by finding its ID?
  • How to create a new web character symbol recognizable by html/javascript?
  • Why isn't my CSS3 animation smooth in Google Chrome (but very smooth on other browsers)?
  • Heap Gives Page Fault
  • Connect ffmpeg to Visual Studio 2008
  • Both Object- and ValueAnimator jumps when Duration is set above API LvL 24
  • How to avoid default initialization of objects in std::vector?
  • second argument of the command line arguments in a format other than char** argv or char* argv[]
  • How to improve efficiency of algorithm which generates next lexicographic permutation?
  • Navigating to the another actvity app getting crash in android
  • How to read the particular message format in android and store in sqlite database?
  • Resetting inventory status after order is cancelled
  • Efficiently compute powers of X in SSE/AVX
  • Insert into an external database using ajax and php : POST 500 (Internal Server Error)

Popular # Hahtags

javascript python java c# php android html jquery c++ css ios sql mysql r reactjs

Popular Questions

  • How do I undo the most recent local commits in Git?
  • How can I remove a specific item from an array in JavaScript?
  • How do I delete a Git branch locally and remotely?
  • Find all files containing a specific text (string) on Linux?
  • How do I revert a Git repository to a previous commit?
  • How do I create an HTML button that acts like a link?
  • How do I check out a remote Git branch?
  • How do I force "git pull" to overwrite local files?
  • How do I list all files of a directory?
  • How to check whether a string contains a substring in JavaScript?
  • How do I redirect to another webpage?
  • How can I iterate over rows in a Pandas DataFrame?
  • How do I convert a String to an int in Java?
  • Does Python have a string 'contains' substring method?
  • How do I check if a string contains a specific word?
.

Copyright © 2021 Jogjafile Inc.

  • Disclaimer
  • Privacy
  • TOS
  • Homegardensmart
  • Math
  • Aftereffectstemplates