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

Add attribute when new node create in 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"} } 但这对我没有帮助。

试试这个方法。它对我有用

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