0

所以,我有一个带有复选框的角树,可以选择父母和孩子。

我在这里要做的是预先选择树的一些项目,例如: preselectids=['105'] 选择项目或者如果它的父母选择所有孩子。

当我通过 showsetIds=['105'] 时,我还想只显示该树的节点。

我举了一个例子,但这仅适用于一个节点,因为代码是以这种方式编写的。我曾尝试在我的本地解决此问题,但无法对其进行修改以满足我的需求。

请任何人都可以指导我完成这个。

https://plnkr.co/plunk/nVDL1GMfgEQSt7io

 return {
    restrict:'E',
    templateUrl:'hierarchySearch.tpl.html',
    scope: {
        dataset:'='
    },
    controller:function($scope) {
        $scope.numSelected = 0;
        //$scope.list is used by ng-tree, dataset should never be modified
        $scope.list = angular.copy($scope.dataset);
        
        $scope.options = {};
        
        $scope.expandNode = function(n,$event) {
            $event.stopPropagation();
            n.toggle();
        }
        
        
        $scope.itemSelect = function(item) {
            var rootVal = !item.isSelected;
            HierarchyNodeService.selectChildren(item,rootVal)
            
            HierarchyNodeService.findParent($scope.list[0],null,item,selectParent);
            var s = _.compact(HierarchyNodeService.getAllChildren($scope.list[0],[]).map(function(c){ return c.isSelected && !c.items;}));
            $scope.numSelected = s.length;
        }   
        
        function selectParent(parent) {
            var children = HierarchyNodeService.getAllChildren(parent,[]);
            
            if(!children) return;
            children = children.slice(1).map(function(c){ return c.isSelected;});
            
            parent.isSelected =  children.length === _.compact(children).length;
            HierarchyNodeService.findParent($scope.list[0],null,parent,selectParent)
        }       

        $scope.nodeStatus = function(node) {
            var flattenedTree = getAllChildren(node,[]);
            flattenedTree = flattenedTree.map(function(n){ return n.isSelected });

            return flattenedTree.length === _.compact(flattenedTree);
        }  

    },
    link:function(scope,el,attr) {
        
        scope.$watch('pastUsersFilter',function(nv){
           if(_.isUndefined(nv)) return;
           
           if(nv) {
               HierarchyNodeService.trimLeafs(scope.list[0]);
           } else {
               scope.list = angular.copy(scope.dataset);
           }
           
        });
        
      
        var inputTimeout;
        var time = 300;
        scope.$watch('searchValue',function(nv) {
            if(!nv && nv !== '') {
                return;
            }
            var previousDataset = angular.copy(scope.list);
            var newData = (scope.searchValue === '') ? angular.copy(scope.dataset) : [HierarchyNodeService.treeSearch(angular.copy(scope.dataset[0]),scope.searchValue)];
            
            if(newData.length === 1 && _.isEmpty(newData[0]) ) {
              scope.emptyData = true;
              return;
            }
            
            scope.emptyData = false;
            if(_.isEqual(previousDataset,newData)) {
              clearTimeout(inputTimeout);
              return;
            } 
            
            scope.list = newData;
            
            
            $timeout.cancel(inputTimeout);
            inputTimeout = $timeout(function() {
                
                var els = document.querySelectorAll('[ui-tree-node]');
                
                Array.prototype.forEach.call(els,function(el) {
                    el = angular.element(el);
                    var elScope = el.scope();
                    if(elScope.$modelValue.match) {
                        
                        elScope.expand();
                        
                        //loop through all parents and keep expanding until no more parents are found
                        var p = elScope.$parentNodeScope;
                        while(p) {
                          p.expand();
                          p = p.$parentNodeScope;
                          
                        }
                    }
                });
            },500);
        });
        
        
      
        
        scope.$watch('list',function(nv,ov) {
            if(!nv) return;
            if(nv && !ov) { scope.$apply();}
            
            
            //UPDATE SELECTED IDs FOR QUERY
            //get the root node
            var rootNode = nv[0];
            
            //get all elements where isSelected == true
            var a = HierarchyNodeService.getSelected(rootNode,[]);
            
            //get the ids of each element
            a = _.pluck(a,'id');
            
            scope.selected = a;
    
        },true);
    }
}

}) }).call(this);

4

0 回答 0