如何在创建新的jstree节点后设置id?

时间:2010-07-05 12:39:01

标签: php jquery json jstree

我正在使用jsTree 1.0。并有这个代码:

$(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?

2 个答案:

答案 0 :(得分:3)

节点变量必须声明为:

var node = data.rslt.obj;

并称为:

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

答案 1 :(得分:0)

我会在成功回调中做alert(jdata)

确保服务器返回安全的JSON并且存在实际的new_id属性。