2012-01-27 65 views
0

私はjstreeのインスタンスを持っています。それはjsonとのajaxです。Jstreeがidにli要素を割り当てていない

ここでは、ツリーの作成方法を説明します。

 $("#tree").jstree({ 
      "plugins" : ["themes", "json_data", "ui"], 
      "json_data" : { 
       "ajax" : { 
        "type": 'GET', 
        "url": function (node) { 
         var nodeId = ""; 
         var url = "" 
         if (node == -1) 
         { 
          //first url called is static 
          url = "myUrl"; 
         } 
         else 
         { 
          //click on a node "li", get the id, append it to the url. 
          nodeId = node.attr('id'); 
          url = "myUrl" + nodeId; 
         } 

         return url; 
        }, 
        "success": function (new_data) { 
         //get the data topNode out of the json object 
         new_data = new_data.topNode; 

         return new_data; 
        } 
       } 
      }, 

     }); 

ここに出力があります。ツリーがレンダリングされるとき

<div id="tree" class="jstree-classic jstree jstree-0 jstree-focused jstree-default"> 
    <ul> 
     <li class="jstree-closed jstree-last"> 
      <ins class="jstree-icon">&nbsp;</ins> 
      <a class="" href="#">Item 1</a> 
     </li> 
    </ul> 
</div> 

しかし<LI>要素は、IDを持っていません。 <LI>に自動的に追加する必要があります。私は何が欠けていますか?

+0

は、JSONであなたのAJAXリターンIDをしていますか? – Radek

+0

はい。私がnewData.topNode.idに警告すると、私は正しい応答を得ます。 –

+0

あなたの 'new_data'の構造は何ですか? – Radek

答えて

1

あなたのデータは、Ajaxので、それだけでATTRとidに属性を変更

{"topNode":{"data":"Item 1", 
      "children":[], 
      "state":"closed", 
      "id":"1", 
      "attributes":{"class":"editContainerL‌​ink", 
          "href":"#", 
          "id":"1" 
          } 
      } 
} 
+0

私はこの形式に変更しましたが、まだIDを入力しません。 "children":[]、 "state": "closed"、 "属性":{"class": "editContainerL ink"、 "{" topNode ":{"データ " href ":"# "、" id ":" 1 "} } } –

+0

IDは2回定義する必要がありますか? –

+1

属性エリアに移動する両方のソリューションを組み合わせて、次に... 「属性」を「attr」に変更すると問題が解決しました。 idは現在LIに移入されています。 {"topNode":{"データ": "アイテム1"、 "子":[]、 "状態": "閉じ"、 "attr":{"class": "editContainerL ink" "href": "#"、 "id": "1" } } } –

0

ようになりセクション

{"topNode":{"data":"Item 1", 
      "children":[], 
      "state":"closed", 
      "id":"1", 
      "attributes":{"class":"editContainerL‌​ink","href":"#"} 
      } 
} 

属性あなたは"id":"1"を移動する必要がある呼び出しから返された以下の場合'a'ノードに表示されます。私はまた、 '李'ノードのidを欲しいですが、今まで私のメタデータの試行は成功していません。

// the `metadata` property will be saved using the jQuery `data` function on the `li` node 
"metadata" : "a string, array, object, etc", 

http://www.jstree.com/documentation/json_data

関連する問題