使用切换/滑动检查/取消检查树 - 一些小的编码问题

发布于 2024-12-13 02:47:38 字数 599 浏览 4 评论 0原文

虽然这确实使用了我昨天提出的问题中的一些代码(动态检查/取消选中树中的复选框),我觉得这是一个稍微不同的问题,因为我需要添加清除 div 并在树中上下滑动数据。

我已经将昨天学到的内容添加到滑块中,按照此链接 - http://jsfiddle.net/3V4hg / - 但现在我添加了清除 div 如果树的底部没有选择任何选项,树不会一直取消选中到顶部。如果您查看 JSFiddle,如果选中 A 和/或 B 然后取消选中它,则父级和祖级不会自动取消选中。另外,由于某种我还没有弄清楚的原因 - 滑块决定在单击子区域中的复选框时滑动(我还注意到,当大陆切换时,区域区域显示的切换图像会发生变化 -没有尝试解决这个问题,就像添加到 JSFiddle 时注意到的那样)。

我还认为可能有更好的方法来编码切换器/滑块(因为被不止一种切换器使用,但我不确定)。

Whilst this does use some of the code from a question I asked yesterday (Dynamically check / uncheck checkboxes in a tree), I feel that this is a slightly different question as I need to add in clearing divs and also slide data in the tree up and down.

I've taken what I learnt yesterday and added in a slider as per this link - http://jsfiddle.net/3V4hg/ - but now I've added clearing divs the tree is not unchecking all the way to the top if the bottom of the tree has no options selected. If you look at the JSFiddle, if you check A and/or B then uncheck it, the parent and grandparent do not uncheck automatically. Also, for some reason that I haven't figured out yet - the slider decides to slide upon clicking the checkbox in the child area (I've also noticed that the toggle image for the region area to display changes when the continent one toggles - haven't tried to solve that as just noticed when adding to JSFiddle).

I'm also thinking that there may be a better way to code the togglers/sliders (since used by more than one kind of toggle, but I'm unsure).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

孤芳又自赏 2024-12-20 02:47:38

小提琴:http://jsfiddle.net/3V4hg/2/

我对您的代码进行了一些修改。看看小提琴和注释(在代码和答案的底部):

$('#delivery_zones :checkbox').change(function(){
     $(this).siblings('ul').find(':checkbox').prop('checked', this.checked);
     if(this.checked){
         $(this).parentsUntil('#delivery_zones', 'ul').siblings(':checkbox').prop('checked', true);
     } else {                   
         $(this).parentsUntil('#delivery_zones', 'ul').each(function() {
             var $this = $(this);
             var childSelected = $this.find(':checkbox:checked').length;
             if(!childSelected){
                // Using `prevAll` and `:first` to get the closest previous checkbox
                $this.prevAll(':checkbox:first').prop('checked', false);
             }
         });
     }
});

// collapse countries and counties onload
$(".country_wrap").hide();                
$(".county_wrap").hide();                                 

// Merged two click handlers
$("#delivery_zones").click(function(event){
    var root = event.target;              // Get the target of the element
    if($.nodeName(root, 'input')) return; // Ignore input
    else if(!$.nodeName(root, 'li')) {
        root = $(root).parents('li').eq(0); // Get closest <li>
    }
    // Define references to <img>
    var img = $('.toggle img', root).eq(0);
    // Define reference to one of the wrap elements *
    var c_wrap = $('.country_wrap, .county_wrap', root).eq(0);
    if(img.attr('src') == "http://uk.primadonna.eu/images/arrow_white_up.gif"){
        img.attr('src', 'http://www.prbuzzer.com/images/downarrow-white.png');
        c_wrap.slideUp("slow");
    } else {
        img.attr('src', 'http://uk.primadonna.eu/images/arrow_white_up.gif');
        c_wrap.slideDown("slow");
    }
});

* 我已将根定义为

  • 元素。应选择第一次出现的 .count(r)y_wrap 元素,这是使用 .eq(0) 实现的。
  • 您之前的代码包含一些逻辑错误,我也已修复: $('.toggle img', this) 选择作为子元素的每个 元素.toggle,这导致树末端的箭头也切换。我使用 event.target 的解决方案更加简洁,并且允许您的示例扩展到更深的树。

    Fiddle: http://jsfiddle.net/3V4hg/2/

    I have applied some modifications to your code. Have a look at the fiddle and comments (at the code, and at the bottom of the answer):

    $('#delivery_zones :checkbox').change(function(){
         $(this).siblings('ul').find(':checkbox').prop('checked', this.checked);
         if(this.checked){
             $(this).parentsUntil('#delivery_zones', 'ul').siblings(':checkbox').prop('checked', true);
         } else {                   
             $(this).parentsUntil('#delivery_zones', 'ul').each(function() {
                 var $this = $(this);
                 var childSelected = $this.find(':checkbox:checked').length;
                 if(!childSelected){
                    // Using `prevAll` and `:first` to get the closest previous checkbox
                    $this.prevAll(':checkbox:first').prop('checked', false);
                 }
             });
         }
    });
    
    // collapse countries and counties onload
    $(".country_wrap").hide();                
    $(".county_wrap").hide();                                 
    
    // Merged two click handlers
    $("#delivery_zones").click(function(event){
        var root = event.target;              // Get the target of the element
        if($.nodeName(root, 'input')) return; // Ignore input
        else if(!$.nodeName(root, 'li')) {
            root = $(root).parents('li').eq(0); // Get closest <li>
        }
        // Define references to <img>
        var img = $('.toggle img', root).eq(0);
        // Define reference to one of the wrap elements *
        var c_wrap = $('.country_wrap, .county_wrap', root).eq(0);
        if(img.attr('src') == "http://uk.primadonna.eu/images/arrow_white_up.gif"){
            img.attr('src', 'http://www.prbuzzer.com/images/downarrow-white.png');
            c_wrap.slideUp("slow");
        } else {
            img.attr('src', 'http://uk.primadonna.eu/images/arrow_white_up.gif');
            c_wrap.slideDown("slow");
        }
    });
    

    * I have defined the root to be a <li> element. The first occurrence of the .count(r)y_wrap element should be selected, which is achieved using .eq(0).

    Your previous code contained some logical errors, which I have also fixed: $('.toggle img', this) selects every <img> element which is a child of .toggle, which caused the arrows at the end of the tree to toggle too. My solution using event.target is more neater, and allows your example to be extended to even deeper trees.

    ~没有更多了~
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文