【问题标题】:jqueryui selectable with nested lists - selecting/deselecting a parent should select/deselect all it's childrenjqueryui 可通过嵌套列表选择 - 选择/取消选择父级应该选择/取消选择它的所有子级
【发布时间】:2013-01-04 20:03:25
【问题描述】:

我正在尝试使用嵌套 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'});

我将如何完成第一部分:选择一个 Parent 会选择它的所有 Children?

谢谢!

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

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

这就是我现在所拥有的:

标记:

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

【问题讨论】:

    标签: multi-select jquery-ui-selectable


    【解决方案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');
                }
            }
        });
    

    【讨论】:

      【解决方案2】:

      除了 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/

      【讨论】:

      • 不错@rocketegg!不过,我看不出行为有何不同。是您依赖上下文/层次结构而不是类吗?
      猜你喜欢
      • 2020-03-26
      • 2018-05-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-05
      • 1970-01-01
      • 2017-03-17
      • 2023-04-04
      相关资源
      最近更新 更多