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

[Solved] two treeviews on one page, one on partial view

1 Answer 84 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.
Manish
Top achievements
Rank 1
Manish asked on 10 Apr 2012, 04:01 AM

Telerik's TreeView control, although fancy and with a lot of functionality is proving to be a nightmare for me now. However I could not find a better TreeView control which supports collapsing/expanding etc. and has a little bit of documentation. Here's my problem.

This is the site master : _Layout.cshtml

<!DOCTYPE HTML>
<html>
<head>
   
<title>@ViewBag.Title</title>
   
<!--
    <link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" />
    <link href="@Url.Content("~/Content/custom.css")" rel="stylesheet" type="text/css" />-->

    @(Html.Telerik().StyleSheetRegistrar()
                    .DefaultGroup(group =>
                        group.Add("telerik.common.css")
                             .Add("~/Content/Site.css")
                             .Add("~/Content/custom.css")
                             .Add("~/Content/themes/humanity/jquery-ui-1.8.18.custom.css")
                    .Combined(true)
                    .Compress(true)))
</head>
<body>
   
<div class="page">
       
<header>
           
<div id="header-logo">
                   
<img alt="X" src="../../Content/Images/logo100.png"/>
           
</div>
           
<div id="title">
               
<h1>title</h1>
           
</div>

            @(Html.Telerik().Menu()
                    .Name("menu")
                    .Items(menu =>
                    {
                        menu.Add().Text("Home").Action("Index", "Home");
                        menu.Add().Text("Products")
                            .Items(item =>
                                       {
                                           item.Add().Text("xxx").Action("xxx", "Products");
                                           item.Add().Text("xxx").Action("xxx", "Products");
                                           item.Add().Text("xxx").Action("xxx", "Products");
                                           item.Add().Text("xxx").Action("xxx", "Products");
                                       });
                        menu.Add().Text("Events").Action("Index", "Events");
                        menu.Add().Text("About Us").Action("About", "Home");
                        menu.Add().Text("Admin").Action("Index", "Admin");
                    })
                    .HtmlAttributes(new { style = "text-align:left;" }))
       
</header>
       
<section id="main">
            @RenderBody()
       
</section>
       
<footer>
       
</footer>
   
</div>
    @(Html.Telerik().ScriptRegistrar()
                        .Scripts(script=>script.Add("~/Scripts/custom.js"))
                        .Globalization(true)
                        .DefaultGroup(group => group.Combined(true)
                                                    .Compress(true)))
</body>
</html>

This is the base page view : Index.cshtml

@{
   
ViewBag.Title = "Admin";
}
<h2>
   
Admin Index</h2>
<div id="navAdminLeft">
    @(Html.Telerik().TreeView()
    .Name("AdminNavTree")
    .Items(item =>
               {
                   item.Add().Text("Products");
                   item.Add().Text("Categories");
                   item.Add().Text("Materials");
                   item.Add().Text("Finishes");
                   item.Add().Text("Packaging");
                   item.Add().Text("File Manager");
                   item.Add().Text("CategoryTree");
               })
        .ClientEvents(events => events.OnSelect("loadAdminContent"))
           )
</
div>
<div id="adminContent">
</div>
<script type="text/
javascript">
    function loadAdminContent(e) {
        $.ajax({
            type: "
GET",
            cache: false,
            url: "
Admin/" + e.item.innerText,
            data: {},
            success: function (data) {
                $("
#adminContent").html(data);
           
}
       
});
   
}
</script>

whenever the user selects a node on the #AdminNavTree the page fetches the content to be displayed and populates #adminContent. I am trying to build an edit page where there is a category tree listed in one div and when the user clicks on any of the category on that div, the details are ajax loaded into another div on the same page. Here is the partial view with the category tree that gets loaded into#adminContent: _CategoryTree.cshtml

@using xxx.Models.Navigation
@model IEnumerable<NavigationItem>

<div id="categoryTree">      
 
@{Html.Telerik().TreeView()
   
.Name("ctree")
       
.BindTo(Model, mappings =>
            mappings
.For<NavigationItem>(binding => binding
                   
.ItemDataBound((currentItem, navigationItem) =>
                       
{
                            currentItem
.Text = navigationItem.Text;
                            currentItem
.Expanded = true;
                                                       
})
                   
.Children(child => child.Items)

               
))
   
.ClientEvents(events=>events.OnSelect("loadContent"))                                  
   
.Render();
}
</div>
<div id="detail"></
div>
<script type="text/javascript">
   
function loadCategoryDetail(e) {
        $
.ajax({
            type
: "POST",
            url
: "Admin/CategoryDetails",
            data
: { id: $("#ctree").data("tTreeView").getItemText(e.item) },
            success
: function (data) {
                $
("#detail").html(data);
           
}
       
});
   
}
</script>

the detail page that is ajax loaded does not contain any telerik controls so listing code for that is a non issue.

The PROBLEM : this will not render. As soon as it tries rendering #ctree treeview it starts throwing all kind of script errors, specifically it ignores loadCategories (says not found). So far I think what is happening is the ajax loaded partial page that contains the treeview is reloading the jquery lib destroying all old bindings and functions in the script. Is there a way to make this work, two treeviews on one page, one previously loaded, one in a ajax loaded partial view, both binding to separate data?

1 Answer, 1 is accepted

Sort by
0
BRUNO
Top achievements
Rank 1
answered on 13 Apr 2012, 08:35 PM
I'm also having problem when two or more treeview's is used in the same partial view.

In my case one treeview show checkbox and not implement DragAndDrop function, the secound treeview dont show checkbox and implement DragAndDrop.
Tags
TreeView
Asked by
Manish
Top achievements
Rank 1
Answers by
BRUNO
Top achievements
Rank 1
Share this question
or