2012-03-12 18 views
5

jsTreeはJSONページからデータをロードしており、正しく表示されます。私はデフォルトでルートノードを選択しようとしていますが、動作させることはできません。ここで最初にノードを選択するためにjQueryプラグインjsTreeを取得できません

は私のjQueryのです:ここでは

$(function() { 
    $("#demo1").jstree({ 
     "plugins" : [ "themes","json_data","ui" ], 
     "json_data" : { 
      "ajax" : { 
       "url" : "categorytreejson.asp" 
      }, 
      "ui" : { 
       "initially_select" : [ "root" ] 
      }, 
     } 
    }); 
}); 

はJSONLintを使用して検証しcategorytreejson.aspからの私のJSONです:ここでは

{ 
    "data": "root", 
    "attr": { 
    "id": "root" 
    }, 
    "children": [ 
    { 
     "data": "Photography", 
     "attr": { 
     "id": "Photography" 
     }, 
     "children": [ 
     { 
      "data": "Lenses", 
      "attr": { 
      "id": "Lenses" 
      }, 
      "children": [ 
      { 
       "data": "Telephoto", 
       "attr": { 
       "id": "Telephoto" 
       } 
      }, 
      { 
       "data": "Macro", 
       "attr": { 
       "id": "Macro" 
       } 
      }, 
      { 
       "data": "Other", 
       "attr": { 
       "id": "Other" 
       } 
      } 
      ] 
     } 
     ] 
    } 
    ] 
} 

は、結果のHTMLです:

<li class="jstree-last jstree-open" id="root"><ins class="jstree-icon">&nbsp;</ins><a class="" href="#"><ins class="jstree-icon">&nbsp;</ins>root</a> 
    <ul style=""> 
     <li class="jstree-closed" id="Photography"><ins class="jstree-icon">&nbsp;</ins><a class="" href="#"><ins class="jstree-icon">&nbsp;</ins>Photography</a> 
      <ul> 
       <li class="jstree-last jstree-closed" id="Lenses"><ins class="jstree-icon">&nbsp;</ins><a href="#"><ins class="jstree-icon">&nbsp;</ins>Lenses</a> 
        <ul> 
         <li class="jstree-leaf" id="Telephoto"><ins class="jstree-icon">&nbsp;</ins><a href="#"><ins class="jstree-icon">&nbsp;</ins>Telephoto</a> 
         </li> 
         <li class="jstree-leaf" id="Macro"><ins class="jstree-icon">&nbsp;</ins><a href="#"><ins class="jstree-icon">&nbsp;</ins>Macro</a> 
         </li> 
         <li class="jstree-last jstree-leaf" id="Other"><ins class="jstree-icon">&nbsp;</ins><a href="#"><ins class="jstree-icon">&nbsp;</ins>Other</a> 
         </li> 
        </ul> 
       </li> 
      </ul> 
     </li> 
</li> 
</ul> 
</li> 
</ul> 
</li> 

そして放火犯によって表示結果のデータ・オブジェクト:

args: [] 
inst: Object { data={...}, get_settings=function(), _get_settings=function(), more...} 
rlbk: false 
rslt: undefined 

私は、結果が空であるため、問題のほとんどがあると仮定していますが、私はなぜわかりませんか?

答えて

4

のUIプラグイン設定をjson_dataプラグインの設定に入れています。
あなたはそれを持ち出す必要があり、それは動作します。

"json_data" : { 
     "ajax" : { 
      "url" : "categorytreejson.asp" 
     } 
    }, 
    "ui" : { 
     "initially_select" : [ "root" ] 
    } 
+1

そうだねuse-、常に最初のノードを選択します。もう一組の目があってうれしいです。 ありがとうございます。 私はまだrsltオブジェクトを持っていない理由を理解できません。 – Chris

0

@Zheileman答えは私のものよりもはるかに優れていますが、私はこの問題を解決する別の方法を共有するために興味深いものになるだろうと思った:

文書に私はjstreeイベントの使用でこれをやった準備:

jstreeのバインド機能で
var i = 0; 
      $.each($("#container").jstree()._model.data, function (value, index) { 
       if (i == 1) { 
        $("#container").jstree(true).select_node(value, false, false); 
        $("#container").jstree(true).open_node(value); 
        return false; 
       } 
       i++; 
      }) 
1

ツリーは負荷のかかった

$('#tree_id').jstree('select_node', 'ul > li:last'); 
ときに最後のノードを選択するために、以下の行を追加します。

$('#tree_id').jstree('select_node', 'ul > li:first'); 

例 -

$('#tree_id').bind("loaded.jstree", function(e, data) { 
    $(this).jstree("open_all"); 
    $('#tree_id').jstree('select_node', 'ul > li:last'); 
}) 
関連する問題