angular-tree-component icon indicating copy to clipboard operation
angular-tree-component copied to clipboard

tri-state checkboxes

Open Freest10 opened this issue 10 years ago • 18 comments

Hello, are you planning "tri-state checkboxes"? I am very need in this in my project) tree_check

Freest10 avatar Oct 18 '16 12:10 Freest10

Sometime in the future yes, but don't know when. It is completely possible to implement this yourself using angular2-tree-component BTW

adamkleingit avatar Oct 19 '16 13:10 adamkleingit

Simply add a checkbox in the template. Then, you need to add the selection logic.

Using the events, you can bind the onActivate and onDeactivate events to check and uncheck the checkboxes.

popojargo avatar Dec 17 '16 05:12 popojargo

Please let us know if you are planning to add this out of the box. If not, can you please provide a sample template which includes html structure. Do I need to include the checkbox inside TreeNodeContent?

debslab avatar Feb 06 '17 15:02 debslab

Hi,

I don't have any schedule for this yet. I'm currently solving performance issues for large trees using virtual scroll and MobX.

MobX is a great way to do tri-state checkboxes, so I plan to use it for this feature as well.

You can override either the treeNodeTemplate or the treeNodeFullTemplate, please refer to the docs.

Thanks

adamkleingit avatar Feb 09 '17 10:02 adamkleingit

Thank you. I am trying following template.

<Tree [nodes]="nodes"> <template #treeNodeTemplate let-node="node" let-index="index"> <input (click)="go(tree, node, $event)" type="checkbox" #Nodecheck> {{ node.data.name }}

Now in the function go, I need to get access to all child nodes checkbox so that I can check them. Please let me know how I can achieve it.

debslab avatar Feb 15 '17 01:02 debslab

Hi..any update on my previous comment...it's an awesome component to use in my project..only one thing which is bugging me is not having checkbox support...if you provide some pointer to how we shall do it, it will be highly appreciated...

debslab avatar Feb 16 '17 15:02 debslab

Hi,

you can you node.getVisibleChildren() to access all children that are visible, or just node.children if you are not doing any filtering on the tree. Also - it's better to use (change) and not (click) on checkboxes, and you must also use [checked] to bind to the checked property of the checkbox.

All the best

--

On Thu, Feb 16, 2017 at 5:30 PM, debslab [email protected] wrote:

Hi..any update on my previous comment...it's an awesome component to use in my project..only one thing which is bugging me is not having checknox support...if you provide some pointer to how we shall do it, it will be highly appreciated...

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/500tech/angular2-tree-component/issues/83#issuecomment-280362938, or mute the thread https://github.com/notifications/unsubscribe-auth/AA2SSvzzZRDImJJZZu4Nrl-tle1PmyZsks5rdGumgaJpZM4KZwFd .

adamkleingit avatar Feb 18 '17 14:02 adamkleingit

Thanks a lot. I have done the implementation as following. // html <Tree [nodes]="groups" #tree [options]="treeOptions" (onActivate)="selectNode($event)"> <template #treeNodeTemplate let-node="node" let-index="index"> <input (change)="go(tree, node, $event,nodeEdit)" type="checkbox" id="check-{{node.data.id}}"> {{ node.data.name }} // code executed if we click on any checkbox public updateChildNodesCheckBox(node,checked) { if (node) { var inputValue:HTMLInputElement = document.getElementById('check-' + node.id); if(inputValue) { inputValue.checked = checked; } for (let i = 0; i < node.children.length; i ++) { this.updateChildNodesCheckBox(node[i], checked); } }

}

But when I access child nodes, if the node is collapsed, I cannot get hold of the child nodes as they are not there in dom. A workaround is to check or uncheck those child checkbox when the node is expanded. Please let me know if I am missing something here.

debslab avatar Feb 22 '17 00:02 debslab

Actually in onToggleExpanded also, I am not getting hold of child node as it's yet to be rendered. So I am not able to do the workaround I mentioned earlier. Please help

debslab avatar Feb 22 '17 01:02 debslab

If I get an event after expand, that will suffice as that time child nodes will be available to me

debslab avatar Feb 22 '17 03:02 debslab

Hey,

you shouldn't update DOM elements directly. Angular provides data-binding for that. You should update a checked property on the nodes themselves and bind to it using [checked] property. something like:

<input ... [checked]="node.data.checked">

updateChildNodesCheckBox(node) {
  node.checked = true;
  if (node.children) {
    node.children.forEach((child) => updateChildNodesCheckBox(child));
  }
}

adamkleingit avatar Feb 22 '17 08:02 adamkleingit

Thank you very much. Finally I am able to implement this. For setting child nodes checkbox, I followed your approach. But for parent nodes I needed to access DOM elements directly. I needed to set the checkbox of parent to indeterminate state which, I guess, is not possible without accessing DOM element. Using checked property of angular, I can select or unselect a checkbox, but unable to set indeterminate state.

debslab avatar Feb 22 '17 17:02 debslab

Logic I have written to set parent node checkbox-

public updateParentNodesCheckBox(node) {
    if (node && node.level > 0 && node.children) {
        let allChildChecked = true;
        let noChildChecked = true;
        for (let child of node.children) {
            if (!child.data.checked) {
                allChildChecked = false;
            } else if (child.data.checked) {
                noChildChecked = false;
            }
        }
        let inputValue: HTMLInputElement = <HTMLInputElement> document.getElementById('check-' + node.id);
        if (allChildChecked) {
            node.data.checked = true;
            inputValue.indeterminate = false;
        } else if (noChildChecked) {
            node.data.checked = false;
            inputValue.indeterminate = false;
        } else {
            node.data.checked = true;
            inputValue.indeterminate = true;
        }
        this.updateParentNodesCheckBox(node.parent);
    }

}

debslab avatar Feb 22 '17 17:02 debslab

Hey, You should be able to use [indeterminate] as well in the template. Angular allows to bind to all properties of DOMNodes.

adamkleingit avatar Feb 26 '17 17:02 adamkleingit

thank you. I updated my code.

debslab avatar Feb 27 '17 02:02 debslab

do we have any official documentation on this feature ?

touqeershafi avatar Jul 11 '17 15:07 touqeershafi

here you go - https://angular2-tree.readme.io/docs/tri-state-checkboxes

debslab avatar Jul 12 '17 04:07 debslab

Hello, i am using the same code but the indeterminate is not working on save Any help?

kmuseligadoo avatar Apr 16 '20 15:04 kmuseligadoo