jqGrid:当我单击网格外部或其他任何地方时如何失去焦点

发布于 2024-10-24 12:09:48 字数 108 浏览 1 评论 0原文

我目前正在使用内联编辑进行编辑,当我在网格外部单击时,它仍在编辑中。我应该使用什么事件处理程序来使其调用恢复行函数,这样数据实际发送到服务器的唯一方法是用户按 Enter 键。

提前谢谢

i'm currently doing editing using inline editing, and when i click outside the grid, it's still being under edit. what event handlers should i use to make it call the restore row function, such that the only way for data to actually sent to the server is if the user presses enter.

thx in advance

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

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

发布评论

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

评论(6

月寒剑心 2024-10-31 12:09:48

我不知道你到底是如何触发内联版本的。我使用 jqGrid 的 ondblClickRow 事件,并且还在寻找一种在用户离开输入或选择时恢复行的方法( 编辑)元素。

我发现跟踪最后选定的元素并在每次单击其他元素时检查它很麻烦。因此,我认为更方便的方法是将 restoreRow 触发器附加到 inputselectblur 事件> 当前正在编辑的元素,如下所示:

ondblClickRow: function(rowid, iRow, iCol, e) {
  grid.jqGrid('editRow', rowid, true);
  $("input, select", e.target).focus().blur(function() {
    grid.jqGrid('restoreRow', rowid);
  });
  return;
}

这样,只要用户离开编辑字段而不按 Enter 键,就会恢复该行。

这种方法对我来说非常有效,希望对其他人也有帮助。

I don't know exactly how you are triggering the inline edition. I use the ondblClickRow event of the jqGrid, and was also looking for a way to restore the row when the user left the input or select (edit) element.

I find it cumbersome to keep track of the last selected element, and checking for it on every click on other elements. So, I think a more convenient way is to attach the restoreRow trigger to the blur event of the input or select element currently being edited, as so:

ondblClickRow: function(rowid, iRow, iCol, e) {
  grid.jqGrid('editRow', rowid, true);
  $("input, select", e.target).focus().blur(function() {
    grid.jqGrid('restoreRow', rowid);
  });
  return;
}

This way, the row is restored whenever the user leaves the edition field without pressing enter.

This approach works great for me, hope it helps someone else too.

紧拥背影 2024-10-31 12:09:48

由于主要问题是当您在网格外部单击时您希望失去焦点,因此我编写了此函数来在网格没有 has() 单击的元素时取消选择单元格:

$(document).click(function(e) {
    e.preventDefault();
    //gets the element where the click event happened
    var clickedElement = e.target;      
    //if the click event happened outside the grid 
    if($("#myGridId").has(clickedElement).size() < 1){
        //unselect the grid row
        $("#myGridId").jqGrid("editCell", 0, 0, false);
    }
});

Since the main problem is that you want to lose the focus when you click outside the grid, I wrote this function to unselect the cell just when the grid don't has() the clicked element:

$(document).click(function(e) {
    e.preventDefault();
    //gets the element where the click event happened
    var clickedElement = e.target;      
    //if the click event happened outside the grid 
    if($("#myGridId").has(clickedElement).size() < 1){
        //unselect the grid row
        $("#myGridId").jqGrid("editCell", 0, 0, false);
    }
});
新雨望断虹 2024-10-31 12:09:48

无论如何,我已经想出了如何去做。只是想把它留在网上的某个地方可能会很好,因为我浪费了很多时间来弄清楚如何去做。希望它有帮助=)

$(document).click(function(e){
    if(e.target.id != lastSelectRoot+"_FieldName"){
        jQuery('#grid').jqGrid('restoreRow',lastSelectRoot);
        lastSelectRoot = null;
    }
});

