4

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

目前,我将选择限制在子级别,但我真正想要的是能够选择父 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>

和当前的javascript:

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

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

谢谢!

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

什么仍然不起作用:选择所有父母的孩子应该选择父母

在这一点上,这就是我所拥有的:

标记:

<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>

和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);
        }           
    });

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

4

2 回答 2

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');
            }
        }
    });
于 2013-01-31T18:57:44.500 回答
0

除了 rolfsf 的回答,如果您想要将父母和孩子绑定在一起的稍微不同的行为:

  1. 如果所有子代仍被选中,则父代仍被选中。和

  2. 如果取消选择子项,请同时取消选择父项,

您可以将此回调函数添加到可选小部件的初始化中:

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");
            }

        }

在此处查看 jsfiddle:http: //jsfiddle.net/gav9q/

于 2013-10-29T18:20:16.513 回答