Reputation: 157
I using this code to check the checkbox in a treeview.
HTML:
<div id="tree">
<ul>
<li>
<input type="checkbox"/> Root
<ul>
<li>
<input type="checkbox"/> Child 1
<ul>
<li><input type="checkbox"/> Subchild 1</li>
<li><input type="checkbox"/> Subchild 2</li>
<li><input type="checkbox"/> Subchild 3</li>
</ul>
</li>
<li>
<input type="checkbox"/> Child 2
<ul>
<li><input type="checkbox"/> Subchild 1</li>
<li><input type="checkbox"/> Subchild 2</li>
</ul>
</li>
</ul>
</li>
</ul>
</div>
JavaScript:
$(document).ready(function() {
$("#tree input:checkbox").change(function() {
var val = $(this).attr("checked");
$(this).parent().find("input:checkbox").each(function() {
if (val) {
$(this).attr("checked", "checked");
} else {
$(this).removeAttr("checked");
}
});
});
});
JSFiddle: http://jsfiddle.net/jRAcq/
this works well. But if the any one of the sub node is unchecked then the root/child root to be unchecked. How can I do this one?
Upvotes: 2
Views: 3723
Reputation: 8552
$(document).ready(function () {
$.extend($.expr[':'], {
unchecked: function (obj) {
return ((obj.type == 'checkbox' || obj.type == 'radio') && !$(obj).is(':checked'));
}
});
$("#tree input:checkbox").live('change', function () {
$(this).next('ul').find('input:checkbox').prop('checked', $(this).prop("checked"));
for (var i = $('#tree').find('ul').length - 1; i >= 0; i--) {
$('#tree').find('ul:eq(' + i + ')').prev('input:checkbox').prop('checked', function () {
return $(this).next('ul').find('input:unchecked').length === 0 ? true : false;
});
}
});
});
for live demo see this link: http://jsfiddle.net/nanoquantumtech/JfMCP/
//Or
$(document).ready(function () {
$.extend($.expr[':'], {
unchecked: function (obj) {
return ((obj.type == 'checkbox' || obj.type == 'radio') && !$(obj).is(':checked'));
}
});
jQuery.fn.reverse = [].reverse;
$("#tree input:checkbox").live('change', function () {
$(this).next('ul').find('input:checkbox').prop('checked', $(this).prop("checked"));
$('#tree').find("input:checkbox + ul").reverse().each(function () {
$(this).prev('input:checkbox').prop('checked', $(this).find('input:unchecked').length === 0 ? true : false);
});
});
});
//==================================================================================//
accroding to your html jquery code is below:
$(document).ready(function () {
$.extend($.expr[':'], {
unchecked: function (obj) {
return ((obj.type == 'checkbox' || obj.type == 'radio') && !$(obj).is(':checked'));
}
});
jQuery.fn.reverse = [].reverse;
$('#PagesTree').find('input:checkbox').live('change', function () {
$('#' + $(this).attr('name').replace(/CheckBox/g, '') + 'Nodes').find('input:checkbox').prop('checked', $(this).prop("checked"));
$('#PagesTree').find('input:checkbox').reverse().each(function () {
var obj = $('#' + $(this).attr('name').replace(/CheckBox/g, '') + 'Nodes');
if (obj.find('input:checkbox').length > 0)
$(this).prop('checked', obj.find('input:unchecked').length === 0 ? true : false);
});
});
});
for live demo see this link: http://jsfiddle.net/nanoquantumtech/GmT7U/
Upvotes: 1
Reputation: 3535
Try this...
$(document).ready(function() {
$("#tree input:checkbox").change(function() {
var val = $(this).attr("checked");
$(this).parent().find("input:checkbox").each(function() {
if (val) {
$(this).attr("checked", "checked");
} else {
$(this).removeAttr("checked");
$(this).parents('ul').each(function(){
$(this).prev('input:checkbox').removeAttr("checked");
});
}
});
});
});
Upvotes: 1