BinkWu / jquery-checktree

Automatically exported from code.google.com/p/jquery-checktree
0 stars 0 forks source link

Set checkboxes as already checked #7

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
If you set the input checkboxes as already checked on page load this is 
not replicated by the code.

Original issue reported on code.google.com by jamestay...@gmail.com on 13 Nov 2008 at 10:36

GoogleCodeExporter commented 8 years ago
Here is a possible patch for this issue.

If you add the following before the return $tree line:

$tree.find("li")
     .children(":checkbox")
     .each(function() {
         var $c = jQuery(this);
         var $checked = $c.get(0).checked;
         var $allChildren = $c.siblings('ul').children().children(':checkbox');
         var $checkedChildren = $allChildren.filter(function() { return
jQuery(this).get(0).checked });
         var $sibs = $c.siblings("div.checkbox");

         if ($checked && $checkedChildren.length == $allChildren.length) {
             $sibs.addClass("checked");
         } else if (!$checked && $allChildren.length > 0 && $checkedChildren.length >
0) {
             $sibs.addClass("half_checked");
         }
     });

Original comment by rohr.ch...@gmail.com on 14 Nov 2008 at 5:53

GoogleCodeExporter commented 8 years ago
initial reset of checkbox state should be also changed from .attr("checked", "")
[line 88] to .attr("checked", function() { return this.checked } )

Original comment by hud...@nostromo.art.pl on 17 Nov 2008 at 2:39

GoogleCodeExporter commented 8 years ago
Is this the same as bug 4?

http://code.google.com/p/jquery-checktree/issues/detail?id=4

Original comment by jgeewax on 25 Nov 2008 at 6:40

GoogleCodeExporter commented 8 years ago
It seems that this is the same as #4

On another note, here is a better block of code for my previous post.  It is 
much faster.

var recursiveCheck = function(branch) {
    var thisState = null;
    branch = jQuery(branch);
    branch.children("ul").children().each(function() {
        var childState = recursiveCheck(this);
        if (thisState == null) {
            thisState = childState;
        } else if (thisState != childState) {
            thisState = "half_checked";
        }
    });
    if (thisState == null) {
        thisState = (branch.children()[2].checked) ? "checked" : "";
    }
    jQuery(branch.children()[1]).addClass(thisState);
    return thisState;
}

$tree.children("li").each(function() {
    recursiveCheck(this);
});

Original comment by rohr.ch...@gmail.com on 25 Nov 2008 at 7:07

GoogleCodeExporter commented 8 years ago
Responding to Comment #4
(http://code.google.com/p/jquery-checktree/issues/detail?id=7#c4), I'm marking 
this
as duplicate.

Original comment by jgeewax on 26 Jan 2009 at 4:34