gridComplete 函数中的 jqGrid 绑定事件

发布于 2025-01-05 00:13:21 字数 906 浏览 0 评论 0原文

我在 ASP.NET MVC 项目中使用 jqgrid。我正在尝试在 gridComplete 函数中绑定单击事件。然后我想检测单击的单元格是否可编辑。到目前为止,我已经得到了这一点:

    gridComplete: function () {
                var ids = jQuery("#resources").jqGrid('getDataIDs');
                for (var i = 0; i < ids.length; i++) {
                    var cl = ids[i];
                    jQuery('#resources').jqGrid('editRow', cl);
                }
                $("#resources td").click(function(e) {
                    ...//here I want to check if clicked cell is editable
                });

     }

如您所见,我正在加载网格后立即准备好行进行编辑。这仅指属性 editable=true 的列。

更新:

解决方案非常简单:

    $("#resources td").click(function(e) {
                    var sClassName = e.target.className;
                    if (sClassName == "editable") {
                        //editable cell is clicked
                    }
     });

I'm using jqgrid in my ASP.NET MVC project. I'm trying to bind click event in gridComplete function. Then I want to detect if cell, that was clicked is editable or not. So far I've got this:

    gridComplete: function () {
                var ids = jQuery("#resources").jqGrid('getDataIDs');
                for (var i = 0; i < ids.length; i++) {
                    var cl = ids[i];
                    jQuery('#resources').jqGrid('editRow', cl);
                }
                $("#resources td").click(function(e) {
                    ...//here I want to check if clicked cell is editable
                });

     }

as you can see I'm making rows ready for edit right after loading a grid. this refers only to columns with attribute editable=true.

UPDATE:

The solution was very simple:

    $("#resources td").click(function(e) {
                    var sClassName = e.target.className;
                    if (sClassName == "editable") {
                        //editable cell is clicked
                    }
     });

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

仅此而已 2025-01-12 00:13:21

我不完全理解你的要求。单击“可编辑”或“不可编辑”单元格时您希望执行哪些其他操作?

不过,我建议您使用 onCellSelect 回调处理程序而不是绑定网格中每个单元格的 click 事件。为了理解每个绑定需要一些网络浏览器资源。您可以只绑定一个单元格的父元素,例如 元素(网格本身),而不是将事件绑定到单元格。由于称为事件流的机制,< em>事件传播或事件冒泡未处理的事件将尝试由父元素或父元素的父元素的相同事件处理程序处理,依此类推。事件处理程序接收 事件对象,其中 target 属性有助于检测最初单击的是哪个元素。

代码

onCellSelect: function (rowid, iCol, cellcontent, e) {
    var colModel = $(this).jqGrid('getGridParam', 'colModel');
    if ($.isArray(colModel) && !colModel[iCol].editable) {
        // non-editable cell is clicked 
    }
}

因此,您可以使用类似或这样的

onCellSelect: function (rowid, iCol, cellcontent, e) {
    if (!this.p.colModel[iCol].editable) {
        // non-editable cell is clicked 
    }
}

来检测当前单击的单元格是否不可编辑。

I don't full understand your requirements. What additional actions you want to do on click of the "editable" or "non-editable" cell?

Nevertheless I would recommend you to use onCellSelect callback handler instead of binding click event to every cell of the grid. For understanding every binding required some resources of the web browser. Instead of binding events to cells you can do only one bind to any parent element of cell like the <table> element (the grid itself). Because of the mechanism known as event flow, event propagation or event bubbling the unhandled events will be tried be handled by the same event handler of the parent element or the parent of the parent and so on. The event handler receive Event object which target property helps to detect which element was originally clicked.

So you can use the code like

onCellSelect: function (rowid, iCol, cellcontent, e) {
    var colModel = $(this).jqGrid('getGridParam', 'colModel');
    if ($.isArray(colModel) && !colModel[iCol].editable) {
        // non-editable cell is clicked 
    }
}

or this

onCellSelect: function (rowid, iCol, cellcontent, e) {
    if (!this.p.colModel[iCol].editable) {
        // non-editable cell is clicked 
    }
}

to detect whether the currently clicked cell was non-editable.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文