Home › Forums › Guriddo jqGrid JS › Enter Key propagates out of jqGrid when pressed inline edit
Tagged: inline edit enter key
From the jqGrid js 5.0.0 change log:
This change affected my code OUTSIDE of jqGrid, causing click events to fire on buttons that didn’t have anything to do with jqGrid. My fix was this, but jqGrid should really be fixed I think:
/* jqGrid 5.0.0 introduced a change that propagated the Enter key up the DOM outside the grid — code below cancels this behavior */
$(document).on(‘keydown’, ‘.ui-jqgrid’, function (e, ui) {
if (e.keyCode === 13) {
e.preventDefault();
}
});
I had to add the class .editable to narrow down cancelling the Enter Key only when editing an inline jqGrid entry.
[code]
$(document).on(‘keydown’, ‘.ui-jqgrid .editable’, function (e, ui) {
if (e.keyCode === 13) {
e.preventDefault();
}
});
[/code]
Hello,
Thanks for the investigation.
Can yo please provide a simple test case or send me a link to the problem?
Thank you.
Kind Regards,
<%@ Page Language=”C#” AutoEventWireup=”true” CodeFile=”InlineEditStandalone.aspx.cs” Inherits=”InlineEditStandalone” %>
<!DOCTYPE html>
<html xmlns=”http://www.w3.org/1999/xhtml”>
<head runat=”server”>
<meta http-equiv=”X-UA-Compatible” content=”IE=edge,chrome=1″ />
<title>jqGrid Inline Edit Test</title>
<!– cupertino, redmond, hot-sneaks, start, ui-darkness, mint-choc, smoothness –>
<link type=”text/css” rel=”stylesheet” href=”//ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/themes/hot-sneaks/jquery-ui.css” />
<link type=”text/css” rel=”stylesheet” href=”jqGrid/ui.jqgrid.5.0.0.css” />
<style type=”text/css”>
body {
font-family: Arial;
}
.ui-widget {
font-size: 0.8em;
}
.ui-jqrid {
font-family: 0.8em;
}
.ui-widget-overlay {
opacity: 0.4 !important;
background: inherit;
background-color: #aaa;
}
</style>
</head>
<body>
<form id=”form1″ runat=”server”>
<asp:ScriptManager ID=”scriptManager” runat=”server” EnablePageMethods=”true” EnablePartialRendering=”false”>
<Scripts>
<asp:ScriptReference Path=”//ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js” />
<asp:ScriptReference Path=”//ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js” />
<asp:ScriptReference Path=”~/jqGrid/i18n/grid.locale-en.5.0.0.js” />
<asp:ScriptReference Path=”~/jqGrid/jquery.jqGrid.5.0.0.min.js” />
</Scripts>
</asp:ScriptManager>
<table id=”dgRegions”></table>
<table id=”pgRegions”></table>
<button id=”btnShowData” style=”display: none;”>Show Data</button>
<script type=”text/javascript”>
var data = {};
var lastSel;
function cancelEditing(myGrid) {
if (lastSel != null) {
$(myGrid).jqGrid(‘restoreRow’, lastSel);
var lrid = $.jgrid.jqID(lastSel);
$(“tr#” + lrid + ” div.ui-inline-edit, ” + “tr#” + lrid + ” div.ui-inline-del”).show();
$(“tr#” + lrid + ” div.ui-inline-save, ” + “tr#” + lrid + ” div.ui-inline-cancel”).hide();
}
}
function LoadGrid(data) {
if ($(‘#dgRegions’)[0].grid) {
$(‘#dgRegions’).jqGrid(‘GridUnload’);
}
var dgRegions = $(‘#dgRegions’).jqGrid({
datatype: ‘local’,
data: data.rows,
cmTemplate: {
searchoptions: { sopt: [‘bw’, ‘cn’, ‘nc’, ‘eq’, ‘ne’, ‘lt’, ‘le’, ‘gt’, ‘ge’, ‘bn’, ‘in’, ‘ni’, ‘ew’, ‘en’] }
},
colModel: [
{ name: ‘RegionID’, hidden: true, key: true },
{
label: ‘Region’, name: ‘RegionDescription’, index: ‘RegionDescription’, editable: true, editoptions: { size: 40, maxlength: 50 }, editrules: {
custom: true,
custom_func: function (value, label) {
if (value.trim().length === 0)
return [false, $.validator.format(‘{0}: Field is required’, label)];
else if (value.substr(0, 1) === ‘ ‘)
return [false, $.validator.format(‘{0}: Field cannot begin with space’, label)];
else
return [true, ”];
}
}
},
{
label: ‘ ‘, formatter: ‘actions’, width: 60, fixed: true, search: false,
formatoptions: {
url: ‘clientArray’,
keys: true,
delbutton: false,
onEdit: function (rowid) {
if (lastSel != null && rowid !== lastSel) {
cancelEditing($(this));
}
lastSel = rowid;
},
afterSave: function (rowid) {
var row = $(this).getRowData(rowid);
$.each(data.rows, function (i, drow) {
if (drow.RegionID === row.RegionID) {
drow.RegionDescription = row.RegionDescription;
return false;
}
});
}
}
}
],
caption: ‘R e g i o n s’,
pager: ‘#pgRegions’,
sortname: ‘RegionDescription’,
sortorder: ‘asc’,
width: 500,
height: ‘100%’,
pginput: true,
pgbuttons: true,
rowNum: 15,
rowList: [10, 15, 20, 25],
viewrecords: true,
shrinkToFit: true,
onSelectRow: function (rowid) {
if (lastSel != null && rowid !== lastSel) {
cancelEditing(this);
}
lastSel = rowid;
}
});
dgRegions
.jqGrid(‘filterToolbar’, {
searchOperators: true,
stringResult: true
})
.jqGrid(‘navGrid’, ‘#pgRegions’, { search: false, refresh: false, edit: false },
{ width: 400 },
{ width: 400 },
{ width: 400 }
);
}
$().ready(function () {
/* jqGrid 5.0.0 introduced a change that propagated the Enter key up the DOM outside the grid — code below cancels this behavior */
//$(document).on(‘keydown’, ‘.ui-jqgrid .editable’, function (e, ui) {
// if (e.which === $.ui.keyCode.ENTER) {
// e.preventDefault();
// }
//});
$(‘#btnShowData’).button().click(function (e, ui) {
alert(JSON.stringify(data));
e.preventDefault();
});
data.rows = [
{ RegionID: 1, RegionDescription: ‘Eastern’ },
{ RegionID: 2, RegionDescription: ‘Northern’ },
{ RegionID: 3, RegionDescription: ‘Southern’ },
{ RegionID: 5, RegionDescription: ‘Testing’ },
{ RegionID: 4, RegionDescription: ‘Western’ }
];
LoadGrid(data);
$(‘#btnShowData’).show();
});
</script>
</form>
</body>
</html>
Hello,
First of all thank you for the code.
Sorry, but I do not understand (or I can not reproduce the problem)
In which browser is this?
Here is your code
I have tested it with Chrome and FireFox latest and IE11, but does not see any problem.
Could you please describe the step for the problem?
Thank you.
Kind Regards,
Hi Tony,
I agree that the page you gave me works okay — why yours works and mine does not I do not know. Here is a link to a totally standalone HTML file that on Windows 10, 8.1 and 7 — any browers — does not work for me.
By the way, after you change the Region description press the Enter key. I get the JSON display of the data object — not the saving of the edited row.
Hello,
You point me to find the problem quick.
The only difference in both codes is the form tag.
You have a form in which yo include the script and jqGrid html contents.
Short: the reason is the form tag – when a enter key is pressed first thing here is the submition of the form (the button element).
To avoid either move the grid content from form tag or
|
1 2 3 4 5 |
$('form').keypress(function(e){ if(e.which === 13){ return false; } }); |
Some discussion here
Kind Regards,
Tony,
The whole point of reporting this issue wasn’t that I could not find a fix, but that the behavior of jqGrid v5.0.0 changed from v4.8.2.
If you return to thecybersafe.net you will see that I have two identical versions of this page — one referencing v5.0.0 and the other v4.8.2, with a button on each page to flip back and forth.
The v5.0.0 page clicks the ‘Show Data’ button when you hit enter on the inline edit selected line after you change the description and the v4.8.2 does not.
Applying the fix you mentioned could be a problem since I have hundreds of pages that may or may not have form submit requirements.
Thanks,
Marv
Ok. I will made some changes, so that everybody can enjoy the new things, but save the existing behavior.
I will let you know.
Kind Regards,
I have changed the code, so that the default behavior is used.
You can download the build from GitHub.
Please let me know if this fixes your problem.
Kind Regards,
Hi Tony,
The problem has been fixed. Thank you very much for the quick response to resolving this issue.
Regards,
Marv
Hello Marv,
Thank you for remind us that we should in most cases save the existing behavior .
Kind Regards,
Copyright 2014 TriRand LtdAll Rights ReservedRSS
Back to Top