2010-07-05 48 views
2

我正在使用jsTree 1.0。而有這樣的代碼:如何在創建新的jstree節點後設置id?

$(document).ready(function() { 
    $("#folders_tree").jstree({ 
     "core": { 
      "initially_open": ["root"] 
     }, "html_data": { 
      "data": '<?= $folders; ?>' 
     }, "themes": { 
      "theme": "default", 
      "dots": true, 
      "icons": true, 
      "url": "<?= Yii::app()->request->baseUrl ?>/css/jstree/themes/default/style.css" 
     }, "contextmenu": { 
      "items": { 
       "create": { 
        "label": "Create", 
        "action": function (obj) { 
         this.create(obj); 
        }, "_disabled": false, 
        "_class": "add", 
        "separator_before": false, 
        "separator_after": false, 
        "icon": false 
       }, "rename": { 
        "label": "Rename", 
        "action": function (obj) { 
         this.rename(obj); 
        }, "_disabled": false, 
        "_class": "rename", 
        "separator_before": false, 
        "separator_after": false, 
        "icon": false 
       }, "remove": { 
        "label": "Delete", 
        "action": function (obj) { 
         this.remove(obj); 
        }, "_disabled": false, 
        "_class": "delete", 
        "separator_before": true, 
        "separator_after": false, 
        "icon": false 
       }, "ccp": false 
      } 
     }, 

     "plugins": ["themes", "html_data", "ui", "crrm", "contextmenu"] 
    }); 

    /* Callbacks */ 

    var folders = $("#folders_tree"); 

    folders.bind("create.jstree", function (e, data) { 
     var parent_id = data.rslt.parent[0].id; 
     var name = data.rslt.name; 
     var node = data.args[0]; 
     var dataArray = { 
      "ref_folder": parent_id, 
      "name": name 
     }; 
     var dataString = JSON.stringify(dataArray); 
     $.ajax({ 
      type: 'POST', 
      url: '<?= Yii::app()->createUrl(' 
      ajax/createfolder ') ?>', 
      data: { 
       data: dataString 
      }, success: function (jdata) { 
       var json_data = JSON.parse(jdata); 
       // Here's! This code is not working. Id is not set. 
       $(node).attr("id", json_data.new_id); 
      }, dataType: 'text' 
     }); 
    }); 
});  


$(node).attr("id", json_data.new_id) // this code is not working. 

我卡在這個:(我如何設置這個ID

+0

「不工作」是沒有多大的錯誤描述。你可以說得更詳細點嗎? – Tomalak 2010-07-05 12:43:08

+0

已創建節點的Html屬性「ID」未設置爲存儲在json_data.new_id中的ID。 – andser 2010-07-05 12:49:31

回答

3

節點變量必須聲明爲:

var node = data.rslt.obj; 

而被稱爲:

node.attr("id", json_data.new_id); 
0

我將在成功回調做alert(jdata)

請確保服務器正在返回安全的JSON並確實存在new_id屬性。

+0

是的。它是存在的。答案是:{「new_id」:「node1106」} – andser 2010-07-05 12:53:02

+0

然後'node'不是你想要的元素。 – 2010-07-05 12:55:24

+0

但是我怎樣才能設置\獲得節點的ID? – andser 2010-07-05 14:00:00