简体   繁体   English

Angular js选择父级和子级值

[英]Angular js Select Parent and Children Values

I'm trying to get the results like the following manner, Any expert inputs please to achieve this! 我正在尝试通过以下方式获得结果,请任何专家输入来实现!

Expected work flow chart, 预期工作流程图,

在此处输入图片说明

Here the default view, 这里是默认视图,

在此处输入图片说明

Scenario 1: When click on the number "1" all the left to right and children need to highlight like the below, 方案1:当点击数字“ 1”时,从左到右,孩子们需要如下所示突出显示,

在此处输入图片说明

Scenario 2: Considering the scenario 1 results, click on the number "3" all the left to right and children need to remove highlight like the below since 3 we consider as parent/root, 方案2:考虑方案1的结果,请从左到右单击数字“ 3”,并且子级需要删除如下所示的突出显示,因为我们认为3是父级/根级,

在此处输入图片说明

Scenario 3: Considering the default view, By default there is no selection When click on the number "18" all the parent values need to highlight like the below, 方案3:考虑默认视图,默认情况下没有选择当单击数字“ 18”时,所有父值都需要如下所示突出显示,

在此处输入图片说明

Scenario 4: Considering the Scenario 3 results , When click on the number "18" only for 18 the highlight need to be removed and like the below, Right to left parent level deselection not required for any value. 方案4:考虑方案3的结果,当仅单击18的数字“ 18”时,需要删除高亮显示,并且像下面一样,不需要从右到左父级取消选择任何值。

在此处输入图片说明

Note: Right to left parent level deselection not required for any value. 注意:任何值都不需要从右到左父级取消选择。

Here is the code: JSFiddle 这是代码: JSFiddle

  $scope.setActivePrevNext = function (arr) {
        let c;
        arr.RowValues.forEach(e => {
            e.isActive = !e.isActive; c = e.isActive;
        });
        index = $scope.groupOfCheckboxes.findIndex(x => x.Row == arr.Row);
        childrenIndex = index + 1;
        if ($scope.groupOfCheckboxes[childrenIndex] !== undefined) {
            $scope.groupOfCheckboxes[childrenIndex].RowValues.forEach(e => {
                e.isActive = c;
            })
        };
    }
    $scope.setBinary = function (id) {
        $scope.groupOfCheckboxes.forEach(e => {
            e.RowValues.forEach(item => {
                if (item.td == id) $scope.setActivePrevNext(e);
            })
        });
    }

I did not give it a try with the logic from your JSFiddle, but instead this is inspired in this answer: Angularjs: understanding a recursive directive 我没有尝试使用JSFiddle中的逻辑,而是从以下答案中获得启发: Angularjs:了解递归指令

Please refer to the answers in that link for explanation of the logic. 请参考该链接中的答案以获取逻辑解释。 From there I just had to had event $emit and $broadcast to be able to tell parents or children scopes to update accordingly 从那里我只需要进行事件$ emit和$ broadcast就能告诉父母或孩子的范围进行相应的更新

 var module = angular.module('myapp', []); module.controller("TreeCtrl", function($scope) { $scope.treeFamily = { name: "1", children: [{ name: "2", children: [{ name: "3", children: [{ name: "4", children: [{ name: "5", children: [] }] }, { name: "9", children: [] }] }, { name: "13", children: [{ name: "14", children: [] }, { name: "18", children: [] }] }] }] }; }); module.directive("tree", function($compile) { return { restrict: "E", scope: { family: '=' }, template: '<div class="circleItem" ng-click="circleClicked()" ng-class="{highlight: isHighlighted}">{{ family.name }}</div>' + '<ul>' + '<li ng-repeat="child in family.children">' + '<tree family="child"></tree>' + '</li>' + '</ul>', compile: function(tElement, tAttr) { var contents = tElement.contents().remove(); var compiledContents; return function(scope, iElement, iAttr) { if (!compiledContents) { compiledContents = $compile(contents); } compiledContents(scope, function(clone, scope) { iElement.append(clone); }); }; }, controller: function($scope) { $scope.$on('event:changeColorOfParents', function(event, clickedCircleNumber) { if (clickedCircleNumber !== $scope.family.name) { $scope.isHighlighted = true; } }); $scope.$on('event:changeColorOfChildren', function(event, clickedCircleNumber) { if (clickedCircleNumber !== $scope.family.name) { $scope.isHighlighted = !$scope.isHighlighted; } }); $scope.circleClicked = function() { $scope.isHighlighted = !$scope.isHighlighted; $scope.$emit('event:changeColorOfParents', $scope.family.name); $scope.$broadcast('event:changeColorOfChildren', $scope.family.name); }; } }; }); 
 li { list-style: none; } tree { margin-left: 20px; display: block; } .circleItem { text-align: center; cursor: pointer; border: 1px solid black; border-radius: 50%; width: 25px; height: 25px; background: white; } .circleItem.highlight { background: red; } 
 <div ng-app="myapp"> <div ng-controller="TreeCtrl"> <tree family="treeFamily"></tree> </div> </div> <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.5/angular.min.js"></script> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM