在JSTree中创建新节点时添加属性

时间:2017-07-04 09:33:46

标签: jquery jstree

如何在JStree中创建新节点时添加属性?

当btnadd点击那个时候我正在添加新节点。 节点创建成功但现在我想添加属性。

我的Html

<div id="jstree">
</div>

<button id="btnadd">create node</button>

<div id="CurentNodeName"></div>
<div id="CurentNodeId"></div>    

脚本

<script>
    var CurrentNode;
    $(function () {
        var data = [
           { "id": "ajson1", "parent": "#", "text": "Simple root node" },
           { "id": "ajson2", "parent": "#", "text": "Root node 2" },
           { "id": "ajson3", "parent": "ajson2", "text": "Child 1" },
           { "id": "ajson4", "parent": "ajson2", "text": "Child 2" },
        ];
        $("#jstree").jstree({
            "core": {
                // so that create works
                "check_callback": true,

                "data": data
            } 
        }).on('create_node.jstree', function (e, data) {
            console.log('saved');
        });

        // listen for event
        $('#jstree').on('changed.jstree', function (e, data) {
            var i, j, r = [], s = [];
            for (i = 0, j = data.selected.length; i < j; i++) {
                r.push(data.instance.get_node(data.selected[i]).text);
                CurrentNode = data.selected[0];
                console.log(data.selected[0]);

            }

            $('#CurentNodeName').html('Selected: ' + r.join(', '));
            $('#CurentNodeId').html('Selected Id: ' + CurrentNode);

        }).jstree();

        // create the New Node when Button Click
        $("#btnadd").on("click", function () {

            $('#jstree').jstree().create_node(CurrentNode, { "id": "ajson5" + CurrentNode, "text": "newly added" }, "last", function () {                   
           });   
    });

</script>

我也试试 “attr”:{title:“if”,value:“expression”}}但这对我没有帮助。

1 个答案:

答案 0 :(得分:0)

试试这种方式。它为我工作

$('#jstree').jstree().create_node(CurrentNode, { 
    li_attr: { "path": "path" },"text": "name" }, 
    "last", function () { }, true);
相关问题