javascript - ExpandCollapse child elements in a tree (AngularJS) - Stack Overflow

I'm a newbie in AngularJS and faced with an issue.Need to create message history tree, which provi

I'm a newbie in AngularJS and faced with an issue.

Need to create message history tree, which provides expand/collapse functionality on the parent item.

I've created view:

<div class="tree">
<ul>
    <li ng-repeat="appt in apptsToFilter() | filter: groupByAppt" ng-click="showChilds()">
        <span>{{appt.startTime}} - Appt. Id: {{appt.appointmentId}} </span>
        <ul>
            <li ng-repeat="item in appts | filter: {appointmentId: appt.appointmentId}" ng-show="active">
                <span>{{item.message}}</span>
            </li>
        </ul>
    </li>
</ul>
</div>

This is my controoler for this view:

function MessageHistoryController($scope, $routeParams, MessageHistoryResource) {
    ......//some code

    $scope.active = false;
    $scope.showChilds = function() {
        if ($scope.active == false) {
            $scope.active = true;
        } else {
            $scope.active = false;
        }
    };
}

This is what I get, when I've expanded first parent item:

ParentItem2:
   - Child1
   - Child2
   - Child3

ParentItem2
   - Child1
   - Child2

ParentItem3
   - Child1

When I click on any parent item - all of my subtrees expanded or collapsed.

But I expected this result, as below (only clicked item should be expanded):

ParentItem2:
   - Child1
   - Child2
   - Child3
ParentItem2
ParentItem3

Any ideas are appreciated. Thanks.

I'm a newbie in AngularJS and faced with an issue.

Need to create message history tree, which provides expand/collapse functionality on the parent item.

I've created view:

<div class="tree">
<ul>
    <li ng-repeat="appt in apptsToFilter() | filter: groupByAppt" ng-click="showChilds()">
        <span>{{appt.startTime}} - Appt. Id: {{appt.appointmentId}} </span>
        <ul>
            <li ng-repeat="item in appts | filter: {appointmentId: appt.appointmentId}" ng-show="active">
                <span>{{item.message}}</span>
            </li>
        </ul>
    </li>
</ul>
</div>

This is my controoler for this view:

function MessageHistoryController($scope, $routeParams, MessageHistoryResource) {
    ......//some code

    $scope.active = false;
    $scope.showChilds = function() {
        if ($scope.active == false) {
            $scope.active = true;
        } else {
            $scope.active = false;
        }
    };
}

This is what I get, when I've expanded first parent item:

ParentItem2:
   - Child1
   - Child2
   - Child3

ParentItem2
   - Child1
   - Child2

ParentItem3
   - Child1

When I click on any parent item - all of my subtrees expanded or collapsed.

But I expected this result, as below (only clicked item should be expanded):

ParentItem2:
   - Child1
   - Child2
   - Child3
ParentItem2
ParentItem3

Any ideas are appreciated. Thanks.

Share Improve this question edited May 22, 2017 at 10:02 Artyom Pranovich asked Dec 4, 2013 at 19:56 Artyom PranovichArtyom Pranovich 6,9628 gold badges43 silver badges60 bronze badges 1
  • What is your question? – Zack Argyle Commented Dec 4, 2013 at 20:28
Add a ment  | 

1 Answer 1

Reset to default 6

That's because you have all of your parent, clickable items, set to one boolean. This can be resolved in the HTML without the active bool.

  1. Set your show/hide element to:

    ng-show="appt.active"

  2. Then set your click call to:

    ng-click="appt.active = !appt.active"

Full Example:

<div class="tree">
<ul>
    <li ng-repeat="appt in apptsToFilter() | filter: groupByAppt" ng-click="appt.active = !appt.active">
        <span>{{appt.startTime}} - Appt. Id: {{appt.appointmentId}} </span>
        <ul>
            <li ng-repeat="item in appts | filter: {appointmentId: appt.appointmentId}" ng-show="appt.active">
                <span>{{item.message}}</span>
            </li>
        </ul>
    </li>
</ul>
</div>

Even though appt.active would initialize as undefined when clicked it will change to true, at least it does on my end and this is how I handle these cases.

You could also loop through the appt and define active in the javascript.


For future reference you can simplify your $scope.showChilds function to:

$scope.showChilds = function () {
    $scope.active = !$scope.active
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744381487a4571452.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信