jqgrid edit and add rows with "tab" key - jqgrid

I'm new to jqgrid , and I'm trying to navigate through the grid with "tab" key.
I want to be able to edit a row, and when I'm editing the last cell in that row, if I click the tab key it will save the current row changes (in the client side , and not by clicking enter) and set the focus to the next row and edit it's cells , and when I get to the last row and cell, the tab click will add a new row and make it in edit mode.
I tried with in line editing and then with cell editing but always got stuck...
how can this be done?
Thanks in advance.

I do not know what you currently have, but I tested this and it works. Since you failed to mention how you would originally begin editing the grid, I did it manually in the ready event, you just have to keep track of what row is currently being edited using the selIRow var.
var selIRow = 1; //keep track of currently edited row
//initialized to 1 for testing purposes
$(document).ready(function () {
$("#jqGrid").jqGrid({
datatype: 'local',
colNames: ['Inv No', 'Date', 'Client', 'Amount', 'Tax', 'Total', 'Notes'],
colModel: [
{ name: 'id', index: 'id', width: 60, editable: true },
{ name: 'invdate', index: 'invdate', width: 90, editable: true },
{ name: 'name', index: 'name', width: 100, editable: true },
{ name: 'amount', index: 'amount', width: 80, editable: true },
{ name: 'tax', index: 'tax', width: 80, editable: true },
{ name: 'total', index: 'total', width: 80, editable: true },
{ name: 'note', index: 'note', width: 150, editable: true,
//Place this code in the col options of the last column in your grid
// it listens for the tab button being pressed
editoptions: {
dataInit: function (elem) { $(elem).focus(function () { this.select(); }) },
dataEvents: [
{
type: 'keydown',
fn: function (e) {
var key = e.charCode || e.keyCode;
if (key == 9)//tab
{
var grid = $('#jqGrid');
//Save editing for current row
grid.jqGrid('saveRow', selIRow, false, 'clientArray');
//If at bottom of grid, create new row
if (selIRow++ == grid.getDataIDs().length) {
grid.addRowData(selIRow, {});
}
//Enter edit row for next row in grid
grid.jqGrid('editRow', selIRow, false, 'clientArray');
}
}
}
]
}
}
],
});
});
Some credit goes to kajo's answer from here for the tab event.

Related

How to Change Row Location in jqgrid?

I am using jqgrid with MVC and for now, I want change Row location when click on ^ to get one line up and inverse.
actually I want to set 'up' and 'down' signs to every row for change one level row locations
any body can help me?!
There are a lot of possible solution. One of them is to use custom formtter to define the buttons and then bind event on loadComplete to move the rows using the jquery. Below the code:
$("#jqGrid").jqGrid({
datatype: "local",
data: mydata,
height: 250,
width: 780,
colModel: [
{ label :'move', formatter : myformatter, width:95},
{ label: 'Inv No', name: 'id', width: 75, key:true },
{ label: 'Date', name: 'invdate', width: 90 },
{ label: 'Client', name: 'name', width: 100 },
{ label: 'Amount', name: 'amount', width: 80 },
{ label: 'Tax', name: 'tax', width: 80 },
{ label: 'Total', name: 'total', width: 80 },
{ label: 'Notes', name: 'note', width: 150 }
],
viewrecords: true, // show the current page, data rang and total records on the toolbar
caption: "Load jqGrid through Javascript Array",
loadComplete : function() {
$(".up,.down").on('click', function () {
var row = $(this).closest("tr.jqgrow");
if($(this).is('.up')){
row.insertBefore(row.prev());
} else {
row.insertAfter(row.next());
}
});
}
});
function myformatter() {
return '<button class="up" >Up</button>' + '<button class="down">Down</button>';
}
Demo here

How to make JqGrid always have empty row at bottom?

