2012-10-10 18 views
6

JSTreeとAjaxに奇妙な問題があります。JSツリー - select_nodeが機能しない

私は

...

$.ajax({ 
    url: 'ajaxTreeView.php?method=edit&partid='+partId, 
    cache: false, 
    success: function(tree) 
    { 
     $('#treeViewer').html(tree); 
    }}); 

...使用(UL、LI、タグで)HTMLを生成アヤックス/ PHP要求を経由して、私のツリーを生成し、使用してコードにJStreeを活性化させます

options = 
{ 
    "core": { animation: 120 }, 
    "themes": { theme: 'corral', dots: true }, 
    "types": 
    { 
     "types": 
     { 
      "parent": { "icon": { "image": "images/custom/Legend/node_select.png" } }, 
      "child": { "icon": { "image": "images/custom/Legend/node_select_child.png" } }, 
      "current": { "icon": { "image": "images/custom/Legend/node.png" } } 
     } 
    }, 
    "plugins": [ "html_data", "types", "themes", "ui", "contextmenu", "cookies" ], 
    "ui": { "select_limit" : 1 }, 
    "cookies": { "save_selected" : false } 
}; 

$("#tree").jstree(options); 

ノードを簡単に選択できないようです。私はinitially_selectを試しましたが、これはうまくいかないようで、理想的ではありません。私が使用してみました

...私は、ハイパーリンクを経由して関数を呼び出すが、私はJStreeをinitaliseした後、私はそれを呼び出す場合は動作しない場合、これは動作します

$('#tree').jstree("select_node", '#ref565', true); 

私は警告がキックオフ前にツリーがレンダリングされていないことを... ...(このすべては、Ajaxの成功ルーチンで起こる)

$('#treeViewer').html(tree); 
$("#tree").jstree(options); 
alert('test'); 
$('#tree').jstree("select_node", '#ref565', true); 

を警告を追加することからご覧ください。

私は、この作品...

$('#treeViewer').html(tree); 
$("#tree").jstree(options); 
setTimeout(function() {selectNode(565);},1250); 
$('#tree').jstree("select_node", '#ref565', true); 

... setTimeoutメソッドを追加しました。

私は明らかに愚かです。間違った構文を使用していますか?ノードを選択するために遅延を設定する必要があるのはなぜですか?

助けてください。

答えて

9

ツリーのロード後に最初に特定のノードを選択する場合は、uiプラグインのinitially_selectオプションを使用することになっています。試してみましたが、投稿したサンプルコードでは使用されていません。あなたは正しいIDを供給していますか?

ノードをプログラムで選択するには、選択するノードがDOMに最初に表示されるまで待つ必要があります。タイムアウトコールバックを使用するのではなく、loaded.jstreeイベントにバインドする方が正しいと推測しています。ツリーの読み込みが完了し、すべてのツリー要素がDOM内にあり、そこでプログラムで選択する必要があります。利用状況を示す

例コード:

$(function() { 

    $("#tree") 
     .jstree({ 
      // List of active plugins 
      "plugins" : [ 
       "ui" 
      ], 

      // UI & core - the nodes to initially select and open will be overwritten by the cookie plugin 
      // the UI plugin - it handles selecting/deselecting/hovering nodes 
      "ui" : { 
       // this makes the node with ID node_4 selected onload 
       "initially_select" : [ "#ref565" ] 
      }, 
      // the core plugin - not many options here 
      "core" : { 
       // just open those two nodes up 
       // as this is an AJAX enabled tree, both will be downloaded from the server 
       "initially_open" : [ "#ref565" ] 
      } 
     }) 
     .bind("loaded.jstree", function (e, data) { 
        $('#tree').jstree("select_node", '#ref565', true); 
     }) 
}); 
+0

これは私が望んでいただけで何ですが、私はいくつかのグーグル後.bindで始まるコードの多くを参照してください。私は愚かである、これは私が書くべきであることを意味するか... $( '#tree')jstree.bind( "loaded.jstree"、function(event、data){ $( '#tree' ).jstree( "select_node"、 '#ref565'、true); }); もしそうなら、それは私のために働いていません。 –

+0

イベントをバインドする方法を示す私の例を更新しました。コーディングエラーのため何かがうまくいかない場合は、デバッガを使って理解してください。 –

+0

ありがとうございます。これらのイベントを理解するのがはるかに簡単であることがわかっています。ツリーのインスタンス化時に事前バインディングできることがわかりました。素晴らしい例、歓声:) –

関連する問題