jqueryui 可使用嵌套列表进行选择 - 选择/取消选择父项应选择/取消选择其所有子项

rol*_*fsf 5 multi-select jquery-ui-selectable

我正在尝试使用嵌套 UL 和 jqueryUI 可选构建一个两级多选控件。

目前,我将选择限制为子级别,但我真正想要的是能够选择父 LI 并选择其所有子 LI。更进一步,我希望能够选择所有子 LI 并选择其父级。任何少于选定的所有子 LI 的内容都将导致父级 LI 被取消选择。

基本标记:

<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>
Run Code Online (Sandbox Code Playgroud)

和当前的 JavaScript:

$('#theParentList').selectable({filter: 'li li'});
Run Code Online (Sandbox Code Playgroud)

我将如何完成第一部分:选择父级会选择其所有子级?

谢谢!

更新:
我现在已经了解了这个概念的大部分内容:
选择父项会选择它的所有子项;
取消选择子项将取消选择其父项

仍然不起作用:选择父级的所有子级应该选择父级

这就是我现在所得到的:

标记:

<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>
Run Code Online (Sandbox Code Playgroud)

和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);
        }           
    });
Run Code Online (Sandbox Code Playgroud)

这是一个小提琴: http: //jsfiddle.net/JUvTD/

rol*_*fsf 1

发布我自己问题的答案,以防其他人需要同样的帮助

选择父级将选择其所有子级 选择所有子级将选择其父级 取消选择父级将取消选择其所有子级 取消选择子级还将取消选择其父级

这是一个工作小提琴: http://jsfiddle.net/QvqCE/1/

和 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');
            }
        }
    });
Run Code Online (Sandbox Code Playgroud)