2015-09-17 64 views
1

這是我的第一個問題。我使用Jquery treeview工作。我已經創建了一個樹形視圖,當我使用鼠標單擊一個父節點時,它擴展得很好。但我真的想通過點擊一個按鈕來展開每個父節點。這段代碼展示了我如何創建一個樹視圖。jquery treeview-如何通過單擊按鈕展開/摺疊給定的單個節點

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

 
a{ 
 
\t 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> 
 
\t <link rel="stylesheet" href="treeStyles.css"> 
 
\t <script src="http://code.jquery.com/jquery-1.7.2.min.js" type="text/javascript" > </script> 
 
\t 
 
\t <script type="text/javascript"> 
 

 
\t $(document).ready(function() { 
 
\t \t \t \t $('.tree li').each(function() { 
 
\t \t \t \t \t \t if($(this).children('ul').length > 0) { 
 
\t \t \t \t \t \t \t \t $(this).addClass('parent');  
 
\t \t \t \t \t \t } 
 
\t \t \t \t }); 
 
\t \t \t \t 
 
       /* $('.tree li.parent > a').click(function() { 
 
\t \t \t \t \t \t $(this).parent().toggleClass('active'); 
 
\t \t \t \t \t \t $(this).parent().children('ul').slideToggle('fast'); 
 
\t \t \t \t });*/ 
 
\t \t \t \t 
 
\t \t \t \t 
 
      
 
    $('#button1') 
 
    .unbind('click') 
 
    .click(function() { 
 
     $('.tree li.parent > a').parent().toggleClass('active'); 
 
     $('.tree li.parent > a').parent().children('ul').slideToggle('fast'); 
 
}); \t \t 
 
}); 
 
/*  
 
$('#button1').click(function(){ 
 
    $('#li_1').children('ul').slideToggle('fast'); 
 
}), 
 
$('#button2').click(function(){ 
 
    $('#li_2').children('ul').toggle(); 
 
}); */  
 
     
 
\t </script> 
 
</head> 
 
<body> 
 
<div id="wrapper"> 
 
    
 
<div class="tree"> 
 
\t \t 
 
    <ul id="list"> 
 
     <div tabindex="-1" id="1"> 
 
\t \t <li id="li_1"><a>TestSuite 1</a> 
 
\t \t \t <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> 
 
\t \t \t </ul> 
 
\t \t </li> 
 
     </div> 
 
     
 
     <div tabindex="-1" id="2"> 
 
\t \t <li id="li_2"><a>TestSuite 2</a> 
 
\t \t \t <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> 
 
\t \t \t </ul> 
 
\t \t </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()函數來展開/摺疊每個測試套件programmatically.Also我需要展開/摺疊通過參考其ID的節點。

我試圖使用openMyOwnBranchInTree()函數展開/摺疊每個父節點,但它不起作用。如何改進我的代碼以獲得我的結果或者是否有任何簡單的方法來完成此任務? 歡迎提供建議.. 謝謝。

已編輯。我修改了以前的代碼,並通過註釋表明我在哪裏更改了這些代碼。)我評論了myLoadFunction1()和myLoadFunction2()函數,並添加了jQuery代碼。

現在#button1展開/摺疊所有子節點..但如何去我的目標?可以請任何人幫助我嗎? 謝謝。

回答

0

你可以用這個切換您開啓/關閉名單:

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

+0

謝謝你回答我的問題,但它不工作.... – halpMe

0

最後我自己找到答案...... :) 我們可以通過使用JQuery來完成這個任務。不需要JavaScript。 這裏是代碼..

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

 
a{ 
 
\t 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> 
 
\t <link rel="stylesheet" href="treeStyles.css"> 
 
\t <script src="http://code.jquery.com/jquery-1.7.2.min.js" type="text/javascript" > </script> 
 
\t 
 
\t <script type="text/javascript"> 
 

 
\t $(document).ready(function() { 
 
\t \t \t \t $('.tree li').each(function() { 
 
\t \t \t \t \t \t if($(this).children('ul').length > 0) { 
 
\t \t \t \t \t \t \t \t $(this).addClass('parent');  
 
\t \t \t \t \t \t } 
 
\t \t \t \t }); \t \t \t \t 
 
      
 
    $('#button1') 
 
    .unbind('click') 
 
    .click(function() { 
 
     $('#li_1 > a').parent().toggleClass('active'); 
 
     $('#li_1 > a').parent().children('ul').slideToggle('fast'); 
 
    }); \t \t 
 
     
 
    
 
    $('#button2') 
 
    .unbind('click') 
 
    .click(function() { 
 
     $('#li_2 > a').parent().toggleClass('active'); 
 
     $('#li_2 > a').parent().children('ul').slideToggle('fast'); 
 
    }); \t \t 
 
    });  
 
     
 
</script> 
 
    
 
</head> 
 
<body> 
 
<div id="wrapper"> 
 
    
 
<div class="tree"> 
 
\t \t 
 
    <ul id="list"> 
 
     <div tabindex="-1" id="1"> 
 
\t \t <li id="li_1"><a>TestSuite 1</a> 
 
\t \t \t <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> 
 
\t \t \t </ul> 
 
\t \t </li> 
 
     </div> 
 
     
 
     <div tabindex="-1" id="2"> 
 
\t \t <li id="li_2"><a>TestSuite 2</a> 
 
\t \t \t <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> 
 
\t \t \t </ul> 
 
\t \t </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>

+0

高興你得到它的工作,但它不是最有效的方式。你可以找到你的列表來切換列表的孩子,而不是進入列表的錨點,然後是父母,然後是孩子。我在上面的答案中加了一個小提琴,緩存了這個列表,以使它更有效率。希望這可以幫助。 – amespower

相關問題