I'm using JqGrid with inline editing and i dont use navigation, pager or footer. I want to add empty row at the bottom to added. I mean empty row will be always there after editing an exsiting row, after adding new row.
I added row in loadComplete event, but i just adds empty row at beginning not anymore.
jQuery("#tableContents").jqGrid({
postData: { orderId: '139358' },
mtype: "POST",
url: "test.asmx/GetContents",
datatype: "json",
ajaxGridOptions: { contentType: 'application/json; charset=utf-8' },
serializeGridData: function (postData) {
return JSON.stringify(postData);
},
jsonReader: { repeatitems: true, root: "d.rows", page: "d.page", total: "d.total", records: "d.records" },
colNames: ['Master Content ID', 'Description Of Goods', 'No Of Items', 'Total Value for Customs', 'Weight', 'Track No'],
colModel: [
{
name: 'master_content_id', index: 'master_content_id', width: 60, hidden: true, editable: true, editrules: { edithidden: false }
},
{
name: 'content', index: 'content', width: 60, sorttype: "string", editable: true, edittype: "text",
editrules: {
required: true
}
},
{
name: 'piecesInt', index: 'piecesInt', width: 90, align: "right", sorttype: "int", editable: true,
editrules: {
number: true,
required: true
}
},
{
name: 'value', index: 'value', width: 100, align: "right", sorttype: "float", editable: true,
editrules: {
required: true
}
},
{
name: 'weight', index: 'weight', width: 80, align: "right", sorttype: "float", editable: true,
editrules: {
required: true
}
},
{
name: 'track_no', index: 'track_no', width: 80, align: "right", sorttype: "string", editable: true
},
],
rowNum: 10,
rowList: [10, 20, 30],
//pager: jQuery('#pager1'),
sortorder: "desc",
viewrecords: true,
regional: 'tr',
height : '100%',
caption: "Manipulating Array Data",
editurl: 'test.asmx/EditContent',
loadonce: true,
sortable: true,
//footerrow: true,
onSelectRow: function (rowid) {
var $self = $(this),
// savedRows array is not empty if some row is in inline editing mode
savedRows = $self.jqGrid("getGridParam", "savedRow");
if (savedRows.length > 0) {
$self.jqGrid("restoreRow", savedRows[0].id);
}
$self.jqGrid("editRow", rowid, {
keys: true,
extraparam: { orderId: '139358', staffId : '111' },
aftersavefunc: function (rowid) {
alert("Saved");
}
});
},
loadComplete: function () {
var $self = $(this)
$self.jqGrid('addRowData', undefined, {});
}
});
How can we do that?
You try to use jqGrid in the way which is opposite to the standard behavior. It's very bad idea. You will have to write a lot of code and have minimal difference to the standard way.
The main problem is: jqGrid hold data inside of the grid. You try to add an empty row inside of the data which is not loaded from the server, which is not in editing mode (like addRow do for example) and which is not saved on the server. So you try to use the grid in the way contradicting to jqGrid logic. It sounds simple, but it is really bad and the implementation will be complex.
I would recommend you to follow the standard way. You will have clean and simple code and the most of the users can use the grid intuitive. For example you can do add navigator toolbar where you add "+"/"Add" button with respect of inlineNav. The user will intuitively understand what need be done to add new row. The difference whether the user clicks on empty row at the bottom of the grid or if the user clicks on "Add new row" button, existing also at the bottom of the grid, is very small. After the click on the button the new row will be added and the user can insert the data. By press of Enter the row will be saved and by press on Esc the empty row will be removed. The code will be simple and it will do almost the same what you try to implement now.

JQGrid with Bootstrap Multiselect and Custom Save

