TreeView with checkboxes - independent parent and child nodes

Posted by: ECHRLicences on 15 December 2020, 9:53 pm EST

  • Posted 15 December 2020, 9:53 pm EST

    Hi,

    I am using the TreeView control in an ASP.NET Core Project.
    I need to have checkboxes to enable selection of one or many nodes.

    I have followed instructions here: https://www.grapecity.com/componentone/docs/mvc/online-mvc-core/UsingCheckBoxes.html and got the tree view to display properly with checkboxes but I need to have different behaviour regarding the parent / child nodes automatic selection.

    The parent nodes needs to be "checkable" on their own, even if not child node is selected.

    Basically the easiest way would be to disable the "auto checking" relationship between parent and child nodes, and let each node be checked separately. Is there any way to achieve this?

    As an example, if I take the link provided below (https://www.grapecity.com/componentone/docs/mvc/online-mvc-core/UsingCheckBoxes.html), I would like to be able to select "Electronics" without any children for example.


    Thanks for your help
  • Marked as Answer

    Replied 16 December 2020, 7:07 pm EST

    Hi,
    For this, you need to remove the checkbox setting for TreeView and add checkbox by your own using the formatItem event.
    tree.formatItem.addHandler(function (s, e) {
    if (e.dataItem.NewItem) {
    var imgUrl = newImageUrl;
    e.element.innerHTML ={html}
    }
    });


    Now, you need to handle the mousedown event for TreeView and check if the target element is check box or not. If yes, you may perform the action by code what you would like to perform.

    Regards,
    Manish Gupta
  • Replied 13 January 2021, 8:32 pm EST

    Hi,

    Thanks for your reply, I was indeed able to achieve it via formatItem to create the Checkboxes and then handling the onClick event of those checkboxes.

    Some other components providers support this out-of-the-box (like https://docs.telerik.com/aspnet-core/html-helpers/navigation/treeview/checkboxes, see CheckChildren) so it would be nice to add it for a future release maybe ?

    Thanks again for your help
  • Replied 14 January 2021, 2:36 pm EST

    Hi,
    Thank you for notifying me that your issue has been resolved using the provided workaround.
    We have forwarded this request to the concerned team with the internal ID 470598.

    Regards,
    Manish Gupta
Need extra support?

Upgrade your support plan and get personal unlimited phone support with our customer engagement team

Learn More

Forum Channels