https://i.sstatic.net/KM8Fo.png
This code is written in Typescript. //DECLARATIONS AND 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)=> {
});
}
}
Check if nodes are selected
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 nodes 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 children are checked - BOTH FATHER AND CHILDREN
- If nodes and children exist and the length of the node is longer than 0, I iterate through them 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 with Angular. Any thoughts?