jqGrid自定义onCellSelect在SaveCell之前不触发 [英] jqGrid custom onCellSelect not firing beforeSaveCell

查看:150
本文介绍了jqGrid自定义onCellSelect在SaveCell之前不触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 onCellSelect 内编写一些代码,该代码可以很好地执行

I am writing some piece of code inside onCellSelect, which executes fine

onCellSelect: function (rowid, iCol, cellcontent) {
        if (iCol > 0) {
            $("#gridMain_d").jqGrid("resetSelection");
            $("#gridMain_d").setSelection(rowid, true);
        }
    }

但是问题是因为此代码 beforeSaveCell 事件未触发.我知道这一点是因为在SaveCell开始工作之前,我删除了此代码.我已经尝试过使用return语句,但是没有用.

But the problem is because of this code beforeSaveCell event is not firing. I know this because as soon as I remove this code beforeSaveCell starts working. i have tried using return statement but nothing works.

更新
我评论了上面编写的代码并添加了此代码

UPDATE
I commented the code written above and added this code

beforeSelectRow: function (rowid, e) {
        var $self = $(this), iCol, cm,
            $td = $(e.target).closest("tr.jqgrow>td"),
            $tr = $td.closest("tr.jqgrow"),
            p = $self.jqGrid("getGridParam");

        if ($(e.target).is("input[type=checkbox]") && $td.length > 0) {
            $self.jqGrid("setSelection", $tr.attr("id"), true, e);
        }
        else {
            $self.jqGrid('resetSelection');
            $self.jqGrid("setSelection", $tr.attr("id"), true, e);
        }
        return true;
    },

但仍然 beforeSaveCell 事件未触发.

更新2
此jsFiddle复制了问题. http://jsfiddle.net/eranjali08/CzVVK/1175/

UPDATE 2
This jsFiddle replicates the issue. http://jsfiddle.net/eranjali08/CzVVK/1175/

推荐答案

有许多相互依赖的回调.而且,在不同版本的jqGrid中,此类依赖关系可能有所不同.我建议您使用beforeSelectRow而不是onCellSelect,因为它将是第一个回调,它将在单击jqGrid单元格时被调用.您可能需要的所有信息都可以从beforeSelectRow的第二个参数(以下代码中的e)获得:

There are many callbacks which depends from each other. Moreover it could be difference of such dependencies in different versions of jqGrid. I recommend you to use beforeSelectRow instead of onCellSelect because it will be the first callback which will be called on click on the cell of jqGrid. All information which you could need you can get from the second parameter (e in the code below) of beforeSelectRow:

beforeSelectRow: function (rowid, e) {
    var $self = $(this),
        $td = $(e.target).closest("tr.jqgrow>td");
        iCol = $.jgrid.getCellIndex($(e.target).closest($td[0]),
        colModel = $self.jqGrid("getGridParam", "colModel"),
        columnName = colModel[i].name;
    //...
    // one can use here $td.html(), $td.text() to access the content of the cell
    // columnName is the name of the column which cell was clicked
    // iCol - the index of the column which cell was clicked
    return true; // or false to suppress the selection
}

您只需不要忘记beforeSelectRow应该返回告知jqGrid是否选择单击的行的值.从beforeSelectRow返回的值false"stop"禁止选择单击的行.所有其他值都允许选择.

You need just don't forget that beforeSelectRow should return the value which inform jqGrid whether to select the clicked row or not. The values false or "stop" returned from beforeSelectRow suppresses the selection of the clicked row. All other values allows the selection.

已更新:我又分析了您的代码一次,希望找到问题的原因.使用resetSelection在使用单元格编辑的情况下是有害的.查看 resetSelection.

UPDATED: I analysed your code one more time and I hope I've found the reason of your problem. You use resetSelection which is evil in case of usage of cell editing. Look at the last line of resetSelection.

t.p.savedRow = [];

销毁包含当前编辑单元格信息的数组.因此无法再保存或还原该单元格.

It destroys the array holding the information about the currently editing cell. So the cell can't be saved or restored more.

要解决此问题,您必须从代码中删除resetSelection.如果确实需要使用resetSelection,则应将其替换为例如调用setSelection的循环.相应的代码可能与以下代码相似:

To solve the problem you have to remove resetSelection from your code. If you really need to use resetSelection you should replace it for example to the loop with call of setSelection. The corresponding code could look close to the code below:

beforeSelectRow: function (rowid, e) {
    var $self = $(this), iCol, cm, i, idsOfSelectedRows,
        $td = $(e.target).closest("tr.jqgrow>td"),
        $tr = $td.closest("tr.jqgrow"),
        p = $self.jqGrid("getGridParam");

    if ($(e.target).is("input[type=checkbox]") && $td.length > 0) {
        $self.jqGrid("setSelection", $tr.attr("id"), true, e);
    }
    else {
        //$self.jqGrid('resetSelection');
        idsOfSelectedRows = p.selarrrow.slice(0); // make copy of the array
        for (i = 0; i < idsOfSelectedRows.length; i++) {
            $self.jqGrid("setSelection", idsOfSelectedRows[i], false, e);
        }
        $self.jqGrid("setSelection", rowid, false, e);
    }
    return false;
},

这篇关于jqGrid自定义onCellSelect在SaveCell之前不触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