I have 2 requirements I need to solve for. I'm using a JQGrid on an aspx page with other form controls. Saving the main form generates a database id that is populated in a hidden field. If the hidden field is empty, I don't want to save the grid record because I need the db id first. If the hidden field has a value, I want to save the row with a editurl.
The second part is related to a Bootstrap multiselect in one of the grid columns (I need the filtering capability). When a user selects a value from the multiselect I'd like to populate the PERNR column with the value selected. I can't use the onchange event for the multiselect since if the user doesn't change from the first option obviously it won't trigger the event.
How can I solve these 2 issues?
var roleDdlItems = $.parseJSON($("input[id$='hdfRoleDdlItems']").val());
$("#grdParticipants").jqGrid({
jsonReader: {
repeatitems: false,
root: 'Table',
page: function (obj) { return 1; },
total: function (obj) { return 2; },
records: function (obj) { return obj.ItemCount; },
id: "0"
},
caption: getParticipantCaption(),//"Participants",
pgbuttons: false,
recordtext: "Total: {2}",
emptyrecords: "",
loadtext: "Loading...",
pgtext: "",
datatype: function () {
UpdateParticipant("getParticipants");
},
ajaxGridOptions: { contentType: 'application/json; charset=utf-8' },
// serializeGridData: function (postData) { return JSON.stringify(postData); },
colNames: ['Id', 'PERNR', 'Name', 'Email', 'RoleId', 'Role', 'Active', 'Updated'],
colModel: [
{ name: 'Id', index: 'Id', hidden: true, editable: true, hidedlg: true },
{ name: 'PERNR', index: 'PERNR', hidden: true, editable: true, hidedlg: true },
{ name: 'Name', index: 'Name', width: 200, align: "center", editable: true, edittype: "select",
editoptions: { value: function () {
var ITDdlItems = $.parseJSON($("input[id$='hdfITEmployeesDdlItems']").val());
var s = "";
if (ITDdlItems.Table4 && ITDdlItems.Table4.length) {
for (var i = 0, l = ITDdlItems.Table4.length; i < l; i++) {
var item = ITDdlItems.Table4[i];
s += item.PERNR + ":" + item.Name + ";";
}
}
s = s.substring(0, s.length - 1);
return s;
},
dataInit: function (e) {
$(e).multiselect({
maxHeight: 200,
enableFiltering: true,
filterPlaceholder: "Search Employees",
enableCaseInsensitiveFiltering: true,
templates: {
filter: '<li class="multiselect-item filter"><div class="input-group"><input class="form-control input-sm multiselect-search" id="txtITEmployeeFilter" type="text"></div></li>',
filterClearBtn: "<span class='input-group-btn'><button class='btn btn-default input-sm multiselect-clear-filter' type='button'><i class='glyphicon glyphicon-remove'></i></button><button class='btn btn-primary input-sm multiselect-clear-filter' type='button' onclick='javascript:searchEmployees("#" + e.id + "");'><i class='glyphicon glyphicon-search'></i></button></span>"
},
onDropdownHide: function (event) {
$(e).multiselect("updateButtonText");
$(e).multiselect("updateSelectAll");
}
// onChange: function(option, checked) {
// var selectedOption = $("#" + e.id + " option:selected").val();
// alert(selectedOption);
// }
});
}
}
},
{ name: 'Email', index: 'Email', width: 250, align: "center", editable: false },
{ name: 'RoleId', index: 'RoleId', hidden: true, editable: true, hidedlg: true },
{ name: 'Role', index: 'Role', width: 175, align: "center", editable: true, edittype: "select",
editoptions: { value: function () {
var roleDdlItems = $.parseJSON($("input[id$='hdfRoleDdlItems']").val());
var s = "";
if (roleDdlItems.Table3 && roleDdlItems.Table3.length) {
for (var i = 0, l = roleDdlItems.Table3.length; i < l; i++) {
var item = roleDdlItems.Table3[i];
s += item.Id + ":" + item.Role + ";";
}
}
s = s.substring(0, s.length - 1);
return s;
},
dataInit: function (e) {
$(e).multiselect({
maxHeight: 200,
onDropdownHide: function (event) {
$(e).multiselect("updateButtonText");
$(e).multiselect("updateSelectAll");
}
});
}
}
},
{
name: 'Active', index: 'Active', hidden: false, width: 75, align: "center", editable: true, hidedlg: true,
edittype: 'checkbox', editoptions: { value: "true:false", defaultValue: "Active" },
formatter: "checkbox", formatoptions: { disabled: true }
},
{ name: 'Updated', index: 'Updated', width: 100, align: "center", editable: false, sorttype: "date",
formatter: 'date', formatoptions: { newformat: 'n/j/y g:i A' }
}
],
rowNum: 5000,
autoWidth: true,
rowList: [],
pager: '#pgrParticipants',
toppager: true,
sortname: 'Name',
viewrecords: true,
sortorder: 'asc'
});
$('#grdParticipants').navGrid("#pgrParticipants", {edit: false, add: false, del: true, refresh: false, search: false, view: false});
$('#grdParticipants').inlineNav('#pgrParticipants',
// the buttons to appear on the toolbar of the grid
{
edit: true,
add: true,
del: true,
cancel: true,
editParams: {
keys: true
},
addParams: {
addRowParams: {
keys: true
}
}
});
$('#grdParticipants').navSeparatorAdd('#pgrParticipants', {
sepclass: 'ui-separator',
sepcontent: '',
position: 'last'
});
$('#grdParticipants').navButtonAdd('#pgrParticipants',
{
buttonicon: "ui-icon-help",
title: "Roles & Responsibilities Matrix",
caption: "Roles & Responsibilities Matrix",
position: "last",
onClickButton: showRoleMatrix
});
* EDIT *
The datatype function contains an ajax call that populates the grid. I’m using this based on an example someone else in my company provided. I’ll look around today for a better method, as it sounds this isn’t the best way. Maybe using url, which I stumbled across today.
This is a tab interface to track outages that will ultimately have a total of 5 separate JQGrids. When the page initially loads for a new outage this grid is on the first tab and is empty. It would only contain records if they’re editing an existing outage. In that instance, I would populate values in the main outage form controls and load data in the grid. The UpdateParticipant function seemed work well for this as it allows me to check for that hidden field before deciding whether a db call is even needed.
The PERNR is an employee id that the user doesn’t need to know, hence hidden. It’s not strictly confidential info, but we don’t advertise it either. They only need to see Name and Email (maybe) based on the value selected from the Name multiselect. Id will be a Participant Id generated by the database after the grid data is saved. Which can’t happen until the main outage form above is saved because I need that unique Outage Id as a foreign key for the participants. This attaches a participant to an outage and becomes the unique Id for the grid rows.
The selectedOption = $("#" + e.id + " option:selected").val() is commented out, but was only to trigger onchange for that specific Bootstrap Multiselect. The Role multiselect didn’t have an onchange function registered in code. So yes, it was for one element only. As mentioned previously, I was trying to use this to populate PERNR on a Name select. I need PERNR not Name to save to the db later. To solve for this I’m using the custom formatter on that column so I’ve since removed that onchange function for the Name multiselect. The ITDdlItems = $.parseJSON($("input[id$='hdfITEmployeesDdlItems']").val()) contains a json list of employee names and PERNRs to populate the Name multiselect. That works fine so this isn’t code relevant.
The only thing I need to figure out is how to perform a check for OutageId in the hidden field before the grid data is posted to the server. Thinking about it last night, if all else fails, I’ll let it post to the server and check there for the OutageId. I just don’t like posting if it isn’t going to make a db call. I’d share a demo somewhere if I knew how. This is an internal application so my options are limited.

