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-20T06:15:52.201258

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

  • How to automatically change path in angular when scrolling
  • Error two clicks to be able to login Angular 16
  • Passing an array of objects through the $http.post method in angular JS does not work
  • Understanding how to apply Angular Signals from beginning on an existing service
  • provider duplicate while compiling a Cordova application for the Android platform
  • How can I use angularjs $parse service in Angular?
  • Width of custom headers in ag-grid (angular) doesn't match with column's width
  • Issues with Katex/ngx-markdown Rendering in Angular 16
  • How to make Angular SSR wait for async operations to finish that are initiated in ngOnInit?
  • I want to install @angular/google-maps npm Package in angular 16.2.12 but "npm install @angular/google-maps" this is not working/ tell me other query
  • Angular 17 standalone application integrate CKEditor 5 -- Error: window is not defined
  • Why is $scope >= 0 showing true in interpolation while empty in controller?
  • The XMLHttpRequest compatibility library was not found
  • Making Gantt Chart Column Labels More Readable
  • Pass key-value pairs of object as individual arguments to component in Angular

Related Questions in TWITTER-BOOTSTRAP

  • General questions about creating a custom theme Moodle CMS
  • How to fix - The modal dissapear below 992px.?
  • Bootstrap horizontal gutters issue
  • How can I add a list of icons to the right of a text area
  • Why isn't my Bootstrap background color class working as expected?
  • Edit button in each row of table using same Bootstrap modal as add button
  • Change Opacity of Bootstrap Button
  • bootstrap 5 multiple tab dropdown with tab pane
  • Bootstrap v5.3 navbar drop-downs not working. Why?
  • Should i learn Bootstrap in 2024
  • How to turn on Bootstrap Intellisense in .NET 8 Blazor
  • Bootstrap 5.3 burger menu not responsive in Underscore WordPress template
  • Bootstrap 5 columns not displaying inline
  • Bootstrap scaffolding not showing good on mobile
  • How to Prevent Select2 Dropdown from Moving with Scroll inside a Bootstrap Vue Modal?

Related Questions in NODES

  • What line of code do I change to avoid duplication in a linked list?
  • AVL tree Nth largest operation - How to have all my tests pass? JAVA
  • Node border width control in Netgraph
  • Java Jackson update json 2nd value instance in array
  • Nodes of a Sankey diagram in R don't group together
  • Change size of terminal_panel = node_barplot in ctree
  • Make a Cluster without using MongoDB Atlas
  • reactive props in Vue3 Treelist causes recursive updates
  • Creating a Method in C# that traverses HTML document and extracts content based on a query, i.e Custom HTML Crawler
  • Get status(like/dislike) or colour of node using accessibility service
  • Handle replace nodes due to remove or crash at Artifactory
  • Drawing a node in click position using Vis.js
  • How do I make several nodes line up and move up when the first one is removed? (GDscript)
  • Which node should I use to detect mouse or touch drag on screen in Godot
  • Why is my element not a node ? Drag and Drop

Related Questions in ANGULAR-UI-TREE

  • how to use recursive function to select the parent in a tree array using angulat ui tree?
  • AngulrJs Ui-tree Search problem with same title
  • Angular-ui-tree shows an extra line when last child has children
  • How to limit drag and drop capability to only sibling-level elements in angular-ui-tree?
  • angular-ui-tree doesn't work with more than 9 levels
  • Total sum all nodes by key
  • angular-ui-tree behaves strangely after updated list
  • AngularJS UI Tree - Single header for entire tree
  • Angular (2) TREE . Double click issue. Not working properly.
  • Angular-ui-tree keep nodes to their level on drag and drop
  • How to Expand a Default Angular UI Tree Node
  • How to delete a node in Angular Tree
  • Uncaught Error: [$injector:modulerr] ui.tree
  • Generate template with nested rows
  • Wrong item scrolled on Mozilla Firefox

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 node.js arrays c asp.net json

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