【问题标题】:Add attribute when new node create in JSTree在 JSTree 中创建新节点时添加属性
【发布时间】:2017-07-05 13:14:14
【问题描述】:

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"} } 但这对我没有帮助。

【问题讨论】:

  • 试试data[i].attr = { title: "if", value:"expression"}

标签: jquery jstree


【解决方案1】:

试试这个方法。它为我工作

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

【讨论】:

  • CurrentNode 是我当前的节点,所以请注意这个
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多