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

[Solved] Check checkbox on item select

3 Answers 141 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.
Donna Stewart
Top achievements
Rank 1
Donna Stewart asked on 02 Feb 2011, 06:52 PM
I know this has to be easy, but I can't seem to figure it out.  I have a treeview with checkboxes and I have an onSelect client event set to call a javascript function to set the data for another treeview.  I would like to check the checkbox of the selected item when I call the onSelect function.  Can I do this in my javascript function?  Also, for this very same javascript function, if the result returned is empty, it doesn't clear the treeview, it just leaves the nodes from the last one there.  How do I clear the treeview if the result returned is empty?

Here are the treeviews:

<%= Html.Telerik().TreeView().Name("WTGroupNames").ShowCheckBox(True).HtmlAttributes(New With {.style = "width:250px; height:550px;"}) _
     .ClientEvents(Function(evt) evt.OnSelect("getUserGroupsForTaskGroup")) _
     .BindTo(CType(Model.wtGoupNames, IEnumerable(Of SelectListItem)),
              Sub(mappings)
                     mappings.For(Of SelectListItem)(
                           Sub(binding) binding _
                                    .ItemDataBound(
                                           Sub(item, linkGroup)
                                                  item.Text = linkGroup.Text
                                                  item.Value = linkGroup.Value
                                                   item.LoadOnDemand = True
                                            End Sub)
                            End Sub)
                 End Sub) _
           .DataBinding(Function(dataBinding) dataBinding.Ajax().Select("GetTasksForGroup", "WebTask"))
%>

<%= Html.Telerik().TreeView().Name("UGsForTGs").ShowCheckBox(True).HtmlAttributes(New With {.style = "width:250px; height:550px;"}) _
                        .Items(Function(treeview) treeview.Add().Text("Related user groups")) _
                        .DataBinding(Function(dataBinding) dataBinding.Ajax().Select("GetUsersForUserGroups", "WebTask"))
%>

Here's the javascript:

function getUserGroupsForTaskGroup(e) {
    $('#usergroupsfortaskgroups').show();
    var taskGroupTree = $('#WTGroupNames').data('tTreeView');
    var selectedValue = taskGroupTree.getItemValue(e.item);
    var userGroupTree = $('#UGsForTGs');
    $.post('<%= Url.Action("GetUserGroupsForTaskGroup", "WebTask") %>', // The url of the /Home/Grid action method
         {
         id: selectedValue
     },  // arguments which need to be passed to the Grid action method
         function (result, textStatus) { //JavaScript callback executed when the ajax request finishes
             userGroupTree.data('tTreeView').bindTo(result); // update the DIV which contains the grid
         },
         "json"
    );
}

Your help is very much appreciated!

Thanks,
Donna

3 Answers, 1 is accepted

Sort by
0
Georgi Krustev
Telerik team
answered on 03 Feb 2011, 10:30 AM
Hello Donna,

 
In order to achieve your goal will need to check checkbox with JavaScript and then call nodeClick method:

var treeView = $(this);
var item = $(e.item);
var checkbox = item.find('.t-checkbox:first [type=checkbox]');
var shouldCheck = !checkbox.is(':checked');
 
checkbox.attr('checked', shouldCheck);
treeView.nodeCheck(item, shouldCheck);
 This code snippet should be added to the getUserGroupsForTaskGroup method.

TreeView UI component should always have at least one item in order to be able to bind it. In order to overcome this you can hide the treeview if returned data is empty.

Best regards,
Georgi Krustev
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
Donna Stewart
Top achievements
Rank 1
answered on 03 Feb 2011, 05:34 PM
This works great except for treeView.nodeCheck(item, shouldCheck); gives the following error - Microsoft JScript runtime error: Object doesn't support this property or method.  I tried changing it to nodeClick and checkboxClick, but I always get the javascript error.  I've commented it out for now.  Thank you so much for your prompt answers and for all of your help!  Please let me know if I need to do the nodeCheck or nodeClick and if so, what is the proper syntax to keep from getting the javascript error.

Thank you again!
Donna
0
Georgi Krustev
Telerik team
answered on 03 Feb 2011, 05:45 PM
Hello Donna,

 
In order to further investigate this issue I will need test project which replicates this issue.

Regards,
Georgi Krustev
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
Tags
TreeView
Asked by
Donna Stewart
Top achievements
Rank 1
Answers by
Georgi Krustev
Telerik team
Donna Stewart
Top achievements
Rank 1
Share this question
or