Skip to content

Expand and Collapse - All and at diffrent levels #17

@ghost

Description

This really ins't an issue but I wanted to contribute. I found your code but needed to do expand and collapse all. I ended up writing these two functions:

function toggleSingleElement(el){
	var children = $(el).parent('li.parent_li').find(' > ul > li');
    if (children.is(":visible")) {
        children.hide('fast');
        $(el).attr('title', 'Expand this branch').find(' > i').addClass('icon-plus-sign').removeClass('icon-minus-sign');
    } else {
        children.show('fast');
        $(el).attr('title', 'Collapse this branch').find(' > i').addClass('icon-minus-sign').removeClass('icon-plus-sign');
    }
}

function globalToggle(element, selector){
	var addClass;
	var removeClass;
	var title;
	
	if($(element).find(' > i').hasClass('icon-minus-sign')){
		addClass = 'icon-plus-sign';
    	removeClass = 'icon-minus-sign';
    	title = 'Expand this branch';
	}else{
		addClass = 'icon-minus-sign';
    	removeClass = 'icon-plus-sign';
    	title = 'Collapse this branch';
	}
	
	$(element).attr('title', title).find(' > i').addClass(addClass).removeClass(removeClass);
	
	
	$(selector).each(function (index, value) {
		console.log('span' + index + ':' + $(this).attr('id'));
		toggleSingleElement(this);
	});
}

Then I added the following to my document ready:

$('.tree li:has(ul)').addClass('parent_li').find(' > span').attr('title', 'Collapse this branch');
    
    $('.tree li.parent_li > span').on('click', function (e) {
    	toggleSingleElement(this);
        e.stopPropagation();
    });
    
    $('.app-toggle').on('click', function (e) {
    	globalToggle(this ,'.app-element');  	
    	
        e.stopPropagation();
    });
    
    $('.device-toggle').on('click', function (e) {
    	globalToggle(this ,'.device-element');  
    	
        e.stopPropagation();
    });

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions