【问题标题】:Jquery treeview-How to expand/collapse a given single node by click on a buttonJquery treeview-如何通过单击按钮展开/折叠给定的单个节点
【发布时间】:2015-09-17 15:55:05
【问题描述】:

这是我的第一个问题。我使用 Jquery 树视图。我已经创建了一个树视图,当我使用鼠标单击父节点时,它展开得很好。但我真的想通过单击一个按钮来展开每个父节点。这段代码展示了我是如何创建树视图的。

body, a {
    color: #3B4C56;
    font-family: sans-serif;
    font-size: 14px;
    text-decoration: none;
}

a{
	cursor:pointer;
}
.tree ul {
    list-style: none outside none;
}
.tree li a {
    line-height: 25px;
}
.tree > ul > li > a {
    color: #3B4C56;
    display: block;
    font-weight: normal;
    position: relative;
    text-decoration: none;
}
.tree li.parent > a {
    padding: 0 0 0 28px;
}
.tree li.parent > a:before {
    <!--background-image: url("../images/plus_minus_icons.png"); -->
    background-position: 25px center;
     content: ""; 
    display: block;
    height: 21px;
    left: 0;
    position: absolute;
    top: 2px;
    vertical-align: middle;
    width: 23px;
}
.tree ul li.active > a:before {
    background-position: 0 center;
}
.tree ul li ul {
    border-left: 1px solid #D9DADB;
    display: none;
    margin: 0 0 0 12px;
    overflow: hidden;
    padding: 0 0 0 25px;
}
.tree ul li ul li {
    position: relative;
}
.tree ul li ul li:before {
    border-bottom: 1px dashed #E2E2E3;
    content: "";
    left: -20px;
    position: absolute;
    top: 12px;
    width: 15px;
}
#wrapper {
    
    width: 400px;
}
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<!-- Note: IE8 supports the content property only if a !DOCTYPE is specified. -->
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
	<link rel="stylesheet" href="treeStyles.css">
	<script src="http://code.jquery.com/jquery-1.7.2.min.js" type="text/javascript" > </script>
	
	<script type="text/javascript">

	$( document ).ready( function( ) {
				$( '.tree li' ).each( function() {
						if( $( this ).children( 'ul' ).length > 0 ) {
								$( this ).addClass( 'parent' );     
						}
				});
				
                /* $( '.tree li.parent > a' ).click( function( ) {
						$( this ).parent().toggleClass( 'active' );
						$( this ).parent().children( 'ul' ).slideToggle( 'fast' );
				});*/
				
				
            
    $('#button1')
    .unbind('click')
    .click( function() {
        $('.tree li.parent > a').parent().toggleClass( 'active' );
        $('.tree li.parent > a').parent().children( 'ul' ).slideToggle( 'fast' );
});		
});
 /*       
$('#button1').click(function(){
    $('#li_1').children('ul').slideToggle( 'fast' ); 
}),
$('#button2').click(function(){
    $('#li_2').children('ul').toggle(); 
});  */       
        
	</script>
</head>
<body>
<div id="wrapper">
    
<div class="tree">
		
   <ul id="list">
        <div tabindex="-1" id="1">
		  <li id="li_1"><a>TestSuite 1</a>
		  	<ul>
                <div tabindex="-1" id="11"><li><a>Test 1 1 Started</a></li></div>
                <div tabindex="-1" id="12"><li><a>Test 1 1 Passed</a></li></div>
                <div tabindex="-1" id="13"><li><a>Test 1 2 Started</a></li></div>
                <div tabindex="-1" id="14"><li><a>Test 1 2 Failed</a></li></div>
                <div tabindex="-1" id="15"><li><a>Test 1 3 Started</a></li></div>
                <div tabindex="-1" id="16"><li><a>Test 1 3 Passed</a></li></div>
		  	</ul>
		  </li>
       </div> 
       
       <div tabindex="-1" id="2">
		  <li id="li_2"><a>TestSuite 2</a>
		  	<ul>
                <div tabindex="-1" id="21"><li><a>Test 2 1 Started</a></li></div>
                <div tabindex="-1" id="22"><li><a>Test 2 1 Passed</a></li></div>
                <div tabindex="-1" id="23"><li><a>Test 2 2 Started</a></li></div>
                <div tabindex="-1" id="24"><li><a>Test 2 2 Failed</a></li></div>
		  	</ul>
		  </li>
       </div>
  </ul>
    
</div>
    
</div>
    
<br><br>
<input type="button" class="buttonLoad" id="button1" value="Expand TestSuite 1" /> 
<input type="button" class="buttonLoad" id="button2" value="Expand TestSuite 2" />     
    
<script>
 /*   
function myLoadFunction1(){
    //expand TestSuite1
    openMyOwnBranchInTree(li_1);
}
  
function myLoadFunction2(){
    //expand TestSuite2
    openMyOwnBranchInTree(li_2);
}
    
function openMyOwnBranchInTree(idLeaf) { 
        $('#table' + idLeaf).parents('li').show(); 
}    
  */   
    
</script>    

</body>
</html>

在这里,我使用 myLoadFunction1() 和 myLoadFunction2() 函数以编程方式展开/折叠每个 TestSuite。此外,我还需要通过引用节点 ID 来展开/折叠节点。

我尝试使用 openMyOwnBranchInTree() 函数展开/折叠每个父节点,但它不起作用。如何改进我的代码以获得我的结果,或者有什么简单的方法来完成这项任务? 欢迎建议。。 谢谢你。

已编辑。

我尝试了以下代码。(编辑前面的代码并通过注释该行显示我更改的位置。)我注释了 myLoadFunction1() 和 myLoadFunction2() 函数,并添加了 jquery 代码。

