I am new to jqgrid. I have the following code to insert a new row when a tab key is pressed. It works for the most part of it. But when a new row is inserted, the focus is given to the second Column in that row instead of First column.
How do I solve this problem??
HTML:
<table id="list11"></table>
<!--<div id="paddtree"></div>-->
<label>press tab to add new row</label>
<script>
var selIRow = 1;
var lastsel2
jQuery("#list11").jqGrid({
// url:'d/${jobId}.htm',
datatype: "json",
colNames: ['First Name', 'Email ID', 'Phone Number'],
colModel: [
// {name:'id',index:'id', width:75, search:true, stype:'text' , search:true, sortable:true},
{name: 'firstName', width: 180, search: true, stype: 'text', sortable: true, editable: true, },
{name: 'email', index: 'email', width: 250, editable: true},
{name: 'phoneNumber', index: 'phoneNumber', width: 100, align: "right", editable: true,
editoptions: {
dataInit: function(elem) {
$(elem).focus(function() {
this.select();
})
},
dataEvents: [
{
type: 'keydown',
fn: function(e) {
var key = e.charCode || e.keyCode;
if (key == 9 || key == 15)//tab
{
var grid = $('#list11');
//Save editing for current row
grid.jqGrid('saveRow', selIRow, false, 'clientArray');
//If at bottom of grid, create new row
// alert(grid.getDataIDs().length);
if (selIRow++ == grid.getDataIDs().length) {
grid.addRowData(selIRow, {});
}
//Enter edit row for next row in grid
grid.jqGrid('editRow', selIRow, true, 'clientArray');
}
}
}
]
}, },
],
beforeRequest: function(data) {
var grid = $('#list11');
grid.addRowData(grid.jqGrid('getGridParam', 'records') + 1, {});
grid.jqGrid('editRow', selIRow, false, 'clientArray');
},
onSelectRow: function(id) {
if (id && id !== lastsel2) {
jQuery('#list11').jqGrid('restoreRow', lastsel2);
jQuery('#list11').jqGrid('editRow', id, true);
lastsel2 = id;
}
},
// editurl: "data/add.htm",
sortname: 'id',
viewrecords: true,
sortorder: "desc",
caption: "Candidates applied for <bold> ${job.getTitle()}</bold>",
// pager : "#paddtree",
emptyrecords: "new",
});
</script>
Thanks.
All you have to do to your code is add the line e.preventDefault();
here:
if (key == 9 || key == 15)//tab
{
e.preventDefault();
...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With