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

[Solved] Get Value of child items

2 Answers 140 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.
Roy Halvorsen
Top achievements
Rank 1
Roy Halvorsen asked on 25 May 2011, 09:59 AM
I am using a TreeView with checkbox support. But, I can nor find out how to get the value of the child items. When I click a parent item, all child items is checked, and when I uncheck the parent item all child items is unchecked. This is working the way it should, but I can only find the value of the parent item, not each checked or unchecked child items. This is my code:

    @(Html.Telerik().TreeView()
        .Name("rtvTreeView")
        .ShowCheckBox(true)
        .ClientEvents(events => events
                    .OnChecked("TreeView_onChecked")
        )
        .BindTo(Model.Parentgroup, mappings =>
        {
            mappings.For<Parentgroup>(binding => binding
                    .ItemDataBound((item, group) =>
                    {
                        item.Value = group.Id.ToString();
                        item.Text = group.Name;
                    })
                    .Children(group => group.Childgroup));
            mappings.For<Childgroup>(binding => binding
                    .ItemDataBound((item, type) =>
                    {
                        item.Value = type.Id.ToString();
                        item.Text = type.Name;
                    }));
        })
    )
 
<script type="text/javascript">
 
    function treeView() {
        return $('#rtvTreeView').data('tTreeView');
    }
 
     function TreeView_onChecked(e) {
        var item = $(e.item);
        var isChecked = e.checked;
        var childCheckBoxes = item.find(":checkbox");
 
        $.each(childCheckBoxes, function (index, checkbox) {
            $(checkbox).attr('checked', isChecked);
            var val = treeView().getItemValue(e.item);
            alert(val);
            treeView().nodeCheck(null, checkbox);
        });
    }
</script>
 

2 Answers, 1 is accepted

Sort by
0
Accepted
Alex Gyoshev
Telerik team
answered on 26 May 2011, 07:51 AM
Hello Roy,

In your code, you are alerting only the text of the parent node (e.item). You can get the value of the items by substituting the following line:
var val = treeView().getItemValue(e.item);
with the following:
var val = treeView().getItemValue($(checkbox).closest(".t-item"));

Greetings,
Alex Gyoshev
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
Maryem
Top achievements
Rank 1
answered on 11 Jun 2012, 03:19 PM
J'ai utilisé un treeview telerik et je veux afficher toute la liste des éléments coché  dans un autre formulaire .comment faire ?

Voici le code que j'ai écris:
    Html.Telerik().TreeView()
            .Name("TreeView")
            .HtmlAttributes(new { style = "width: 300px; float:; margin-bottom: 40px; margin-left: 3px;" })
            .ShowCheckBox(true)

           .BindTo(Model, mappings =>
           {
               mappings.For<HowToTelerikDemo.Models.CategoryTest>(binding => binding
                       .ItemDataBound((item, category) =>
                       {
                           item.Text = category.NomCategory;
                          

                           item.Expanded = true;

                       })

                       .Children(category => category.TestTobDisplayedInTree));


               mappings.For<HowToTelerikDemo.Models.ModelTestForTree>(binding => binding
                       .ItemDataBound((item, test) =>
                       {
                           item.Text = test.TestDisplayedName;
                         


                       }));

 <script type="text/javascript">

       function treeView() {
           return $('#TreeView').data('tTreeView');
       }

       function TreeView_onChecked(e) {
           var item = $(e.item);
           var isChecked = e.checked;
           var childCheckBoxes = item.find(":checkbox");

           $.each(childCheckBoxes, function (index, checkbox) {
               $(checkbox).attr('checked', isChecked);
               var val = treeView().getItemValue($(checkbox).closest(".t-item"));
               alert("Le test séléctionné est"+ val);
               treeView().nodeCheck(null, checkbox);
           });
       }
</script>

           })
        .ClientEvents(events => events
                    .OnChecked("TreeView_onChecked")
        )
         .Render();
    
    
}
Tags
TreeView
Asked by
Roy Halvorsen
Top achievements
Rank 1
Answers by
Alex Gyoshev
Telerik team
Maryem
Top achievements
Rank 1
Share this question
or