2013-04-05 32 views
0

我使用jstree來顯示/管理類別樹。我取了樹Ajax調用並返回以下響應:jstree記得打開/關閉的樹節點

0: {id:35, name:amrs trade, parent_id:null} 
1: {id:36, name:trips, parent_id:35} 
2: {id:37, name:tribute, parent_id:null} 
3: {id:38, name:music, parent_id:null} 
4: {id:39, name:recordings, parent_id:38} 
5: {id:40, name:live shows, parent_id:38} 
6: {id:41, name:others, parent_id:null} 
7: {id:42, name:investments, parent_id:null} 
8: {id:43, name:gold & silver, parent_id:42} 
9: {id:44, name:debentures, parent_id:42} 
10: {id:46, name:production, parent_id:35} 
11: {id:54, name:real estate, parent_id:42} 

我使用json_data選項來呈現jstree:

$("#incomeCategoryTree").jstree({ 
    "json_data" : { 
     "data" : income, 
     "progressive_render" : true 
    }, 
    "plugins" : [ "themes", "json_data" ] 
}) 
.bind("open_node.jstree close_node.jstree", function (e) { 
    console.log(e); 
}); 

我要記住每個動作用戶已經(打開或關閉任何樹節點),所以我綁定了兩個open_nodeclose_node操作。我的問題是,該事件已對節點剛點擊的任何信息:

.Event {type: "open_node", timeStamp: 1365192438814, jQuery183029903660411946476: true, isTrigger: true, exclusive: undefined…} 

currentTarget: div#incomeCategoryTree.jstree jstree-0 jstree-default jstree-focused 
data: null 
delegateTarget: div#incomeCategoryTree.jstree jstree-0 jstree-default jstree-focused 
exclusive: undefined 
handleObj: Object 
isTrigger: true 
jQuery183029903660411946476: true 
namespace: "jstree" 
namespace_re: /(^|\.)jstree(\.|$)/ 
result: undefined 
target: div#incomeCategoryTree.jstree jstree-0 jstree-default jstree-focused 
timeStamp: 1365192438814 
type: "open_node" 
__proto__: Object 

我猜我失蹤,但jstree文檔是恕我直言非常差,大多數的選項,甚至沒有提及...

+0

我不確定我是否真正理解了您要查找的內容,但也許cookie插件可以提供幫助? http://www.jstree.com/documentation/cookies – 2013-09-13 07:38:18

回答

2

我已經成功地做到以下幾點:

$("#incomeCategoryTree").jstree({ 
    "json_data" : { 
     "data" : income, 
     "progressive_render" : true 
    }, 
    "plugins" : [ "themes", "json_data" ] 
}) 
.bind("open_node.jstree close_node.jstree", function (event, data) { 
    var state = event.type == "open_node" ? "open" : "closed"; 
    IncomeCategoryControl.setState(data.rslt.obj.attr("id"), state) 
}); 

,這是管理樹數據對象

var IncomeCategoryControl = { 
    data: null, 
    fetchData: function() { 
     $.ajax({ 
      type: "GET", 
      dataType: "json", 
      context: this, 
      async: false, 
      url: "../php/client/json.php", 
      data: { 
       type: "incomeCategories" 
      } 
     }).done(function(response) { 
      this.data = response; 
     }); 
    }, 
    getData: function() { 
     if (this.data == null) { 
      this.fetchData(); 
     } 
     return this.data; 
    }, 
    setState: function(id, value) { 
     var found = $(this.getData()).map(function() { 
      return (this.id == id) ? this : null; 
     }); 
     if (found.length) { 
      found[0].state = value; 
      return true; 
     } else { 
      return false; 
     } 
    } 
};