只需在某处添加这段代码并将适当的部分(例如 FieldName 和 lastSelectRoot 和 #grid)更改为您已经使用的内容即可。

Anyway, i've figured out how to do it already. Just thought might be good to leave it somewhere online as I wasted quite a bit of time figuring out how to do it. Hope it helps =)

$(document).click(function(e){
    if(e.target.id != lastSelectRoot+"_FieldName"){
        jQuery('#grid').jqGrid('restoreRow',lastSelectRoot);
        lastSelectRoot = null;
    }
});

Just add this piece of code somewhere and change the appropriate parts, such as FieldName and lastSelectRoot and #grid to what you are already using.

过期以后 2024-10-31 12:09:48

该解决方案对我有用,并且看起来比其他选项更简单。是通用的,不需要任何全局变量。

$(document).on('focusout', '[role="gridcell"] *', function() {
    $("#mygrid").jqGrid('editCell', 0, 0, false);
});

基于 $(document).on('click') 的解决方案有一个潜在的缺陷。某些组件(例如 select2)不会将单击事件传播到文档,因此如果您的页面上有它并且被单击(这就是我的情况),它将失败。

This solution is working for me and looks simpler than the other options. Is generic and doesn't need any global variable.

$(document).on('focusout', '[role="gridcell"] *', function() {
    $("#mygrid").jqGrid('editCell', 0, 0, false);
});

Solutions based on $(document).on('click') have a a potential flaw. Some components like select2 don't propagate the click event to the document, so it will fail if you have it on your page and it's clicked (that was my case).

旧时模样 2024-10-31 12:09:48

即使我在使用内联编辑时也遇到了同样的问题。我已经找到了解决方法。我仍然不知道这是否是一个正确的解决方案。

当我编辑一行时,

var lastSel;

// In grid I am using some thing like this for editing
    ondblClickRow: function(rowId, iRow, iCol, e){
//initially I am saving my prev row selected for editing and then editing the selected row.

        if(rowId && rowId!==lastSel){ //lastSel is a global variable
            jQuery(this).saveRow(lastSel); 
            lastSel=rowId; 
         }        
        $(this).jqGrid('resetSelection');   
        $(this).jqGrid('editRow', rowId, true, null, null, 'clientArray');


    }

当我想将数据发送到服务器进行更新时,我使用了这种类型的东西,我在第一行中使用以下语句,然后发送数据到服务器。

$(gridId).saveRow(lastSel);//where lastSel is the global variable I have selected.

希望这能让您了解如何解决您的问题。
顺便说一句,我只制作了一行可以随时编辑。

Even I faced the same issue while working with inline editing.I have gone for a workaround.I still dont know yet whether it is a right a solution.

When I was editing a row I used some thing of this sort

var lastSel;

// In grid I am using some thing like this for editing
    ondblClickRow: function(rowId, iRow, iCol, e){
//initially I am saving my prev row selected for editing and then editing the selected row.

        if(rowId && rowId!==lastSel){ //lastSel is a global variable
            jQuery(this).saveRow(lastSel); 
            lastSel=rowId; 
         }        
        $(this).jqGrid('resetSelection');   
        $(this).jqGrid('editRow', rowId, true, null, null, 'clientArray');


    }

when I wanted to send the data to server to update I am using the following the statement in my first line and sending then sending the data to the server.

$(gridId).saveRow(lastSel);//where lastSel is the global variable I have selected.

Hope this gives you an idea how to do solve your problem.
BTW I have made only one row to be editable at any point of time.

春花秋月 2024-10-31 12:09:48

我尝试了几种不同的变体。基于 Mauricio Reis 的 代码我自己写的。

var lastSel = -1;

$("#mygrid").jqGrid({
    ...
    beforeSelectRow: function(rowid) {
        if (rowid !== lastSel) {
            lastSel = rowid;
            $("#mygrid").jqGrid('restoreRow',lastSel); // cancel edit
        }
        return true;
    },
    onCellSelect: function(rowId,iCol,cellcontent,e) {
        if(iCol == 1 || iCol == 2) // editable columns
            sgrid.jqGrid('editRow', rowId, true);
    },
    ...
});
...
$(document).click(function(e) {
    if(sgrid.has(e.target).size() < 1)
        $("#mygrid").jqGrid('restoreRow',lastSel); // cancel edit
});

如果您想保存行而不是取消编辑,只需放置

$("#mygrid").jqGrid('saveRow', lastSel); // save row

即可

$("#mygrid").jqGrid('restoreRow',lastSel); // cancel edit

Im tryed few different variants. Based on Mauricio Reis's code I wrote my own.

var lastSel = -1;

$("#mygrid").jqGrid({
    ...
    beforeSelectRow: function(rowid) {
        if (rowid !== lastSel) {
            lastSel = rowid;
            $("#mygrid").jqGrid('restoreRow',lastSel); // cancel edit
        }
        return true;
    },
    onCellSelect: function(rowId,iCol,cellcontent,e) {
        if(iCol == 1 || iCol == 2) // editable columns
            sgrid.jqGrid('editRow', rowId, true);
    },
    ...
});
...
$(document).click(function(e) {
    if(sgrid.has(e.target).size() < 1)
        $("#mygrid").jqGrid('restoreRow',lastSel); // cancel edit
});

If you wants to save row instead cancel editing just put

$("#mygrid").jqGrid('saveRow', lastSel); // save row

instead

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