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

[Solved] How can I create a cascade treeview with checkbox support?

1 Answer 18 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.
Wayne
Top achievements
Rank 1
Wayne asked on 16 May 2013, 11:26 AM
I'm using Telerik MVC Extension. 
Code goes like this:
@(Html.Telerik().TreeView().ExpandAll(true)
        .Name("SupportTopicsTreeView")
        .ShowCheckBox(true)... )

What I want is to create a cascade checkbox tree, is there a quick way to do this? It's a pain to write my own function to support that.

1 Answer, 1 is accepted

Sort by
0
Wayne
Top achievements
Rank 1
answered on 17 May 2013, 08:28 AM
I put my implementation here just in case somebody like me might need it.
 
function supportTopicsTreeView_onChecked (e) {
 
       var findChildrenCheckboxes = function (item) {
           return $(item).children('ul').find(":checkbox");
       };
 
       var findChildrenItems = function (item) {
           return $(item).find(".t-item");
       };
 
       var findParentItem = function (item) {
           return $(item).parents('.t-item:first');
       };
 
       var findParentCheckbox = function (item) {
           return findCurrentCheckbox(findParentItem(item));
       };
 
       var findCurrentCheckbox = function (item) {
           return $(item).children("div").find(":checkbox");
       };
 
       var isLeafNode = function (item) {
           return findChildrenItems(item).length == 0;
       };
 
       var isRootNode = function (item) {
           return findParentItem(item).length == 0;
       };
 
       var checkAllChildren = function (item) {
           findChildrenCheckboxes(item).prop("checked", true).prop("indeterminate", false);
       };
 
       var unCheckAllChildren = function (item) {
           findChildrenCheckboxes(item).prop("checked", false).prop("indeterminate", false);
       };
 
       var isChildrenAllChecked = function (item) {
           return findChildrenCheckboxes(item).filter(":not(:checked)").length == 0;
 
       };
 
       var isChildrenAllUnchecked = function (item) {
           return findChildrenCheckboxes(item).filter(":checked").length == 0;
       };
 
       var handleChildrenNodes = function (item) {
           if (isLeafNode(item)) {
               return;
           }
           var isChecked = findCurrentCheckbox(item).prop("checked");
           if (isChecked) {
               checkAllChildren(item);
           }
           if (!isChecked) {
               unCheckAllChildren(item);
           }
       };
 
       var handlerParentNode = function (item) {
           if (isRootNode(item)) {
               return;
           }
           var parent = findParentItem(item);
           var parentCheckbox = findParentCheckbox(item);
           if (isChildrenAllUnchecked(parent)) {
               parentCheckbox.prop("checked", false).prop("indeterminate", false);
           } else if (isChildrenAllChecked(parent)) {
               parentCheckbox.prop("checked", true).prop("indeterminate", false);
           } else {
               parentCheckbox.prop("checked", true).prop("indeterminate", true);
           }
 
           handlerParentNode(parent);
 
       };
       //cascade checkbox
       handleChildrenNodes(e.item);
       handlerParentNode(e.item);
   }
Tags
TreeView
Asked by
Wayne
Top achievements
Rank 1
Answers by
Wayne
Top achievements
Rank 1
Share this question
or