You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
<div id="jstree"></div>
<script type="text/javascript">
var linkObj;
function CustomTreeViewClickHandler(node, selected, event) {
$('.clicked').removeClass('clicked');
console.log("error 15");
linkObj = $(node.li); // Convert the node object to a jQuery element
if (linkObj) {
linkObj.addClass('clicked');
$("#Project").val(linkObj.attr("data-id"));
console.log("error 4");
__postback();
} else {
// Handle the case when linkObj is null
console.error("linkObj is null.");
}
}
$(function () {
$('#jstree').jstree({
'core': {
'data': {
'url': '@Url.Action("ProjectTreeData")',
'data': function (node) {
var idToSend = (node.id === '#') ? 'source' : node.id;
console.log(idToSend); // Log the id that will be sent to the server
return { 'id': idToSend };
}
}
}
});
$('#jstree').on('changed.jstree', function (e, data) {
console.log("error 2");
CustomTreeViewClickHandler(data.node, data.selected, e);
});
});
</script>`
Also there is controller method:
public ActionResult ProjectTreeData(string root)
{
List<JsTreeNode> nodes = new List<JsTreeNode>();
Project rootProject = null;
if (root == "source")
{
rootProject = context.Projects.Single(x => x.ID == Project.DEFAULT);
bool hasChildren = context.Projects.Any(x => x.Parent != null && x.Parent.ID == rootProject.ID);
nodes.Add(new JsTreeNode()
{
id = rootProject.ID.ToString(),
parent = "#", // "#"" means it's a root node
text = rootProject.Name,
state = new State
{
opened = false,
disabled = false,
selected = false
}
});
}
else
{
List<ProjectLevel> levels = context.ObjectContext.ExecuteStoreQuery<ProjectLevel>("select ID, Level from [fGetProjectsLevel](-1)").ToList();
int currentId = Convert.ToInt32(root);
rootProject = context.Projects.Single(x => x.ID == currentId);
IEnumerable<Project> projectChildren = context.Projects.Where(x => x.Parent != null && x.Parent.ID == rootProject.ID).OrderBy(x => x.Name);
foreach (Project projectChild in projectChildren)
{
int level = levels.Where(x => x.ID == projectChild.ID).ToArray()[0].Level;
// Create an array of child nodes here
JsTreeNode[] childNodes = GetChildNodesForProject(projectChild);
nodes.Add(new JsTreeNode()
{
id = projectChild.ID.ToString(),
parent = rootProject.ID == 0 ? "#" : rootProject.ID.ToString(),
text = projectChild.Name,
classes = string.Format("{0}{1}", "folder", level < 2 ? "" : " no-child"),
hasChildren = true,
state = new State { opened = false },
children = childNodes
});
}
}
return Json(nodes.ToArray(), JsonRequestBehavior.AllowGet);
}
The thing is that i see in console that node.id = source, but root value in controller is null.
also i see in network tab json from server:
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
I have a view and this is a part of it:
Also there is controller method:
The thing is that i see in console that node.id = source, but root value in controller is null.
also i see in network tab json from server:
In console i also see the error: Uncaught TypeError: Cannot read properties of undefined (reading 'id')
As a result tree is not loading. Can someone please advise to me here?
Beta Was this translation helpful? Give feedback.
All reactions