Create custom item in jsTree Context menu

拥有回忆 提交于 2019-12-02 19:32:51

Here's how you could customize the contextmenu plugin:

$("#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);
                    }
                }
            };
        }
    }
});

Alright, in this example I am only calling the base function inside the click handlers: this.create(obj);, this.rename(obj); and this.remove(obj); where obj will be the node that was clicked.

So now for example if you want to send an AJAX request to the server when a new item is added you could subscribe to the create.jstree event as shown in the demo page of the jsTree documentation:

<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>

Inspect the e and data arguments that are passed to the create.jstree event callback. They contain lots of useful information about the newly created node that you could use to send along with the AJAX request.

Inspired by this example you could continue extending it with the remove.jstree and rename.jstree events as shown in the documentation. So when you look at it, all that was needed was to read the documentation. For example I've never used jsTree in my life but 5 minutes was all that it took me to find the example in the documentation and do a quick spike for you. So next time you have a programming related question about some plugin or framework that you are using please put more efforts into reading the documentation first.

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