jsTree Node Expand/Collapse

前端 未结 4 733
故里飘歌
故里飘歌 2021-02-09 08:55

I ran into the excellent jstree jQuery UI plug in this morning. In a word - great! It is easy to use, easy to style & does what it says on the box. The one thing I have n

4条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-02-09 09:09

    The above answer will construct tree again and again. The below code will open the node and collapse which are already opened and it does not construct tree again.

    .bind("open_node.jstree",function(event,data){
            closeOld(data);
            });
    

    and closeOld function contains:

    function closeOld(data)
    {
        if($.inArray(data.node.id, myArray)==-1){
                myArray.push(data.node.id);
                if(myArray.length!=1){
                    var arr =data.node.id+","+data.node.parents;
                    var res = arr.split(",");
                    var parentArray = new Array();
                    var len = myArray.length-1;
                    for (i = 0; i < res.length; i++) {
                        parentArray.push(res[i]);
                    }
                    for (var i=len;i >=0;i--){
                        var index = $.inArray(myArray[i], parentArray);
                    if(index==-1){
                        if(data.node.id!=myArray[i]){
                        $('#jstree').jstree('close_node',myArray[i]);
                            delete myArray[i];
                        }
                    }
                    }
            }
        }
    

提交回复
热议问题