How to get partially selected parent id in jstree

戏子无情 提交于 2019-12-11 07:38:02

问题


I am using "jstree" for treeview (Parent-Child) structure uisng checkboxes and returning id for each selected item. But here I am getting only children's Id and not getting selected parent Id. Kindly help me to get it too.

click [enter code here http://jsfiddle.net/2kwkh2uL/5807/ ]to find the code.


回答1:


Each object carries a key called parent, this contains the id. The answer below:

$('#container').jstree({
    'core' : {
        'data' : [
            { "text" : "P1", "children" : [
                { "text" : "O11" },
                { "text" : "O12" },
                { "text" : "O13"}     
            ]
            },
             { "text" : "P2", "children" : [
                { "text" : "O21" },
                { "text" : "O22" },
                { "text" : "O23"}     
            ]
            },
        ]
    },
    "plugins" : ["checkbox"]
});
$('#submitdiv').show();
		$('#submit').click(function(){
			var selectedElmsIds = [];
			var selectedElms = $('#container').jstree("get_selected", true);
			$.each(selectedElms, function() {
			    selectedElmsIds.push(
          {
            id: this.id,
            parent: this.parent
          }
         );
			    console.log('Id node: '+this.id);
			    console.log('Id parent: '+this.parent);
			});
			console.log(JSON.stringify(selectedElmsIds));
		});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jstree/3.3.3/jstree.min.js"></script>
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/jstree/3.3.3/themes/default/style.min.css">

<div id="container"></div>
  <div id="submitdiv" style="display:none;position:absolute">
  <button id="submit">submit</button>
</div>

Espero haberte ayudado, saludos.

PS: I apologize for my bad English, I speak Spanish and I'm using google translator.



来源:https://stackoverflow.com/questions/44006085/how-to-get-partially-selected-parent-id-in-jstree

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!