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

[Solved] Treeview state cookies not working

2 Answers 51 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.
Doug R
Top achievements
Rank 1
Doug R asked on 17 Feb 2012, 01:19 AM
Hi, I have followed the example here:
http://www.telerik.com/support/kb/aspnet-mvc/treeview/persisting-treeview-state-in-cookie.aspx
In order to persist the expanded state between post backs.
Here is my code:

@( Html.Telerik().TreeView()
    .Name("ApplicationTreeView")
 
    .BindTo(Model, mappings =>
    {
        mappings.For<AspNet_Applications>(binding => binding
                .ItemDataBound((item, app) =>
                {
                    //item.Text = String.Format("{0}", app.ApplicationName);
                    item.Text = app.ApplicationName;
                    //item.Text = String.Format("{0} ({1})", app.ApplicationName, app.RoleCount);
                    item.ImageUrl = Url.Content("~/Content/images/mailfolder.gif");
                    item.Expanded = ((string[])ViewData["ExpandedNodes"]).Contains(item.Text);
                    item.Action("Index", "Admin", new {app = app.ApplicationId});
                })
                .Children(group => ((IEnumerable<object>) group.DistributionLists).Union ((IEnumerable<object>)group.Roles))
                );
        mappings.For<AspNet_Roles>(binding => binding
                .ItemDataBound((item, role) =>
                {
                    item.Text = String.Format("{0} ({1})", role.RoleName, role.UserCount);
                    item.ImageUrl = Url.Content("~/Content/images/mailOutbox.gif");
                    item.Action("Index", "Admin", new { role = role.RoleId });
                }).Children(role => role.ChildRoles)
                );
        mappings.For<DistributionLists>(binding => binding
                .ItemDataBound((item, dl) =>
                {
                    item.Text = String.Format("{0}", dl.DistributionListName);
                    item.ImageUrl = Url.Content("~/Content/images/mailPersonalFolders.gif");
                    item.Action("Index", "Admin", new {dl = dl.DistributionListID});
                })
                );            
    })
    .ClientEvents(events => events
            .OnCollapse("updateTreeViewState")
            .OnExpand("updateTreeViewState")
    )
      )

The problem is that the script: $(e.item).text()
returns the expanded node and all the text for all its children. So it returns this:
"parentchild1child2"
instead of:
"parent"
any ideas what I am missing?
Thanks

2 Answers, 1 is accepted

Sort by
0
Doug R
Top achievements
Rank 1
answered on 17 Feb 2012, 06:12 PM
just to be more clear, it is the javascript call to:

$(e.item).text()

that is producing the wrong text in the below codew

<script type="text/javascript">
    function updateTreeViewState(e) {
        var expandedNodes = $.cookie('ExpandedNodes');
        expandedNodes = expandedNodes ? expandedNodes.split(';') : [];
          
        var itemIndex = $.inArray($(e.item).text(), expandedNodes);
          
        if (e.type == "expand" && itemIndex == -1)
            expandedNodes.push($(e.item).text());
        else if (e.type == "collapse" && itemIndex >= 0)
            expandedNodes.splice(itemIndex, 1);
          
        $.cookie('ExpandedNodes', expandedNodes.join(';'));
    }
</script>
0
Darren
Top achievements
Rank 1
answered on 09 May 2012, 02:42 PM
Hi Doug, 

I had the same problem for a long time but I think I have solved it. The problem was exactly as you described below so by removing $(e.item).text() and using treeView.getItemText(e.item) I got the expanded node to save without the associated children. See code for more details, let me know if it works for you or if you have any questions.

Darren

function updateTreeViewState(e) {
            var expandedNodes = $.cookie('ExpandedNodes');
            expandedNodes = expandedNodes ? expandedNodes.split(',') : [];
            var treeView = $("#TreeView").data("tTreeView");
            var itemIndex = $.inArray(treeView.getItemText(e.item), expandedNodes);                      
            if (e.type == "expand" && itemIndex == -1) {
                expandedNodes.push(treeView.getItemText(e.item));
            }
            else if (e.type == "collapse" && itemIndex >= 0) {
                expandedNodes.splice(itemIndex, 1);
            }
            $.cookie('ExpandedNodes', expandedNodes.join());
        }
Tags
TreeView
Asked by
Doug R
Top achievements
Rank 1
Answers by
Doug R
Top achievements
Rank 1
Darren
Top achievements
Rank 1
Share this question
or