- (function($)
- {
- function Tree() {
- var $this = this;
- function treeNodeClick()
- {
- $(document).on('click', '.tree li a input[type="checkbox"]', function() {
- $(this).closest('li').find('ul input[type="checkbox"]').prop('checked', $(this).is(':checked'));
- }).on('click', '.node-item', function() {
- var parentNode = $(this).parents('.tree ul');
- if ($(this).is(':checked')) {
- parentNode.find('li a .parent').prop('checked', true);
- } else {
- var elements = parentNode.find('ul input[type="checkbox"]:checked');
- if (elements.length == 0) {
- parentNode.find('li a .parent').prop('checked', false);
- }
- }
- });
- };
- $this.init = function() {
- treeNodeClick();
- }
- }
- $(function() {
- var self = new Tree();
- self.init();
- })
- }(jQuery))
above jquery used for selecting parent node all child node wil be selected.but my requirement is checking child node parent should be selected ..please give me solutions
Karthikeyan KPosted Sep 11, 2015, 12:17 AM
Karthikeyan KPosted Sep 11, 2015, 12:14 AM
Root
Child
Child
Sub child
Sub child
Child
Sub child
Sub child
$(this).siblings('ul').find(':checkbox').prop('checked', this.checked);
if (this.checked) {
$(this).parentsUntil('#treeList', 'ul').siblings(':checkbox').prop('checked', true);
} else {
$(this).parentsUntil('#treeList', 'ul').each(function(){
var $this = $(this);
var childSelected = $this.find(':checkbox:checked').length;
if (!childSelected) {
$this.prev(':checkbox').prop('checked', false);
}
});
}
});
OUTPUT