【问题标题】:jquery datatable context menu doesnt appearjquery数据表上下文菜单不出现
【发布时间】:2014-09-05 07:25:05
【问题描述】:

我使用datatables.net 中的以下内容在右键单击表格行时显示上下文菜单。添加此代码后,网页的默认功能已禁用,但未出现上下文菜单。怎么了?

"fnDrawCallback": function () {
            $('#example tr').on('mouseenter', function () {       
                $(this).contextMenu({
                    menu: 'productionRightClickMenu'
                },
            function (action, el, pos) {
                alert("hi");         
            });
            });
        }

在我对数据表使用服务器端处理后,我在网格上使用的常用上下文菜单不起作用。(使用客户端处理上下文菜单适用于以下代码。)

    //Function for context menu
(function ($, window) {

    $.fn.contextMenu = function (settings) {

        return this.each(function () {

            // Open context menu
            $(this).on("contextmenu", function (e) {
                //open menu
                $(settings.menuSelector)
                    .data("invokedOn", $(e.target))
                    .show()
                    .css({
                        position: "absolute",
                        left: getLeftLocation(e),
                        top: getTopLocation(e)
                    })
                    .off('click')
                    .on('click', function (e) {
                        $(this).hide();

                        var $invokedOn = $(this).data("invokedOn");
                        var $selectedMenu = $(e.target);

                        settings.menuSelected.call(this, $invokedOn, $selectedMenu);
                    });

                return false;
            });

            //make sure menu closes on any click
            $(document).click(function () {
                $(settings.menuSelector).hide();
            });
        });

        function getLeftLocation(e) {
            var mouseWidth = e.pageX;
            var pageWidth = $(window).width();
            var menuWidth = $(settings.menuSelector).width();

            // opening menu would pass the side of the page
            if (mouseWidth + menuWidth > pageWidth &&
                menuWidth < mouseWidth) {
                return mouseWidth - menuWidth;
            }
            return mouseWidth;
        }

        function getTopLocation(e) {
            var mouseHeight = e.pageY;
            var pageHeight = $(window).height();
            var menuHeight = $(settings.menuSelector).height();

            // opening menu would pass the bottom of the page
            if (mouseHeight + menuHeight > pageHeight &&
                menuHeight < mouseHeight) {
                return mouseHeight - menuHeight;
            }
            return mouseHeight;
        }

    };
})(jQuery, window);
    //for context menu

//$(document).on('keydown', '.inputs', function (e) {

$("#example td").contextMenu({
    menuSelector: "#contextMenu",
    menuSelected: function (invokedOn, selectedMenu) {
        var value = invokedOn.parent().children(':first').text();

        $.ajax({
            url: "../xyz/GetItemInfoDetails",
            type: 'POST',
            dataType: 'json',
            data: { "item_id": value },
            success: function (data) {
                if (data.ItemID != null) {

                    $("#value_itemId").html(data.ItemID);
                    $("#value_ItemDescription").html(data.ItemDescription);
                    $("#value_ItemGroup").html(data.ItemGroup);
                    $("#value_ItemCategory").html(data.ItemCategory);
                    $("#value_ItemUnitOfMesure").html(data.ItemUnitOfMesure);
                }
            }
        });
        $("#itemdetailsmodal").modal('show');

    }
});

【问题讨论】:

  • 只有一个 jQuery dataTables,但是有多个 jQuery ContextMenu's - 你用的是什么 contextMenu?

标签: jquery jquery-ui contextmenu jquery-datatables jquery-callback


【解决方案1】:

在实例化数据表时使用 fnRowCallback 选项。然后在它的函数内部创建你的上下文菜单。

"fnRowCallback": function (nRow){
     $(nRow).on('mousenter', function () {       
            $(this).contextMenu({
                menu: 'productionRightClickMenu'
            },
        function (action, el, pos) {
            alert("hi");         
        });
     });
 } 

类似这样的事情可能会与 nRow 一起使用,作为对表中选定行的回调

【讨论】:

  • 这里的“实例化”是什么意思....我试过鼠标输入;在上下文菜单上,在加载时没有按预期工作.. 我试试你的建议..
  • 将mouseenter的错字更正为mouseenter...它将起作用
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多