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

How to get parent checkbox unchecked when one of his childs is unchecked

6 Answers 945 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.
walid
Top achievements
Rank 1
walid asked on 03 Jun 2010, 10:55 AM

I have a treeview that has the checkboxes shown.
What i would like to do is to uncheck parent when one of child checkboxes is unchecked.
I'am using a script provide in previous post to get all childs checkboxes checked when parent is checked.
http://www.telerik.com/community/forums/aspnet-mvc/treeview/how-to-get-child-checkboxes-selected-when-parent-checkbox-is-selected.aspx
I just can't find parent of clicked checkbox
I'am using this :

 

$(checkedbox).closest(

'.t-item').parent().closest('.t-item').find('> ul > li > div > .t-checkbox :checkbox');

 

 

Thanks

6 Answers, 1 is accepted

Sort by
0
Rich
Top achievements
Rank 1
answered on 29 Mar 2012, 04:10 AM
Did you find a solution for this?
0
BRUNO
Top achievements
Rank 1
answered on 09 Apr 2012, 04:00 PM
Please any solution for this issue?
0
Rich
Top achievements
Rank 1
answered on 09 Apr 2012, 04:55 PM
Two different solutions:

First solution:

Make sure you add this to the treeview code in the view:
        .ClientEvents(events => events
                        .OnChecked("nodeChecked")
                        .OnDataBound("nodeChecked")
            )

Then add this javascript function:

        function nodeChecked(e) {
            var item = $(e.item),
            isChecked = e.checked,
            treeView = $(this).data('tTreeView'),
            childCheckBoxes = item.find(":checkbox");
            treeView.nodeCheck(childCheckBoxes, e.checked);
        }

Alternative solution:

Assuming your treeview id is TreeView, add this javascript - make sure it's executed AFTER the treeview is rendered:
$('#TreeView').find("li")
                          .find('> div > .t-checkbox :checkbox')
                          .bind('click', function (e) {
                              var isChecked = $(e.target).is(':checked');
                              var treeView = $($(e.target).closest('.t-treeview')).data('tTreeView');
                              var item = $(e.target).closest('.t-item');
                              var checkboxes = item.find('.t-checkbox :checkbox');
                              $.each(checkboxes, function (index, checkbox) {
                                  $(checkbox).attr('checked', isChecked ? true : false);
                                  treeView.checkboxClick(e, checkbox);
                              });

                              var siblings = item.parent().find('> li .t-checkbox');
                              var siblingsLength = siblings.length;
                              var checkedLength = siblings.find(':checked').length;

                              var parentCheckBox = item.parent().closest('.t-item').find('> div .t-checkbox :checkbox');
                              parentCheckBox.css('opacity', "1");
                              parentCheckBox.css('filter', "alpha(opacity=100)");

                              if (siblingsLength == checkedLength)
                                  parentCheckBox.attr('checked', true);
                              else if (checkedLength == 0)
                                  parentCheckBox.attr('checked', false);
                              else {
                                  parentCheckBox.attr('checked', true)
                                  parentCheckBox.css('opacity', "0.5");
                                  parentCheckBox.css('filter', "alpha(opacity=50)");
                              }
                              treeView.checkboxClick(e, parentCheckBox)

                          });

This solution will dim parent checkbox if only some children are unchecked.
0
BRUNO
Top achievements
Rank 1
answered on 10 Apr 2012, 02:50 PM
Thank you Rich, but I cant use your solution yet.

When using first solution, the javascript function is not executed.

Using the secound solution I got the following error:
treeView.checkboxClick is not a function

Please do you know what might be happening?

Thanks
0
Rich
Top achievements
Rank 1
answered on 10 Apr 2012, 04:26 PM
Bruno -

Make sure your treeview is named "TreeView".  Here is an example of the view code:

@{Html.Telerik().TreeView()
        .Name("TreeView")
        .ShowCheckBox(true)
        .Items(item =>
        {
            item.Add()
                .Text("Status").Expanded(true).Checked(true)
                .Items(subItem =>
                {
                    subItem.Add().Text("On Plan").Checked(true);
                });
 
            item.Add()
                .Text("Owner").Checked(true)
                .Items(subItem =>
                {
                    subItem.Add().Text("PSP").Checked(true);
                });
        })
        .ClientEvents(events => events
                        .OnChecked("nodeChecked")
                        .OnDataBound("nodeChecked")
            )
        .Render();
         
        }

Let me know if that doesn't work...
0
shilin
Top achievements
Rank 1
answered on 15 May 2012, 07:33 PM

Hi Rich,

I use the alternative solution in your demo code, the only problem is that when post back to server, only the actually checked checkboxes (not the ones checked by JavaScript) will show up in the TreeView_checkedNodes list. I changed treeView.checkboxClick to treeView.nodeCheck, and then the problem was solved. But when I use IE back button and resubmit the form, the checked checkboxes will not show up in the TreeView_checkNodes list again. It seems that the states of all the checkboxes have been lost. BTW, there is no this issue in Firefox. Any ideas would be appreciated.

Thanks.

Tags
TreeView
Asked by
walid
Top achievements
Rank 1
Answers by
Rich
Top achievements
Rank 1
BRUNO
Top achievements
Rank 1
shilin
Top achievements
Rank 1
Share this question
or