[FIXED] I have to put the hyphen on the parent node when a child is selected with primeng

Issue

enter image description here

This is Typescript
//DICHIARATIONS AN CODE

Ngonint–>

  ngOnInit() {
        if(this.title === "Create"){
        this.dataProfilo = {}
        this.dataProfilo.function = [];
        this.service.getTree().subscribe(
          (res) => {
            this.node= res.nodeTree;
          },
        )
        }else{
        this.dataProfilo={...this.profilo};
        this.service.getProfiloById(this.profilo.id).subscribe(
          (res) =>{
            if(res && res.nodeTree.length >0){
            this.node= res.nodeTree;
            res.nodeTree.forEach(elem => {
              this.checkSelectionNode(elem);
    
            });

      if(res && res.nodeTree.length >0){
              this.node= res.nodeTree;
              res.nodeTree.forEach(elem => {
                this.uncheckSelectionNode(elem);
              });
            }
          }
          },
          (error)=> {
          });
        }  
    }
   

if nodes are checked

  checkSelectionNode(node) {
        if (node.data.flag) {
          this.selectedFile.push(node);
        }
        if (node && node.children && node.children.length > 0) {
          node.children.forEach(childNode => {
            this.checkSelectionNode(childNode);
          }
          );
        }
      }
**if node are unchecked**

 uncheckSelectionNode(node) {
    if (node.data.flag == false) {
      this.unselectedFile.push(node);
    }
    if (node && node.children && node.children.length > 0) {
      node.children.forEach(childNode => {
        this.uncheckSelectionNode(childNode);
      }
      );
    }
  }

IF NODES ARE SELECTED – both father and child——————-

  nodeSelect(event){
        if(event.node.children && event.node.children.length > 0){
          event.node.children.forEach(element => {
            const functionFK = this.checkChildrenNode(element);
            if(functionFK){
              let checkNodeExist = false;
              this.dataProfilo.function.forEach(element => {
                  if(element === functionFK){
                    checkNodeExist = true;
                  }
              });
              if(!checkNodeExist){
                this.dataProfilo.function.push(functionFK);
              }
            }
      });
    } else {
      if(event.node.data.functionFK){
        let checkNodeExist = false;
        this.dataProfilo.function.forEach(element => {
          if(element === event.node.data.functionFK){
            checkNodeExist = true;
          }
     });
      if(!checkNodeExist){
        this.dataProfilo.function.push(event.node.data.functionFK);
      }
      }
    }
  }

   **OTHER METHOD!**

   nodeUnselect(event) {
        const nodes = [];
        if (event.node.children && event.node.children.length > 0) {
          event.node.children.forEach(element => {
            const functionFK = this.checkChildrenNode(element);
            if (functionFK) {
              nodes.push(functionFK);
            }
          });
    
      this.dataProfilo.function = this.dataProfilo.function.filter(val => !nodes.includes(val));
    
    } else {
      if (event.node.data.functionFK) {
        this.dataProfilo.function = this.dataProfilo.function.filter(
          elem => {
            return elem != event.node.data.functionFK
          }
        );
      }
    }
    
      }
    
      
*if childrean are checked - BOTH FATHER AND CHILDREN
  • If nodes and children exist and length’s node is longer than 0, I do a forEach/a loop to see if they are checked
checkChildrenNode(node): any {
        if (node && node.children && node.children.length > 0) {
          node.children.forEach(childNode => {
            this.checkChildrenNode(childNode);
          });
        } else {
          return node.data.functionFK;
        }
      }

  changeAssegnate(event:any){
    if(event.checked){
      this.toggle = 'assegnate';
    }else{
      this.toggle =  null;
    }
      console.log(event)

  }      
    }

This is my code. I’m using primeng – Angular.
Any ideas?

Solution

that’s the common behavior if you check primeng’s tree documentation here

Answered By – Salik Khan

Answer Checked By – Marilyn (Easybugfix Volunteer)

Leave a Reply

(*) Required, Your email will not be published