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

Scroll node to middle of tree

2 Answers 93 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
Nir
Top achievements
Rank 1
Nir asked on 03 Dec 2008, 04:40 PM
Hi,

I have a tree with many (more than 1000) nodes.

After finding a node to select I want it to be scrolled to the middle of the tree visible section (when possible...).
I am using scrollIntoView - but this only gets the node to the bottom section of the tree.

Is  there a way to set the tree's scrollTop attribute so that the node will be in the middle of the visible tree?

Thanks, Nir.


2 Answers, 1 is accepted

Sort by
0
Atanas Korchev
Telerik team
answered on 04 Dec 2008, 03:52 PM
Hello Nir,

There is no built-in way do that. As you have noticed the scrollIntoView method scrolls the node to the bottom of the treeview. You can modify the scrollTop of the treeview div to tweak the scroll position:

var treeViewElement = treeView.get_element();
treeViewElement.scrollTop = 200;

Regards,
Albert
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Nir
Top achievements
Rank 1
answered on 08 Dec 2008, 01:27 PM
Hi,

I added the following functions to use when I want to scroll the node to the middle of the tree:
function ScrollSelectedToMiddle(node)  
    var tree = node.get_treeView(); 
    var oDomTree = tree.get_element(); 
    oDomTree.scrollTop = 0; 
    node.scrollIntoView(); 
    var iScrollTop = oDomTree.scrollTop; 
    var posY  = findPosY(node.get_element()); 
    var iHalfOfScreen = parseInt(oDomTree.offsetHeight/2); 
    if (iScrollTop == 0) { 
       oDomTree.scrollTop = parseInt(posY/2); 
    } else { 
        oDomTree.scrollTop += iHalfOfScreen; 
    } 
 
function findPosY(obj)  
    var curtop = 0; 
    if (obj.offsetParent) { 
        while (obj.offsetParent) { 
            curtop += obj.offsetTop 
            obj = obj.offsetParent; 
        } 
    } else if (obj.y) 
        curtop += obj.y; 
    return curtop; 
I works well for me.
Tags
TreeView
Asked by
Nir
Top achievements
Rank 1
Answers by
Atanas Korchev
Telerik team
Nir
Top achievements
Rank 1
Share this question
or