This is a migrated thread and some comments may be shown as answers.

OnClientNodeChecking

1 Answer 17 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
Christian
Top achievements
Rank 1
Christian asked on 19 Mar 2013, 02:11 PM
Hi,

I have an TreeView with CheckChildNodes=true. Also I have add the client event ClientNodeChecking.

I expect if I click a node the client event ClientNodeChecking is fired for the clicked node and for all child nodes. Unfortunaltly it is only fired for the clicked node! Why and how can I solved this?

Background is that i must check the count of all checked nodes and only allowed a maximum of 10.

function FnTreeView_ClientNodeChecking(sender, args)
{
    if (!args.get_node().get_checked())
    {
        var selectedNodesCount = sender.get_checkedNodes().length;
 
        if (selectedNodesCount > 10)
        {
            args.set_cancel(true);
        }
    }
}


Thanks for help,

Kind regards,
Christian

1 Answer, 1 is accepted

Sort by
0
Bozhidar
Telerik team
answered on 20 Mar 2013, 07:57 AM
Hello Christian,

The OnClientNodeChecking event fires as a result of an user interaction. It's fired for the node that's being clicked with the mouse (or by pressing space). Since the check child nodes functionality uses javascript to manually check the child nodes after that, the event is not raised again. It the same as if you use the following code to check a node:
$find("RadTreeView1").get_nodes().getNode(0).check()

In your case you can use the following code to cancel the check if the resulting amount of checked nodes is bigger than 10:
function OnClientNodeChecking(sender, args)
{
    if (!args.get_node().get_checked())
    {
        var selectedNodesCount = sender.get_checkedNodes().length;
        var childNodes = args.get_node().get_allNodes();
        var childNodesCount = childNodes.length;
 
        for(var i = 0; i < childNodesCount; i++) {
            if (childNodes[i].get_checked()) {
                selectedNodesCount--;
            }
        }
 
        selectedNodesCount += childNodesCount;
  
        if (selectedNodesCount > 10)
        {
            args.set_cancel(true);
        }
    }
}

If you want to check child nodes until the total number reaches 10, I suggest you implement this functionality manually, instead of using the CheckChildNodes feature.
 

Kind regards,
Bozhidar
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
Tags
TreeView
Asked by
Christian
Top achievements
Rank 1
Answers by
Bozhidar
Telerik team
Share this question
or