jqgrid inline searching

I have a jqgrid that I am performing an inline edit on. I am using the textarea instead of text. How do I submit the data once I'm done editing the cell. "Enter" works on text and obviously doesn't on textarea since it creates a new line.
This is a snippet from my code
grid4 = $('#CaseNotes').jqGrid({
...
{ name: 'Note', index: 'Note', width: 650, align: 'left', sortable: false,
editable: true, edittype: 'textarea', editoptions: { rows: '5', cols: '100' }
},
...
onSelectRow: function (id) {
if (id && id != lastsel) {
grid4.restoreRow(lastsel);
lastsel = id;
}
grid4.jqGrid('editRow', id, true, '', '', '', '', reload);
},
editurl: '#Url.Action("EditCaseNote", "CaseNote")',
...
});
//function to reload the grid
function reload(id, result) {
grid4.setGridParam(
{
url: '#Url.Action("DisplayCaseNotesGrid", "CaseInfo")',
datatype: 'json'
}
).trigger('reloadGrid');
}
If I understand you correct you mean "inline editing" instead of "inline searching". Because you can't use Enter key you have to include some additional button in the Navigator toolbar which call saveRow method. You can either add the corresponding row manually with respect of navButtonAdd or use inlineNav method.
Thanks to Oleg's advise I was able to solve this. Here is my code incase anyone runs into a similar problem. I removed my refresh function since I no longer needed it.
grid4 = $('#CaseNotes').jqGrid({
...
{ name: 'Note', index: 'Note', width: 650, align: 'left', sortable: false,
editable: true, edittype: 'textarea', editoptions: { rows: '5', cols: '100' }
},
...
onSelectRow: function (id) {
if (id && id != lastsel) {
grid4.restoreRow(lastsel);
lastsel = id;
}
grid4.jqGrid('editRow', id, { keys: true, afterrestorefunc: reload });
},
...
});
//Adds the button to the pager
grid4.jqGrid('navButtonAdd', '#casenotes_pager', {
caption: 'Save Case Note',
buttonicon: 'none',
onClickButton: function () {
//calls the saveRow function
grid4.jqGrid('saveRow', lastsel,
{
url: '#Url.Action("EditCaseNote", "CaseNote")'
}
);
//refreshes the grid
grid4.setGridParam(
{
url: '#Url.Action("DisplayCaseNotesGrid", "CaseInfo")',
datatype: 'json'
}
).trigger('reloadGrid');
}
});

