Thursday, 27 September 2018

How to populate jstree children with object of array and get it back on click

How to populate children and get its content on click of each of them

in the below code i wanted to have:

[{"cid":2091,"name":"name1","text":"Child 1"},{"cid":2095,"name":"name2","text":"A Child 2"},{"cid":2098,"name":"name3","text":"A Child 3"}]

instead of [ "Child 1", "A Child 2","A Child 3"]

i mean to say if i click Child 1 i should get {"cid":2091,"name":"name1","text":"Child 1"}

here is jsfiddle: http://jsfiddle.net/2Jg3B/2102/

$('#jstree').jstree({
        "json_data" : {
            "data" : [
                {
                    "data" : "A node",
                    "metadata" : { id : 23 },
                    "children" : [ "Child 1", "A Child 2","A Child 3"] // replace with [{"cid":2091,"name":"name1","text":"Child 1"},{"cid":2095,"name":"name2","text":"A Child 2"},{"cid":2098,"name":"name3","text":"A Child 3"}]
                },
                {
                    "attr" : { "id" : "li.node.id1" },
                    "data" : {
                        "title" : "Long format demo",
                        "attr" : { "href" : "#" }
                    }
                }
            ]
        },
        "plugins" : [ "themes", "json_data", "ui" ]
    });

   $("#jstree").bind(
        "select_node.jstree", function(evt, data){

          if(data.inst._get_parent().length <= 0 || data.inst._get_parent()===-1){
            console.log('data',data.inst.get_json());
            console.log('data.node.id',data);
          }else{
             var parent = data.inst._get_parent();
              console.log(data.inst.get_json(parent));
          }
            //selected node object: data.node;

      });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="http://old.static.jstree.com/v.1.0pre/jquery.jstree.js"></script>
<div id="jstree">
</div>

Please Note: if get the {"cid":2091,"name":"name1","text":"Child 1"} on click of Child 1 and so on.. plugin version can be either new or this one

Please help me thanks in advance!



from How to populate jstree children with object of array and get it back on click

No comments:

Post a Comment