0

Looking to display a nested json in a treeview structure in ui-select.
I can create a tree using an angular directive:

Code

data:        
$scope.treeFamily = {
            name : "Parent",
            children: [{
                name : "Child1",
                children: [{
                    name : "Grandchild1",
                    children: []
                },{
                    name : "Grandchild2",
                    children: []
                },{
                    name : "Grandchild3",
                    children: []
                }]
            }, {
                name: "Child2",
                children: []
            }]
        };


directive:
        module.directive("tree", function($compile) {
            return {
                restrict: "E",
                transclude: true,
                scope: {family: '='},
                template:       
                    '<ul>' + 
                        '<li ng-transclude></li>' +
                        '<p>{{ family.name }}</p>' + 
                        '<li ng-repeat="child in family.children">' +
                            '<tree family="child"></tree>' +
                        '</li>' +
                    '</ul>',
                compile: function(tElement, tAttr, transclude) {
                    var contents = tElement.contents().remove();
                    var compiledContents;
                    return function(scope, iElement, iAttr) {
                        if(!compiledContents) {
                            compiledContents = $compile(contents, transclude);
                        }
                        compiledContents(scope, function(clone, scope) {
                                 iElement.append(clone); 
                        });
                    };
                }
            };
        });

html:
        <tree family="treeFamily">

        </tree>

I would like to be able to search/select each node in ui-select. Looking for suggestions.

1 Answer 1

3

Sadly, there really isn't any way of making your directive work inside of the ui-select directive as it is handling all of its options creation using repeats and does not give you the option of overriding this without in essence rewriting the ui-select box. However, you can template the display items. start by flattening your data into an array:

 [{name: 'Parent', treelevel: 0},
  {name: 'Child', treelevel: 1},
  {name: 'Grandchild', treelevel: 2}]

then from there, write your ui-select as follows:

<ui-select ng-model="person.selected" theme="select2" ng-disabled="disabled" style="min-width: 300px;">
  <ui-select-match placeholder="Select a person in the list or search his name/age...">{{$select.selected.name}}</ui-select-match>
  <ui-select-choices repeat="person in people | propsFilter: {name: $select.search, age: $select.search}">
    <div style="padding-left: {{15 * person.treelevel}}px" ng-bind-html="person.name | highlight: $select.search"></div>
  </ui-select-choices>
</ui-select>

that will display everything in a tree like format. the following plunk has a working version of this: http://plnkr.co/edit/Rzlu6zIHOYzVlhQebh7V?p=preview

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.