jqueryui selectable with nested lists - selecting/deselecting a parent should select/deselect all it's children

StackOverflow https://stackoverflow.com/questions/14164090

  •  13-01-2022
  •  | 
  •  

Question

I'm trying to build a two-level multi-select control using nested ULs and jqueryUI selectable.

Currently, I'm restricting selections to the Child level, but what I really want is to be able to select a Parent LI and have all it's Child LIs select as well. Going further, I would want to be able to select all Child LIs and have their Parent be selected. Anything less than all Child LIs selected would result in the Parent being un-selected.

The basic markup:

<ul id="theParentList">
    <li>Parent 1
        <ul>
            <li>Child 1.1</li>
            <li>Child 1.2</li>
        </ul>
    </li>
    <li>Parent 2
        <ul>
            <li>Child 2.1</li>
            <li>Child 2.2</li>
        </ul>
    </li>
</ul>

and the current javascript:

$('#theParentList').selectable({filter: 'li li'});

How would I accomplish the first part: selecting a Parent selects all of it's Children?

Thanks!

UPDATE:
I've got it most of this concept working now:
Selecting a Parent selects all of it's Children;
Deselecting a Child will deselect it's Parent

What still isn't working: Selecting all of a Parent's Children should select the Parent

Here's what I've got, at this point:

Markup:

<ul id="theParentList">
    <li class="level-1">
        <div>Parent 1</div>
        <ul class="level-2">
            <li><div>Child 1.1</div></li>
            <li><div>Child 1.2</div></li>
        </ul>
    </li>
    <li class="level-1"><div>Parent 2</div>
        <ul class="level-2">
            <li><div>Child 2.1</div></li>
            <li><div>Child 2.2</div></li>
        </ul>
    </li>
</ul>

and the js:

    function SelectSelectableElement (selectableContainer, elementsToSelect){
        $(elementsToSelect).not(".ui-selected").addClass("ui-selecting");
    }

    function handleSelected (){};

    function handleSelection (El){
        if( $(El).parent().hasClass('level-1')){
            var ch = $(El).parent().find('.level-2 .ui-selectee');
            SelectSelectableElement('#theParentList', ch);
        }else{
            //this is a level-2 item 
            //if El and all of it's level-2 siblings are selected, then also select their level-1 parent
        }
    };

    function handleUnselected (El){
        if( $(El).parent().hasClass('level-1') ){
            //unselect all of it's children
            $(El).parent().children().each( function(){
                $(this).find('.ui-selectee').removeClass('ui-selected').addClass('ui-unselected');
            });
        }else{
            //this is a level-2 item, so we need to deselect its level-1 parent
            $(El).parents('li.level-1').find(">:first-child").removeClass('ui-selected');
        }
    };

    $('#theParentList').selectable({
        filter: 'li div',
        selecting: function(event, ui){
            handleSelection(ui.selecting);
        },
        selected: function(event, ui) {
            handleSelected(ui.selected);
        },
        unselected: function(event, ui) {
            handleUnselected(ui.unselected);
        }           
    });

Here's a fiddle: http://jsfiddle.net/JUvTD/

Was it helpful?

Solution

Posting an answer to my own question, in case anyone else needs help with the same

Selecting a parent will select all of it's children Selecting all of the children will select their parent UnSelecting a parent will unselect all of it's children Unselecting a child will also unselect it's parent

here's a working fiddle: http://jsfiddle.net/QvqCE/1/

and the javascript

    $('#theParentList').selectable({
        filter: 'li div',
        selecting: function (event, ui) {
            if ($(ui.selecting).parent().hasClass('level-1')) {
                //this is a level-1 item, so select all of it's children
                var ch = $(ui.selecting).parent().find('.level-2 .ui-selectee');
                $(ch).not(".ui-selected").addClass("ui-selecting");
            } else {
                //this is a level-2 item, so check to see if all of it's siblings are also selected
                var sibs = $(ui.selecting).parent().siblings().find('.ui-selectee');
                var notSelected = 0;
                for (var i = 0; i < sibs.length; i++) {
                    if ($(sibs[i]).hasClass('ui-selected') || $(sibs[i]).hasClass('ui-selecting')) {
                        notSelected = notSelected
                    } else {
                        notSelected = notSelected + 1;
                    }
                }
                if (notSelected === 0) { //all siblings are selected, so select their level-1 parent as well
                    $(ui.selecting).parent().parent().parent().find('>:first-child').not(".ui-selected").addClass("ui-selecting");
                }
                //otherwise, just select as usual
            }
        },
        unselected: function (event, ui) {
            if ($(ui.unselected).parent().hasClass('level-1')) {
                //unselect all of it's children
                $(ui.unselected).parent().children().each(function () {
                    $(this).find('.ui-selectee').removeClass('ui-selected').addClass('ui-unselected');
                });
            } else {
                //this is a level-2 item, so we need to deselect its level-1 parent
                $(ui.unselected).parents('li.level-1').find(">:first-child").removeClass('ui-selected');
            }
        }
    });

OTHER TIPS

In addition to rolfsf's answer, in case you want slightly different behavior that binds the parent and child together:

  1. If all children are still selected, the parent remains selected. and

  2. If a child is deselected, deselect the parent as well,

you can add this callback function into the initialization of the selectable widget:

unselecting: function (event, ui) {
            if ($(ui.unselecting).parent().hasClass('level-1')) {
                //if all children still selected, don't deselect this
                var sibs = $(ui.unselecting).next().find('.ui-selectee');
                if (sibs.length > 0) {
                    var notSelected = 0;
                    for (var i = 0; i < sibs.length; i++) {
                        if ($(sibs[i]).hasClass('ui-selected') || $(sibs[i]).hasClass('ui-selecting')) {
                            notSelected = notSelected;
                        } else {
                            notSelected = notSelected + 1;
                        }
                    }
                    if (notSelected === 0) { //all children still selected, so keep their level-1 parent selected as well
                        $(ui.unselecting).addClass("ui-selecting");
                    }
                }

            } else {
                //if unselecting a child, unselect parent as well
                $(ui.unselecting).parent().parent().prev().removeClass('ui-selected').removeClass("ui-selecting");
            }

        }

See the jsfiddle here: http://jsfiddle.net/gav9q/

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top