jqGrid - 在 jqGrid 中右键单击单元格确定列的名称

jqGrid - determine name of column on right click of a cell in jqGrid

所以我必须在打开右键单击 table 中的任何单元格打开的上下文菜单时获取列名称。我找到了这个答案 jqGrid - determine name of column on right click of a column in jqGrid,它仅适用于列 headers。

到目前为止我的代码

loadComplete: function () {
                    $('tr.jqgrow').contextMenu('myMenu2', {
                        bindings: {
                            CA: function(trigger) {
                                var cm = $('#' + MY_GRID ).jqGrid("getGridParam", "colModel");
                                //CODE SHOULD BE ADDED HERE -- to get the column name and pass to addToContext();
                                var rowData = jQuery('#' + MY_GRID ).jqGrid ('getRowData', trigger.id);
                                addToContext(rowData);
                            }
                        }
                    });
                },

也试过这段代码,但我得到了未定义的

var cellName = $(trigger).closest('td').attr('aria-described-by');

您可以像这样使用 aria-describedby 属性使用 onContextMenu 来确定列名称

    ....contextMenu('contextMenu', {
        bindings: {
            'edit': function (t,c) {
                console.log(colname)
                editRow();
            },
            'add': function (t) {
                addRow();
            },
            'del': function (t) {
                delRow();
            }
        },
        onContextMenu: function (event) {
            colname = $(event.target).attr('aria-describedby');
            colname = colname.substr(gridId.length + 1);
            return true;
        }
    });

其中colname是全局定义的,gridId是没有#

的网格id

P.S。如果单元格中的内容复杂,正确的是替换

$(event.target).attr('aria-describedby');

$(event.target).closest('td').attr('aria-describedby');