2012-03-01 22 views
2

我試圖創建一個'jsTree'樹視圖,它從.Net webservice獲取數據。 除a節點的href屬性外,一切正常。無論我嘗試什麼,它總是呈現爲'#'。 據我所知,任何數據對象中的所有屬性都被複制到a節點。jsTree - 在Json數據中設置href屬性

下面是我當前的json對象的一個​​例子。反正可以找出爲什麼href屬性沒有複製到節點?

[ { "attributes" : { "id" : "rootnode_2", 
     "rel" : "root2" 
     }, 
    "children" : [ { "attributes" : { "id" : "childnode_9", 
       "rel" : "folder" 
      }, 
      "children" : [ { "attributes" : { "id" : "childnode_23", 
        "rel" : "folder" 
        }, 
       "children" : null, 
       "data" : { "href" : "http://www.google.com", 
        "title" : "Test_Below_1" 
        }, 
       "state" : null 
       } ], 
      "data" : { "href" : "http://www.google.com", 
       "title" : "Test_1" 
      }, 
      "state" : null 
     }, 
     { "attributes" : { "id" : "childnode_10", 
       "rel" : "folder" 
      }, 
      "children" : [ { "attributes" : { "id" : "childnode_24", 
        "rel" : "folder" 
        }, 
       "children" : null, 
       "data" : { "href" : "http://www.google.com", 
        "title" : "Test_Below_2" 
        }, 
       "state" : null 
       } ], 
      "data" : { "href" : "http://www.google.com", 
       "title" : "Test_2" 
      }, 
      "state" : null 
     } 
     ], 
    "data" : { "href" : "http://www.google.com", 
     "title" : "Glatt" 
     }, 
    "state" : "closed" 
    } ] 

這是我如何初始化樹;

$("#jstreejson").jstree({ 
     json_data : { 
      "data": treeObject 
     }, 
     themes: { 
      "theme": "apple", 
      "dots": true, 
      "icons": true, 
      "url": "/Scripts/themes/apple/style.css" 
     }, 
     plugins: ['core', 'themes', 'json', "json_data"] 
    }); 

回答

2

所以......我不確定這是完全正確的。你無法控制遠錨屬性,因爲我知道,但你可以在JSON不添加東西到attr哈希值,然後使用select_node.jstree事件,打開所需的鏈接,即:

.bind("select_node.jstree", function (e,data) { 
    var href_address = data.rslt.obj.attr("whatever"); 
    // open desired link 
} 
+0

嘗試將href值添加到'attr'哈希值,而不是'data'哈希值。 – Amir 2012-03-01 13:57:01

+0

完美運作。謝謝! – Erik 2012-03-01 14:02:04