Greetings, I am using jsTree to generatate my hierarchical data. JsTree is generated as follows:
$(function() {
$("#industries").tree({
data: {
type: "json",
opts: {
url: "/Admin/GetIndustries/"
}
}
});
});
it works find and the jsonresult is something like:
[{"attributes":[],"data":{"title":"Adwokaci, Notariusze","id":"1a051101-c3fa-48f2-b2e1-c60d1b67ea22"},"children":[{"attributes":[],"data":{"title":"Kancelarie adwokackie","id":"26d6cff1-3c7f-4a2f-bf5a-422e08127b43"
my question is: how can I save id of selected node in some hidden field? I do something like this:
<script type="text/javascript">
$("#industries").click(function() {
var tree = $.tree.reference("industries");
var t = $.tree.focused(); if (t.selected) t.selected; else alert("Select a node first");
alert(t.id);
});
but it does not work. I get in my alert window "undefined". Can someone please help me?
EDIT: I've changed the jstree instance as follows:
$(function() {
$("#industries").tree({
callback: {
onselect: function(NODE, TREE_OBJ) {
alert(NODE.id);
}
},
data: {
type: "json",
opts: {
url: "/Admin/GetIndustries/"
}
}
});
});
and i get empty alertt
Or just bind the select node:
$("#industries").tree({
callback: {
onselect: function(NODE, TREE_OBJ) {
alert(NODE.id);
}
},
data: {
type: "json",
opts: {
url: "/Admin/GetIndustries/"
}
}
})
.bind("select_node.jstree", function (NODE, REF_NODE) {
var a = $.jstree._focused().get_selected();
}
});
Try looking at the variable a for the ID, or NODE. I was actually using REF_NODE to get
I did not check all the answers but saw that you didnt select any so decided to post a method that worked for me
$(function () {
$("#demo1")
.bind("select_node.jstree", function (event, data) {
var selectedObj = data.rslt.obj;
alert(selectedObj.attr("id") + selectedObj.attr("data"));
})
this is if you want the id and the title of the node. hope this helps
You can use it in your bind()
function like this:
.bind("check_node.jstree", function(e, data) {
//console.log(data.rslt.obj.attr(\'id\'));
if (data.rslt.obj !== undefined && data.rslt.obj.attr(\'id\') !== undefined) {
jQuery.ajax({
async: false,
type: "POST",
dataType: "json",
url: "' . Yii::app()->createUrl('adsmanager/adsfields/ajaxappendcategory') . '",
data: {
"id" : data.rslt.obj.attr(\'id\'),
"itemId" : "' . Yii::app()->getRequest()->getParam('id') . '",
},
success: function(r) {
if(r === undefined || r.status === undefined || !r.status) {
data.rslt.obj.removeClass(\'jstree-checked\');
data.rslt.obj.addClass(\'jstree-unchecked\');
} else {
niceBox(\'ok, checked\');
}
}
});
}
return true;
});
The same with uncheck action.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With