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

[Solved] Search for an item and remove the others

4 Answers 157 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
Juliano
Top achievements
Rank 1
Juliano asked on 02 Sep 2008, 02:09 PM

How can I search for an item by its text and remove all other nodes out of the hierarchy?

ex:

Computers
--- Programming
----- Web
-------- C#
----- Windows
-------- VB.Net
--- Design
----- Photoshop
----- Fireworks

If the user searches for "VB.Net" it must clean the treeview and display only:

Computers
--- Programming
----- Windows
-------- VB.Net

How can I do this?

And how can I search for nodes by using just part of the text (Wildcard search)?

4 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 03 Sep 2008, 07:39 AM
Hi Juliano,

Here is the sample code snippet for searching an item in RadTreeView and hiding the rest of the items. You can alter the flow accordingly.

CS:
protected void RadTreeView1_PreRender(object sender, EventArgs e) 
    { 
        foreach (RadTreeNode node in RadTreeView1.Nodes) 
        { 
            for (int i = 0; i < node.Nodes.Count; i++) 
            { 
                for (int j = 0; j < node.Nodes[i].Nodes.Count; j++) 
                { 
                   
                    if (node.Nodes[i].Nodes[j].Text != "My Documents") 
                    { 
                        node.Nodes[i].Nodes[j].Visible = false
 
                    } 
                } 
                
            }   
             
        } 
    } 


You can also go through the following code library submission which explains how to search for a TreeView node.
TreeNode searching

Regards
Shinu.
0
Yana
Telerik team
answered on 03 Sep 2008, 08:14 AM
Hello Juliano,

You can also remove nodes client-side like this:

<script type="text/javascript">  
    function remove()  
    {  
        var tree= $find("<%= RadTreeView1.ClientID %>");  
        var node = tree.findNodeByText("VB");  
        var parent_nodes = new Array(node);  
          
        while(node.get_parent())  
        {  
            var parent = node.get_parent();  
            parent_nodes.push(parent);  
            if(parent == tree)  
                break;  
            else   
            {  
               for(var i=0; i<parent.get_nodes().get_count(); i++)  
               {  
                    if(parent.get_nodes().getNode(i) != node)  
                    {  
                              
                        node1 = parent.get_nodes().getNode(i);  
                        tree.trackChanges();  
                        node1.get_nodes().clear();  
                        parent.get_nodes().remove(node1);  
                        tree.commitChanges();  
                   }  
                      
               }  
               node = parent;  
            }  
        }  
    }  
</script> 

Greetings,
Yana
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Juliano
Top achievements
Rank 1
answered on 03 Sep 2008, 12:21 PM
I'll try both codes.

But I have another question. When using client-script to control the RadTreeView, I get a reference to the RadTreeView as in your suggested JS code, but I can't get the complete intellisense for its object.

If I use:
var tree= $find("<%= RadTreeView1.ClientID %>");

I
can't see some functions like findNodeByText in the Intellisense drop-down.

Why?

findNodeByText doesn't seem to perform wildcard search or simply partial string search, so I had to implement my own method.

function findCompleteText(partOfTheText)  
{  
    var tree = $find("<%= RadTreeView1.ClientID %>");  
    var nodes = tree.get_allNodes();  
              
    for (var i = 0; i < nodes.length; i++)  
    {  
        var nodeText = nodes[i].get_text();  
                  
        if (nodeText.indexOf(partOfTheText) > -1)  
        {  
            return nodeText;  
        }  
    }  
 
    return "";  
0
Atanas Korchev
Telerik team
answered on 03 Sep 2008, 01:47 PM
Hi Juliano,

RadControls for ASP.NET Ajax currently do not support the JavaScript autocomplete feature of Visual Studio 2008. This is logged in our TODO list and will appear in future releases.

Regards,
Albert
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
Tags
TreeView
Asked by
Juliano
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Yana
Telerik team
Juliano
Top achievements
Rank 1
Atanas Korchev
Telerik team
Share this question
or