我在asp.net mvc3中使用带有contextmenu的jsTree创建了一个树形视图。
<div id="divtree">
<ul id="tree">
<li><a href="#" class="usr">@Model.Name</a>
@Html.Partial("Childrens", Model)
</li>
</ul><script type="text/javascript">
$(function () {
$("#divtree").jstree(
{
"plugins": ["themes", "html_data", "ui", "crrm", "contextmenu"]
});
});它运行良好。

我想在上下文菜单中创建一个自定义项目。例如,创建一个新菜单项。新建用于在上下文菜单中创建新员工,并在数据库中插入员工。我使用了一个jQuery POST函数来完成这个任务。而是如何处理上下文菜单项中的单击事件。
发布于 2013-01-03 15:28:46
下面是定制上下文菜单插件的方法:
$("#divtree").jstree({
"plugins": ["themes", "html_data", "ui", "crrm", "contextmenu"],
"contextmenu": {
"items": function ($node) {
return {
"Create": {
"label": "Create a new employee",
"action": function (obj) {
this.create(obj);
}
},
"Rename": {
"label": "Rename an employee",
"action": function (obj) {
this.rename(obj);
}
},
"Delete": {
"label": "Delete an employee",
"action": function (obj) {
this.remove(obj);
}
}
};
}
}
});好的,在这个例子中,我只调用了点击处理程序中的基函数:this.create(obj);,this.rename(obj);和this.remove(obj);,其中obj将是被点击的节点。
现在,例如,如果您想在添加新项时向服务器发送AJAX请求,则可以订阅create.jstree事件,如jsTree文档的demo page中所示:
<script type="text/javascript">
$("#divtree").jstree({
"plugins": ["themes", "html_data", "ui", "crrm", "contextmenu"],
"contextmenu": {
"items": function ($node) {
return {
"Create": {
"label": "Create a new employee",
"action": function (obj) {
this.create(obj);
}
},
"Rename": {
"label": "Rename an employee",
"action": function (obj) {
this.rename(obj);
}
},
"Delete": {
"label": "Delete an employee",
"action": function (obj) {
this.remove(obj);
}
}
};
}
}
})
.bind("create.jstree", function (e, data) {
$.ajax({
url: "@Url.Action("create", "employees")",
type: 'POST',
data: {
"name" : data.rslt.name
},
success: function (result) {
}
});
});
</script>检查传递给create.jstree事件回调的e和data参数。它们包含许多关于新创建的节点的有用信息,您可以使用这些信息与AJAX请求一起发送。
受此示例的启发,您可以继续使用文档中所示的remove.jstree和rename.jstree事件对其进行扩展。因此,当您查看它时,所有需要做的就是阅读文档。例如,我一生中从未使用过jsTree,但我花了5分钟才在文档中找到了示例,并为您快速完成了测试。因此,下次当你对正在使用的一些插件或框架有一个与编程相关的问题时,请先花更多的精力阅读文档。
https://stackoverflow.com/questions/14133984
复制相似问题