2010-03-11 56 views
3

我正在使用帶有「Checkbox」插件的jsTree jQuery插件並使用異步http請求來延遲加載樹的每個級別。所有的作品都很棒,除了我不能讓樹在第一級之後預選某些節點。我正在使用「selected」屬性來提供一組ID來預先選擇。樹的頂層ID已正確預選。但是,級別加載時,樹的較低級別中的ID未選中。我錯過了什麼嗎?如何使用jsTree jQuery插件預選節點

下面是構造函數代碼:

var myArrayOfIDs = new Array(); 
    myArrayOfIDs[0] = '123'; //etc... 

    $(sDivID).tree(
     { 
      data : { 
       async : true, 
       opts : {url : sURL} 
      }, 
      plugins:{ 
       "checkbox" : {three_state : false} 
      }, 
      selected : myArrayOfIDs, 
      ui:{ 
       theme_name : "checkbox", 
       dots : false, 
       animation : 400 
      }, 
      callback : { 
       beforedata : function(NODE, TREE_OBJ) { return { id : $(NODE).attr("id") || 0, rand : Math.random().toString() } } 
      } 
     } 
    ) 

回答

2

嗯,你的代碼看起來好像沒什麼問題。

我參加了一個稍微不同的方法在我的解決方案,因爲我想在服務器即你SURL告訴我哪些項目應選擇 - 然後jsTree的onload回調我選擇它們。

更新:我已經更新了我jsTree博客文章到最新版本jsTree here

-Matt

+0

由於在自己的博客mattfrear.com/2010/05/19/jstree馬特Frear評論,這不能工作了。所以我們沒有任何其他解決方案? – Vinh 2011-06-07 14:36:47

+0

@Vinh我已經更新了我的博客文章,因此它現在可以與1.0版本的jsTree一起使用。 – 2011-12-20 10:52:50

+0

嗨,我想感謝你@Matt Frear。我用你的文章,它幫助了我很多。但是現在我想讓選定的節點onload。它不適合我。你能看到我的問題嗎? http://stackoverflow.com/questions/17519593/how-to-make-some-selected-items-as-checked-on-load-in-jstree-selected-selec – 2013-07-08 05:44:47

6
$(".jstree").jstree({ 
    "plugins" : [ "themes", "html_data", "checkbox", "ui" ], 
    "checkbox": { 
     "real_checkboxes": true, 
     "real_checkboxes_names": function (n) { 
      return [n[0].id, 1]; 
     }, 
     "two_state": true 
    } 
}).bind("loaded.jstree", function (event, data) { 
    $('li[selected=true]').each(function() { 
     $(this).removeClass('jstree-unchecked').addClass('jstree-checked'); 
    }); 
}); 

我用這對jstree的最新版本。

+0

謝謝,它爲我工作,我需要它。 – 2013-07-08 05:46:50

+0

從我的理解,當用戶取消選擇的節點,這將只處理CSS和事業的問題。我能夠預選使用本示例中的節點[這裏](http://stackoverflow.com/a/34877409/4896260) – usr4896260 2016-02-02 12:53:12

1

這是我如何更新選擇樹被加載後:

$('#jstree_div') 
     .on('changed.jstree', treeChanged) 
     .on('loaded.jstree', treeLoaded) 
     .jstree({ 
     'plugins': ["checkbox"], 
     'core': { 
      'data': { 
       'url': '/xmldata/getcategories' 
      } 
     } 
    }); 

function treeLoaded(event, data) { 
    data.instance.select_node(['2', '5']); //node ids that you want to check 
}