在dynatree兄弟中混合多个选择(复选框)和单选(单选按钮)

时间:2015-06-29 04:36:55

标签: javascript jquery dynatree

我的dynatree看起来像这样: -

                                               |---- Their Car
                       |----------- Cars ----- |---- My Car
                       |
   Objects ----------- |----------- Boats -----| --- My Boat
                       |                       | --- Your Boat
                       |                       | --- custom1_Your Boat
                       |                       | --- custom2_Your Boat
                       |
                       |----------- Bikes1 -----| --- Your Bike
                       |----------- custom1_Bikes1 -----| --- My Bike

我希望用户能够在Bikes1custom1_Bikes1以及Your Boatcustom1_Your Boatcustom2_Your Boat中选择一个节点。其余节点应该是多选的

这是我迄今为止所尝试的内容

 $(function(){
    $("#tree").dynatree({
    checkbox: true,
    selectMode: 2,
    initAjax: {
        url: 'get-list.php'
    },
    onSelect: function(flag, node){
        if (flag) {
            var siblings = node.getParent().getChildren();


            if((node.data.title).indexOf('custom') >= 0) { // If node contains the string 'custom'

            for( var x in siblings) { // Loop through the sibling nodes


                if(((node.data.title).split('_')[1] == x.data.title) || x.data.title.indexOf('custom') >= 0) { // Check if there is another node containing 'custom' or if there's a node with the same name after '_'
                     x.select(!flag); //deselect that node
                }

            }

            }

            else {   // If the node doesn't contain the string 'custom'

                for (var x in siblings) { // Loop through sibling
                if((x.data.title).indexOf('custom') >= 0) { // Check if there is a node with string 'custom'
                     x.select(!flag); //deselect that node
                }
               }
             }
          }
         }
         // Do something else with the list of selected nodes

        });
    });

似乎节点没有被自动取消选择,我想知道selectMode:2是否覆盖了编程行为

1 个答案:

答案 0 :(得分:0)

回答我自己的问题,问题出在我如何循环siblings

以下是更正后的代码:

$(function(){
var inEventHandler = false;
$("#tree").dynatree({
    checkbox: true,
    selectMode: 2,
    initAjax: {
        url: 'get-list.php'
    },

    onSelect: function(select, dtnode) {   
        if (select) {
            var siblings = dtnode.getParent().getChildren();


            if((dtnode.data.title).indexOf('custom') >= 0) { //Check if it's a custom dtnode

            for( var i = 0; i < siblings.length; i++) { // Loop through sibling nodes


                if(((dtnode.data.title).split('_')[1] == siblings[i].data.title) || (siblings[i].data.title.indexOf('custom') >= 0 && siblings[i] != dtnode )) { // Check if there is another custom dtnode or base base dtnode 

                     siblings[i].select(false); //deselect that dtnode
                }

            }

            }

            else {

               for (var i = 0; i < siblings.length; i++) {
                  if((siblings[i].data.title).indexOf('custom') >= 0) { // Check if there is another custom dtnode 

                     siblings[i].select(false);
                   }
               }


            } 
       }

     // Do something with the selected nodes
     }  
});
});