【问题标题】:Create custom item in jsTree Context menu在 jsTree 上下文菜单中创建自定义项
【发布时间】:2013-01-03 06:19:46
【问题描述】:

我在 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"]
        });
});

一切正常。

我想在上下文菜单中创建一个自定义项。例如创建一个新的菜单项。 New 用于在上下文菜单中创建新员工,并将员工插入 DB。我为这个任务使用了一个 jQuery POST 函数。但是如何处理上下文菜单项中的点击事件。

【问题讨论】:

  • 是的,这是可能的。 What have you tried 你有什么问题?
  • 对不起,我解释一下我的问题...

标签: asp.net-mvc treeview jstree


【解决方案1】:

以下是自定义上下文菜单插件的方法:

$("#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 事件回调的edata 参数。它们包含许多有关新创建节点的有用信息,您可以使用这些信息与 AJAX 请求一起发送。

受此示例的启发,您可以继续使用 remove.jstreerename.jstree 事件对其进行扩展,如文档中所示。因此,当您查看它时,只需阅读文档即可。例如,我一生中从未使用过 jsTree,但我只用了 5 分钟就在文档中找到了示例并为您做了一个快速的秒杀。因此,下次您对正在使用的某些插件或框架有编程相关问题时,请先花更多精力阅读文档。

【讨论】:

  • 非常感谢先生,我是第一次使用 Treeview,当然,我必须仔细阅读所有文档,再次表示感谢您的宝贵努力。
  • 我解决了这个问题。我使用“data.rslt.name”而不是“data.rslt.new_name”。
  • 当我尝试过这段代码时,萤火虫捕获错误/this.create 不是函数/。使用 jsTree v. 3.1.1。怎么了?
  • @loviji 检查this 包含的内容。我有同样的问题,它是指向窗口对象[object Window] 的指针。将其替换为对 jstree 本身的引用,它应该可以工作。
  • 这里是 3.2.1 stackoverflow.com/questions/21096141/…的版本@
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-11-12
  • 2023-03-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多