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

[Solved] Ignore OnClientNodeClicked while ctrl is pressed

4 Answers 170 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
Mathew
Top achievements
Rank 1
Mathew asked on 15 Feb 2008, 08:22 AM
Hi,

I'm using a RadTreeView (in Prometheus v. 2007.3.1314.20) for both site navigation and administration. When I click on a node the browser redirects to the page it represents, and when I drag a node elsewhere, it moves the page it represents to that new logical location. This works perfectly at the moment for single nodes.

I need the ability to move multiple nodes at once. I can do this already if I select the nodes via checkboxes and then drag them to their new location, but I don't like having the checkboxes enabled. I prefer the method of holding the CTRL key while clicking the nodes, and then dragging them.

The problem is that when I click on a node - even if I'm holding CTRL down - the page fires the javascript that I specify in OnClientNodeClicked. This javascript redirects the browser to another page. I want some way of preventing the redirect from happening if I'm holding down CTRL.

Here is the javascript called via OnClientNodeClicked:

function loadNode(sender, eventArgs) 
    document.location.href = "index.aspx?id="+ node.get_value(); 

It'd be really nice if the TreeView could just be set to ignore OnClientNodeClicked if CTRL is being held down, but another solution could just be for me to check if CTRL was being held down in the loadNode function itself. However, I can't find any keydown information in the eventArgs. Do you have any suggestions on how I can proceed?

Many thanks for a great product!

4 Answers, 1 is accepted

Sort by
0
Fernandes
Top achievements
Rank 1
answered on 15 Feb 2008, 06:55 PM
Hi Mathew!


Have you tried this approach?

    <script language="javascript" type="text/javascript">  
     
    function loadNode(sender, eventArgs)    
    {    
        if(!event.ctrlKey)  
        {  
            document.location.href = "index.aspx?id="+ node.get_value();    
        }    
    }    
      
    </script> 

Hope it helps!

Fernandes

PS: I forgot to mention that this may work only in IE, but similar approaches can be taken on other browsers. :)
0
Accepted
Mathew
Top achievements
Rank 1
answered on 18 Feb 2008, 12:22 AM
Thanks Fernandes, I had previously tried that in Firefox but because it adheres to the standards it doesn't allow a global event variable. This is great for standards, but not so great when the radtreeview doesn't pass the full event into the handler JS.

As a workaround I've used this to set a global 'controlKey' boolean when ctrl goes up and down, and I check it before redirecting. I'll paste it here in case others find it useful. It works in IE and FF on Windows, but I haven't tested other platforms.

document.onkeydown = checkKey; 
document.onkeyup = release; 
var controlKey = false
 
function release() 
    controlKey = false
 
function checkKey(e) 
    var k = e || window.event; 
    if(k.keyCode) 
    { 
        keycode = k.keyCode; 
    } 
    else if(k.which) 
    { 
        keycode = k.which; 
    } 
    if(keycode == 17) 
    { 
        controlKey = true 
    } 
 
function loadNode(sender, eventArgs) 
    if (!controlKey) 
    { 
        document.location.href = "index.asp?id="+ eventArgs.get_node().get_value(); 
    } 

0
Simon
Telerik team
answered on 18 Feb 2008, 11:20 AM
Hello Mathew,

The code is fine and should work on every platform.

However, it would be better if you declare the 'keycode' variable locally in the function, let's say before the if-clause:

... 
var k = e || window.event; 
var keycode; 
 
if(k.keyCode) 
..... 

Best wishes,
Simon
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center
0
Mathew
Top achievements
Rank 1
answered on 18 Feb 2008, 10:28 PM
Oh, yes. Thanks for pointing it out.
Tags
TreeView
Asked by
Mathew
Top achievements
Rank 1
Answers by
Fernandes
Top achievements
Rank 1
Mathew
Top achievements
Rank 1
Simon
Telerik team
Share this question
or