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

Expand a branch to one particular node

4 Answers 204 Views
TreeList
This is a migrated thread and some comments may be shown as answers.
Paul
Top achievements
Rank 1
Paul asked on 06 Oct 2011, 07:06 PM

As part on the functionality I am trying to include in my web application, is the ability to “search” the TreeList. I have a field selector drop down list and a text box or check box to enter the search criteria. The search button performs a post back where I then location a match in the data source. Now that I have the matching record, how can I force it to be displayed selected? The result of the search may be a child node somewhere down the hierarchy not currently being displayed. If this is the case I would want the branch with the matching criteria to be expanded to this item. Then I would want to select the item. Does anyone have any ideas on how to force a node to be displayed?

I have not looked into using ExpandItemToLevel since it is liable to expose too many items. I don’t think it would work for one level at a time since I might be calling it for items not displayed, recursive calls.

4 Answers, 1 is accepted

Sort by
0
Accepted
Veli
Telerik team
answered on 11 Oct 2011, 09:44 AM
Hello Paul,

RadGrid does not currently support expanding to a particular item specified by a key value or any other data. Only expanding to level is supported, which expands all items up to a particular level. RadTreeList does not keep reference to TreeListDataItems that are not expanded. Thus, the items you see in the treelist (expanded items and their children) are the only items RadTreeList maintains. You cannot find an item that is not expanded, because it doesn't exist.

However, you can implement a solution for this scenario. If you expand all items first, you can find an item by its data key value. This is made possible through the ExpandAllItems and CollapseAllItems methods in RadTreeList. Using these methods, we can expand all items, find the relevant item and collapse back the treelist, keeping the previously expanded indexes, as well as any parent item of the found item. Here is a sample method that searches for and selectes a data item by specified ID key value:

private void FindAndSelectItem(int id)
{
    //first check if the item is not already in the Items collection
    foreach (var item in RadTreeList1.Items)
    {
        if ((int)item.GetDataKeyValue("ID") == id)
        {
            item.Selected = true;
            return;
        }
    }
         
    //save the previously expanded indexes
    var expandedIndexes = new TreeListHierarchyIndex[RadTreeList1.ExpandedIndexes.Count];
    RadTreeList1.ExpandedIndexes.CopyTo(expandedIndexes);
 
    //add newly expanded indexes here
    var newIndexes = new List<TreeListHierarchyIndex>();
 
    //cause all items to expand to reveal all data
    RadTreeList1.ExpandAllItems();
 
    //loop through all the items and search for the target by its key value
    foreach (var item in RadTreeList1.Items)
    {
        if ((int)item.GetDataKeyValue("ID") == id)
        {
            //select the item
            item.Selected = true;
                 
            //expand all parents of the item
            var parent = item.ParentItem;
            while (parent != null)
            {
                newIndexes.Add(parent.HierarchyIndex);
                parent = parent.ParentItem;
            }
        }
    }
 
    //shrink back the treelist
    RadTreeList1.CollapseAllItems();
 
    //restore the previously expanded indexes
    RadTreeList1.ExpandedIndexes.AddRange(expandedIndexes);
 
    //add the newly expanded indexes that will reveal the selected item
    foreach (var index in newIndexes)
    {
        if (!RadTreeList1.ExpandedIndexes.Contains(index))
        {
            RadTreeList1.ExpandedIndexes.Add(index);
        }
    }
 
    //rebind to reflect the changes
    RadTreeList1.Rebind();
}

Attaching a test page to demonstrate this scenario.

Veli
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
0
Paul
Top achievements
Rank 1
answered on 11 Oct 2011, 05:22 PM
Thanks, I tried it in my code with only a minor change, field name, and it worked. I'm new to the Telerik controls and this demonstrates a better method of performing some of the task I already coded. I guess I will be doing some recoding.
0
Jo A. Kim
Top achievements
Rank 1
answered on 17 Mar 2012, 09:17 AM
Hallo Veli,

I am evaluating telerik controls.

I have some questions to this topic:

  1. How can I search for the display text?
    I dit it on this way, is it the right one or is there a better solution?
    //first check if the item is not already in the Items collection
                foreach (var item in RadTreeList1.Items)
                {
                    foreach (TableCell cel in item.Cells)
                    {
                        if (cel.Text == itemtosearch)
                        {
                            item.Selected = true;
                            return;
                        }   

                    }
                }
  2. If the RadTreeList has multipages, it does not select the page where the itemtosearch where found. How can I select the right page?
  3. If I get multiple results, I want to move (select) the next result. How can I search for next result?

Thank You in Advance
Jo A. Kim

0
Tsvetoslav
Telerik team
answered on 22 Mar 2012, 08:41 AM

Hello Jo A. Kim,

Straight to your questions.

1. Your approach is correct and viable.

2. When the tree list has multiple pages, you should:
 
2.1. set AllowPaging = false
2.2. call the tree list's Rebind() method
2.3. search for the item.
2.4. determine what page it lies on given the total items count and the page size.
2.5. set the tree list's CurrentPageIndex to the page number determined in step 4.
2.6. once again rebind the tree list.

3. Find all items containing your search criteria, determine all the information you need to select the item (indexes to expand, page index, data key value), persist them in a custom object of yours and serialize it in ViewState or SessionState. Then on the next search, just pull it from there, expand the items by the expand indexes persisted and select the item containing the text.


Hope it helps.



Regards,

Tsvetoslav
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
TreeList
Asked by
Paul
Top achievements
Rank 1
Answers by
Veli
Telerik team
Paul
Top achievements
Rank 1
Jo A. Kim
Top achievements
Rank 1
Tsvetoslav
Telerik team
Share this question
or