在jstree复选框中添加新节点名称

时间:2017-12-14 23:40:32

标签: javascript php jquery jstree

我第一次使用jstree开始使用json / PHP 我想知道如何在复选框中设置$array['mynewkey']的值。我怎样才能得到它。

这是我的代码:

<script type="text/javascript">
    (function($){
        // check in php file or dir and create another array varible called extension contain file / dir value
        // switch the extention variable here
        $('#tree-container').jstree({
            "plugins" : ["contextmenu", "types", "wholerow","checkbox"],
               'core' : {
                   'data' : <?php echo json_encode($array); ?>
                }
         });
     })(jQuery132);
</script>

1 个答案:

答案 0 :(得分:0)

您可以尝试此示例

<div id="jstree"></div>
<button id="sam">Create node</button>
<button id="get" onclick="submitMe()">Get node Id</button>

<script>
$(function() {
 // Sample data 
  var data = [{
    "id": "1",
    "parent": "#",
    "text": "Simple root node"
  }, {
    "id": "2",
    "parent": "#",
    "text": "Root node 2"
  }, {
    "id": "3",
    "parent": "2",
    "text": "Child 1"
  }, {
    "id": "4",
    "parent": "2",
    "text": "Child 2"
  }];

  $("#jstree").jstree({
    "core": {
      // so that create works
      "check_callback": true,
      "data": data // You can your own data
    },
    "plugins": ["checkbox"],
  });

  // To create new node
  $("#sam").on("click", function() {
    $('#jstree').jstree().create_node('#', {
      "id": "5",
      "text": "newly added"
    }, "last", function() {
      alert("Created Successfully...");
    });
  });
});

// To get all the node
function submitMe() {
  var checked_ids = [];
  var selectedElms = $('#jstree').jstree("get_selected", true);
  $.each(selectedElms, function() {
    checked_ids.push(this.id);
    console.log(checked_ids);
 });
}
</script>