jQuery Checkbox Tree Plugin – New and Improved
I have rewritten my jQuery plugin released last year, checkboxtree. A lot of options were asked for in the comments and I have tried to add all of those. Plus the old code made me cringe. I have updated the demos. And I have started a github repository.
The setup is the same. The html should look like this:
<ul class="mytree">
<li>
<input type="checkbox" name="foo" value="whatever" />
<label>Item 1</label>
<ul>
<li>
<input type="checkbox" name="foo" value="whatever" />
<label>Child Item 1</label>
</li>
</ul>
</li>
</ul>
All you need to use checkbox tree is this:
jQuery(document).ready(function(){
jQuery(".mytree").checkboxTree();
})
The options to be passed in as an object are as follows:
| Option | Description |
|---|---|
| collapsedarrow |
(default: "img-arrow-collapsed.gif") The path to the image used to indicate a collapsed row. |
expandedarrow |
(default: "img-arrow-expanded.gif ") The path to the image used to indicate an expanded row. |
blankarrow |
(default: "img-arrow-blank.gif") The path to the image used when there are no child checkboxes. |
activeClass |
(default: "checkboxtreeactive") The class added to the ul when the plugin has been successfully used. |
hoverClass |
(default: "over") The class added to the label on hover for ie6. (only added in ie6) |
checkchildren |
(default: false) If set to false the child checkboxes of the one you select will not be checked. If set to true, child checkboxes will be checked. |
checkparents |
(default: true) If set to true, all parents of a checked checkbox will be checked. |
collapsed |
(default: true) This dictates whether or not the tree will be collapsed or expanded on initial load. |
hidecbwithjs |
(default: false) If this is set to false you will need to hide the checkboxes with css. If true the checkboxes will be hid by the plugin. |
checkedClass |
(default: "checked") This is the class used on the label when a checkbox is in checked state. |
July 1st, 2009 at 11:47 am
I want to have a copy
July 1st, 2009 at 12:42 pm
You can get a copy at the repository linked above – http://github.com/magearwhig/jquery-checkboxtree
You can also get all the images and css from the demo.
July 18th, 2009 at 3:06 pm
Yo. I used the version here:
But to have it select certain items upon init you can add something like this:
$(‘li.checked’).each( function() {
$(this).find(‘div.checkbox’).click();
});
Then when you output in PHP or something add:
class=”checked”
to your list item. It should toggle all parents and whatnot.
Laterz.
July 18th, 2009 at 8:32 pm
Jonathan, There is no need to do that, if you just add checked=”checked” to the checkboxes that should be selected, everything will be taken care of. The script already looks for checkboxes that are already checked onload.
July 24th, 2009 at 7:43 am
Awesome stuff thanks for this!
I did modify a line of code to fit a need I had which was to collapse everything but those that had a checked child. Might be useful to add an option for it.
(top line here)
if(settings.collapsed && !(jQuery(“ul li input:checked”,$this).length)){
$this.find(“ul”).hide();
$collapseImage = jQuery(”)
$collapseImage.data(“collapsedstate”,0)
}else{
$collapseImage = jQuery(”)
$collapseImage.data(“collapsedstate”,1)
}
September 16th, 2009 at 1:30 am
Nice plugin. I think I will use it for my project, the only point I don’t like is that you are using images to replace the checkbox, and so the checkbox aren’t style like any other checkbox in my forms. And that’s a problem for me as, I use changing form theme styling.
I think I will try to adapt your work to keep the default checkbox. But before doing it, can you tell me why dit you choose this way? Did you find any difficulties to keep the original checkbox?
September 16th, 2009 at 6:55 am
Hi David, Used images so that the checkbox could have more than two states. We wanted to be able to show that a checkbox had checked children but was not checked itself. You should be able to not hid the checkboxes easily. In the configuration keep hidecbwithjs set to false and don’t hide the checkboxes with css.
October 1st, 2009 at 11:04 am
I am using this plugin for my project. Just got one question, when I use this
$(‘#checkbox_id’).click(funcion{
// my code here…
})
the function is never called. I need a event listener for the checkbox check and uncheck event. Do you have an easy way to do it?
October 2nd, 2009 at 1:11 pm
Greetings from Venezuela..
I’m a Rails developer working on a huge Rails project and definitely I’ll use your excellent plugin. Thanks a lot.
Just one question, I need to create a tree made by just radiobuttons (allowing to select just one option). Please tell me please tell me what should change in your plugin.
Thanks in advance..
October 7th, 2009 at 11:18 am
Your Repository contains 2 different(!) versions of “jquery.checkboxtree.js” in the zip to download … I just checked that you removed some uncommented lines, but two versions with diffrent sizes are… confusing at first!
October 9th, 2009 at 4:42 am
Can you please add the toggle functionality.
I need to add Tow link ‘Collapse’ and ‘Expand’ to collapse and expand the respectably.
Please give the Example code based on this plugin.
Thanks
Anirban
November 11th, 2009 at 4:25 pm
You might want to checkout this plugin :
http://www.redcarrot.co.uk/2009/11/11/collapsible-checkbox-tree-jquery-plugin/
November 24th, 2009 at 1:44 am
Hello,
Nice plugin. I’d like to use it in one of my projects. What is the license behind it?
Thanks
Stefan
November 24th, 2009 at 9:22 am
Stefan, Feel free to use it however you like.
December 11th, 2009 at 4:49 pm
Great plugin!
It’s being very useful for me!
I’m still new to jQuery but I’m trying to implement the “expand all/collapse all” functionality. It seems like I can’t only do the $(“ul”).hide() and change the images from expanded to collapsed because the control stores some information about the expanded/collapsed state and I don’t know how to acess this stored information in order to modify it.
Any idea? I will appreciate it a lot.
Thanks.
December 21st, 2009 at 8:59 am
I just wanted to mention that I could solve my ‘collapse all’ problem by just firing the click() event of the visible ’s used to represent the collapse/expand buttons like if they were clicked by the user. Now I just have to check if the images are in a visible because if not, they are already collapsed.
January 2nd, 2010 at 2:02 am
[...] floatmargin.com » Blog Archive » jQuery Checkbox Tree Plugin – New and Improved – very cool tree selector Share and Enjoy: [...]
January 25th, 2010 at 4:45 pm
I have another question.
What if I need to add more ul>li>labels to one of the branches of the tree?
Obviously they won’t get automatically activated with the plugin, so I tried to do the .activateCheckboxTree() specifically for the new branch that I add but some of the events doesn’t seem to work.
Any idea of how can I add new sub-branches and activate the checkboxTree on them?
January 28th, 2010 at 4:26 am
How to get the checked checkbox values?