javascripttypescriptprimengangular18primeng-tree

Primeng P-tree checkboxes aren't checkable


I have a simple p-tree populated with checkboxes.

I'm able to render it okay - but clicking on the checkboxes doesn't actually make them appear as checked.

Even if I do something as simple as this in the ts file and HTML file, respectively:

ts file:

    myObjectNodes: TreeNode[] = [];
    mySelectedObjectNodes: TreeNode[] = [];

    ngOnInit() {

      //The issue is that the nodes also need a key value
      const checkbox1 = {
        label:"checkbox 1"
      };

      const checkbox2 = {
        label:"checkbox 2"
      };

      this.myObjectNodes.push(checkbox1);
      this.myObjectNodes.push(checkbox2);

      this.mySelectedObjectNodes.push(checkbox1);
    }

HTML file:

    <p-tree [value]="myObjectNodes" 
              selectionMode="checkbox"
              [(selection)]="mySelectedObjectNodes">

It renders fine - note that the first checkbox is checked upon loading, as it should be since it was added to mySelectedObjectNodes Upon Load

Once it loads, however, the user can unselect the checked checkbox - but after that it's useless. Neither checkboxes can be selected...

After the user unselects the selected checkbox - neither can be selected.

No matter how many times you click either checkbox - they never appear as selected.

enter image description here

Any suggestions?


Solution

  • You need to have a selectedNodes array and it should work:

    
    <p-tree [value]="myObjectNodes" selectionMode="checkbox" [(selection)]="selectedObjectNodes" />