2012-03-28 21 views
0

我正在使用jsTree jQuery插件。我有一個問題,我的樹在最初開始擴展。jsTree根據initial_selected參數的順序擴展節點?

這是用戶看到在加載頁面的內容:

enter image description here

這是我想用戶看到:

enter image description here

下面是我使用的HTML供測試用。我發現這個行爲是由initially_select參數的順序決定的。當我有'initially_select': ['2', '1']時,樹最初顯示爲展開。當我有'initially_select': ['1', '2']樹最初顯示關閉。通過首先選擇最頂層的節點,樹仍然關閉。但是,在我的真實用例中,我的節點上有一些有意義的id,這些id不容易排序;我沒有[簡單]的方法來輕鬆確定哪些項目是我的樹中的「最頂層」。

我試過了建議的解決方案here。我在loaded.jstree回調中調用了close_all,但似乎在選擇initially_selected項之前,加載的事件回調運行。因此,jsTree關閉所有節點(已關閉),然後選擇幾個節點,導致其中許多節點再次展開。

我只想讓我的jsTree在用戶加載頁面時完全摺疊。

這裏的HTML演示:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<html lang="en"> 
<head> 
    <title>jsTree Test</title> 
    <meta http-equiv="content-type" content="text/html;charset=utf-8" /> 
</head> 
<body> 
    <div id="my_tree"> 
    <ul> 
     <li> 
     <a id="1">Item 1</a> 
     <ul> 
      <li><a id="2">Item 1.1</a></li> 
     </ul> 
     </li> 
    </ul> 
    </div> 
    <script type="text/javascript" src="jquery-1.7.1.min.js"></script> 
    <script type="text/javascript" src="jquery.jstree.js"></script> 
    <script type="text/javascript"> 
     $(document).ready(function() { 
      $('#my_tree').jstree(
       {'plugins': ['themes', 'checkbox', 'html_data', 'ui'], 
       'checkbox': {'override_ui': true, 'real_checkboxes': true}, 
       'ui': {'initially_select': ['2', '1']}, 
       'themes': {'icons': false}} 
      ); 
     }); 
    </script> 
</body> 
</html> 

回答

0

嘗試用jQuery 1.6.4測試它。

我在使用jQuery和jQuery 1.7.x時遇到問題,不得不恢復到1.6.4