jqGrid has missing id in checkbox

new to jqGrid. I'm displaying a grid, readonly so far, but now with a checkbox in one column. However, the checkbox is not produced with id or name attributes - which according to the documentation it will be. Any pre-requisites to get the id's? Ideally I'd like an id on teh checkbox that is like the prefix + rowid.
Code is:
jQuery("#grdProducts").jqGrid(
{
url: '<%: Url.Action( "GetPageByCode", "Products" ) %>',
datatype: "xml",
mtype: 'POST',
colNames: [ 'Code', 'Language', 'Type', 'Name', 'Price £', 'Web', 'Dimensions', 'Actions' ],
colModel:
[
{ name: 'code', index: 'ProductCode', width: 120, key: true },
{ name: 'language', index: 'Category1', width: 100 },
{ name: 'type', index: 'Category2', width: 140 },
{ name: 'name', index: 'Name', width: 240 },
{ name: 'price', index: 'PriceGross', width: 60, align: "right" },
{ name: 'web', width: 32, align: "center",
editable:true, edittype:'checkbox', editoptions: {value:"True:False" },
formatter: "checkbox", formatoptions: {disabled : false } },
{ name: 'dimensions', width: 120, sortable:false },
{ name: 'actions', width: 120, sortable:false }
],
height:660,
rowList:[ 20,50,100 ],
pager: '#pgrProducts',
sortname: 'ProductCode',
// speed improvement - as long as certain features not needed
gridview: true,
// shows the 'showing 1 - n of total' in the pager, ... and the text to display
viewrecords: true,
recordtext: "showing products {0} - {1} of {2}",
// alternate colour rows
altRows:true, altclass: 'gridAltRow',
// shows sort arrows in titles
viewsortcols: [ true ],
// the grid outer caption
caption: 'Products by product code',
// configurable page size
rowNum: <%: Model.PageSize %>,
// double click on a row goes to the edit row page
ondblClickRow: function( rowid )
{
document.location.href = '/Product/Edit/' + rowid;
}
}
);
} );
Why you need to have id attribute in the checkbox? If you need to get checkbox it will be very easy in your case because there are only one checkbox in the row, so you can use
$('#' + rowid + ' input:checkbox')
If you need to detect 'click' event for example to the checkbox you can find the corresponding code in the demo from the answer.

Resources