现在#button1 展开/折叠所有子节点.. 但是如何到达我的目标?谁能帮助我? 谢谢。

【问题讨论】:

    标签: javascript jquery html treeview jstree


    【解决方案1】:

    终于我自己找到了答案... :) 我们可以使用 JQuery 来完成这项任务。不需要javascript。 这是代码..

    body, a {
        color: #3B4C56;
        font-family: sans-serif;
        font-size: 14px;
        text-decoration: none;
    }
    
    a{
    	cursor:pointer;
    }
    .tree ul {
        list-style: none outside none;
    }
    .tree li a {
        line-height: 25px;
    }
    .tree > ul > li > a {
        color: #3B4C56;
        display: block;
        font-weight: normal;
        position: relative;
        text-decoration: none;
    }
    .tree li.parent > a {
        padding: 0 0 0 28px;
    }
    .tree li.parent > a:before {
        <!--background-image: url("../images/plus_minus_icons.png"); -->
        background-position: 25px center;
         content: ""; 
        display: block;
        height: 21px;
        left: 0;
        position: absolute;
        top: 2px;
        vertical-align: middle;
        width: 23px;
    }
    .tree ul li.active > a:before {
        background-position: 0 center;
    }
    .tree ul li ul {
        border-left: 1px solid #D9DADB;
        display: none;
        margin: 0 0 0 12px;
        overflow: hidden;
        padding: 0 0 0 25px;
    }
    .tree ul li ul li {
        position: relative;
    }
    .tree ul li ul li:before {
        border-bottom: 1px dashed #E2E2E3;
        content: "";
        left: -20px;
        position: absolute;
        top: 12px;
        width: 15px;
    }
    #wrapper {
        
        width: 400px;
    }
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <!-- Note: IE8 supports the content property only if a !DOCTYPE is specified. -->
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
    <head>
    	<link rel="stylesheet" href="treeStyles.css">
    	<script src="http://code.jquery.com/jquery-1.7.2.min.js" type="text/javascript" > </script>
    	
    	<script type="text/javascript">
    
    	$( document ).ready( function( ) {
    				$( '.tree li' ).each( function() {
    						if( $( this ).children( 'ul' ).length > 0 ) {
    								$( this ).addClass( 'parent' );     
    						}
    				});				
                
        $('#button1')
        .unbind('click')
        .click( function() {
            $('#li_1 > a').parent().toggleClass( 'active' );
            $('#li_1 > a').parent().children( 'ul' ).slideToggle( 'fast' );
        });		
         
        
         $('#button2')
        .unbind('click')
        .click( function() {
            $('#li_2 > a').parent().toggleClass( 'active' );
            $('#li_2 > a').parent().children( 'ul' ).slideToggle( 'fast' );
        });		
        });    
            
    </script>
        
    </head>
    <body>
    <div id="wrapper">
        
    <div class="tree">
    		
       <ul id="list">
            <div tabindex="-1" id="1">
    		  <li id="li_1"><a>TestSuite 1</a>
    		  	<ul>
                    <div tabindex="-1" id="11"><li><a>Test 1 1 Started</a></li></div>
                    <div tabindex="-1" id="12"><li><a>Test 1 1 Passed</a></li></div>
                    <div tabindex="-1" id="13"><li><a>Test 1 2 Started</a></li></div>
                    <div tabindex="-1" id="14"><li><a>Test 1 2 Failed</a></li></div>
                    <div tabindex="-1" id="15"><li><a>Test 1 3 Started</a></li></div>
                    <div tabindex="-1" id="16"><li><a>Test 1 3 Passed</a></li></div>
    		  	</ul>
    		  </li>
           </div> 
           
           <div tabindex="-1" id="2">
    		  <li id="li_2"><a>TestSuite 2</a>
    		  	<ul>
                    <div tabindex="-1" id="21"><li><a>Test 2 1 Started</a></li></div>
                    <div tabindex="-1" id="22"><li><a>Test 2 1 Passed</a></li></div>
                    <div tabindex="-1" id="23"><li><a>Test 2 2 Started</a></li></div>
                    <div tabindex="-1" id="24"><li><a>Test 2 2 Failed</a></li></div>
    		  	</ul>
    		  </li>
           </div>
      </ul>
        
    </div>
        
    </div>
        
    <br><br>
    <input type="button" class="buttonLoad" id="button1" value="Expand TestSuite 1" /> 
    <input type="button" class="buttonLoad" id="button2" value="Expand TestSuite 2" />     
    
    </body>
    </html>

    【讨论】:

    • 很高兴你让它工作了,但这不是最有效的方法。您可以通过转到列表的子项来找到要切换的列表,而不是进入列表的锚点,然后是父项,然后是子项。我在上面的答案中添加了一个小提琴,也缓存了列表以提高效率。希望这会有所帮助。
    【解决方案2】:

    您可以通过以下方式打开/关闭您的列表:

        $list1 = $('#li_1').children('ul'); //cache your variables
        $list2 = $('#li_2').children('ul');
    
        $('#button1').click(function(){
          $list1.slideToggle( 'fast' );
          $list2.hide();
        }),
        $('#button2').click(function(){
          $list2.slideToggle( 'fast' );
          $list1.hide();
        });  
    

    添加小提琴https://jsfiddle.net/twrmkqug/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-03-10
      • 2015-04-20
      • 1970-01-01
      • 2011-09-18
      • 2016-05-17
      • 2018-05-25
      • 1970-01-01
      • 2020-05-21
      相关资源
      最近更新 更多