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

[Solved] Hierarchical binding on self referencing table

6 Answers 306 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Pierre-Olivier Grangaud
Top achievements
Rank 2
Pierre-Olivier Grangaud asked on 26 Apr 2010, 04:50 PM
Hi,

I have a self referencing table with 3 columns : function_id, function_parent_id, function_name (function_parent_id is null for the root function, others contain the id of their parent).
I would like to bind it server side to the treeview. I almost found the right code to do it using the nice sample provided here http://www.telerik.com/community/forums/aspnet-mvc/treeview/how-to-get-child-checkboxes-selected-when-parent-checkbox-is-selected.aspx#1141070 but I don't know the code I need to write in the Children method to finish it.

<% Html.Telerik().TreeView() 
            .Name("TreeView") 
            .ShowCheckBox(true) 
            .ExpandAll(true) 
            .BindTo(Model as IEnumerable<Function>mappings => 
            { 
                mappings.For<Function>(binding => binding 
                        .ItemDataBound((item, function) => 
                        { 
                            item.Text = function.function_name; 
                            item.Value = function.function_id.ToString() ; 
                        }) 
                        .Children(child => child.Functions)); 
            }) 
            .Render(); 
        %> 

The issue is that I want only function that have parent id NULL on the first level. On this solution all the functions are displayed on the root level.

How can I sort that ?
Best regards,


6 Answers, 1 is accepted

Sort by
0
Atanas Korchev
Telerik team
answered on 26 Apr 2010, 04:55 PM
Hello Pierre-Olivier Grangaud ,

The checkboxes example shows binding to self-referencing hierarchy (the Employees table from the Northwind database).

I hope this helps,
Atanas Korchev
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
Pierre-Olivier Grangaud
Top achievements
Rank 2
answered on 27 Apr 2010, 11:10 AM
Hi Atanas,

Thank you for you reply, unfortunately it doesn't help. I copy/pasted the code from the demo and I have the same behavior than before, all elements appears at the root level (and of course at their right place in the hierarchy). See the screenshot in attachment.

I'm using MVC 1 with Telerik.Web.Mvc.dll -> 2010.1.309.135. 

Best regards,

0
Atanas Korchev
Telerik team
answered on 27 Apr 2010, 12:19 PM
Hello Pierre-Olivier Grangaud ,

The code from the demo is setup to bind the treeview to the root level employees. I suggest you do the same in your case.

Regards,
Atanas Korchev
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
Pierre-Olivier Grangaud
Top achievements
Rank 2
answered on 27 Apr 2010, 01:53 PM
Hi Atanas,

As I said I copy pasted the code from the demo (I removed the box checking condition because I don't need it right now), here is the code :

<% Html.Telerik().TreeView() 
    .Name("TreeView") 
    .ShowCheckBox(true) 
    .ExpandAll(true) 
    .BindTo(Model as IEnumerable<Employee>mappings => 
    { 
        mappings.For<Employee>(binding => binding 
                .ItemDataBound((item, employee) => 
                { 
                    item.Text = employee.FirstName + " " + employee.LastName; 
                    item.Value = employee.EmployeeID.ToString(); 
 
                    item.Expanded = true
                }) 
                .Children(category => category.Employees)); 
 
        mappings.For<Employee>(binding => binding 
                .ItemDataBound((item, employee) => 
                { 
                    item.Text = employee.FirstName + " " + employee.LastName; 
                    item.Value = employee.EmployeeID.ToString(); 
                })); 
    }) 
    .Render(); 
%> 

The data comes frome the Northwind database, data access made with LINQ to SQL.

Is there anything wrong or did I miss something ? Is there is more code required to do this binding to achieve the same result of the demo ? 

I'm using MVC 1 with Telerik.Web.Mvc.dll -> 2010.1.309.135. 

Best regards,
0
Accepted
Atanas Korchev
Telerik team
answered on 27 Apr 2010, 02:50 PM
Hi Pierre-Olivier Grangaud ,

The important bit is in the controller. The treeview is bound to the root customers only:

        public ActionResult CheckBoxSupport()
        {
            return View(GetRootEmployees());
        }
And here is the implementation of GetRootEmployees:

        private static IEnumerable<Employee> GetRootEmployees()
        {
            NorthwindDataContext northwind = new NorthwindDataContext();
            
            return northwind.Employees.Where(e => e.ReportsTo == null);
        }

The trick is that only root records are returned. In your case you should make sure the treeview is bound to a collection with root records.

Greetings,
Atanas Korchev
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
Pierre-Olivier Grangaud
Top achievements
Rank 2
answered on 27 Apr 2010, 03:01 PM
Hi,

Oh right didn't noticed the name of the function used to get the data in the demo :x. It make sense now ...

Thank you for pointing it out !

Best regards,
Tags
TreeView
Asked by
Pierre-Olivier Grangaud
Top achievements
Rank 2
Answers by
Atanas Korchev
Telerik team
Pierre-Olivier Grangaud
Top achievements
Rank 2
Share this question
or