jquery context menu title from dom

时间:2016-07-11 19:09:28

标签: jquery contextmenu

I am getting information from the dom to populate my context menu

What is happening is it's returning the very first clicked menu's title and not the next clicked menu's title:

I want it to show the correct menu title.

    $('.context_menu_users').on
    (
        'click', 
        function(e) 
        {
            var controller = this.getAttribute("data-controller");
            var delete_visible = false;
            var activate_visible = false;

            switch (controller)
            {
                case "users":
                    delete_visible = false;
                    activate_visible = true;
                    break;
                default:
                    delete_visible = this.getAttribute("data-delete");
                    activate_visible = false;
            }

            $.contextMenu
            (
                {
                    selector: '.context_menu_users', 
                    trigger: 'left',
                    className: controller+'-title',
                    callback: function(key, options) 
                    {
                        clicked = this[0];
                        id = clicked.getAttribute("data-id");
                        name = clicked.getAttribute("data-name");
                        controller = clicked.getAttribute("data-controller");
                        action = clicked.getAttribute("data-action");
                        can_delete = clicked.getAttribute("data-delete");
                        context_ment_items = null;
                        switch (key)
                        {
                            case "add_user":
                            case "add_department":
                            case "add_group":
                                key = "add";
                                id = "";
                            case "view":
                            case "edit":
                            case "delete":
                            case "activate":
                                usersAction(controller, key, id, name)
                        }
                    },
                    items:
                    {
                        "view": {name: "View", icon: "view"},
                        "edit": {name: "Edit", icon: "edit"},
                        "delete": {name: "Delete", icon: "delete", visible: delete_visible},
                        "activate": {name: "Change Activation", icon: "activate", visible: activate_visible},
                        "sep1": "---------",
                        "add_user": {name: "Add New User", icon: "add-user"},
                        "add_department": {name: "Add New Department", icon: "add-department"},
                        "add_group": {name: "Add New Group", icon: "add-group"},
                    }
                }
            );

            // set a title for menu
            $('.' + controller + '-title').attr
            (
                'data-menutitle',
                controller.toLowerCase().replace
                (
                    /\b[a-z]/g,
                    function(letter)
                    {
                        return letter.toUpperCase();
                    }
                )
                + " Menu"
            );

        }
    );

https://jsfiddle.net/0qLzk7sb/

1 个答案:

答案 0 :(得分:0)

I figured it out if you go to the jsfiddle above it has the solution