Table of Contents

Cell Editing

Cell editing provides the frontend user with functionality to change the contents of one single cell from a row and then the developer has the abbility to handle the changed data by AJAX or in a jqGrid cell edit event (see below).

Cell Editing supports key navigation and editing individual cells, with the following behaviour:

When we click on a cell that is not editable, the cell is selected and we can use the up, down, left and right keys to navigate through the cells.

If we move to a cell that is editable, we can press [Enter] to edit the cell. The cell is saved when we press [Enter] again, when we press [Tab], or when we click on another cell. If we press [ESC], the cell is not saved. When editing a cell, the cursor keys move only within the cell.

When we click on cell that is editable, then we go directly into edit mode.

The cell is not editable if it has a class 'not-editable-cell', instead that in colModel is set to be editable

Installation

In order to use this module you should mark the Cell editing and Common when you download the grid. For more information refer to Download.

For Developers of the JQuery plugin itself they can use the grid.celledit.js file in the src directory.

The properties, events and methods used in cell editing are a sub-set of those of the parent grid, and described below

Properties

These properties are specific for cell editing and should be set in grid options

Property

Type

Description

Default

cellEdit

boolean

Enables (disables) cell editing. When this option is set to true, onSelectRow event can not be used, and hovering is disabled (when mouseover on the rows).

false

cellsubmit

string

Determines where the contents of the cell are saved - can have two values: 'remote' or 'clientArray'.
If remote the content of the cell if anything is changed is immediately saved to the server using the cellurl property, via ajax. The rowid and the cell content are added to the url by default. If you have the “mtype” setting set to post, the key value pears will be send as post variables. For example, if we save the cell named mycell,{id: rowid, mycell: cellvalue} is added to the url.
If cellsubmit is 'clientArray', no ajax request is made and the content of the changed cell can be obtained via the method getChangedCells or thru a event.

remote

cellurl

string

the url where the cell is to be saved. You need to set this only when you use cellsubmit as 'remote'.

null

ajaxCellOptions

object

This option allow to set global ajax settings for the cell editiing when we save the data to the server. Note that with this option is possible to overwrite all current ajax setting in the save request including the complete event.

empty object

Examples

When using the 'remote' setting you could add these lines to your grid configuration:

When your are using 'clientArray' as cellsubmit setting, only these settings are important:

{'cellEdit':true,'cellsubmit':'remote'}

Events

These events are related to cell editing and should be used in grid options.

Many of the following events use the parameters defined here:

rowid - is the id of the row

cellname is the name of the cell (name from colModel)

value - the value of the cell

iRow - the index of the row (do not mix with rowid)

iCol - the index of the column

Event

Parameters

Description

afterEditCell

rowid, cellname, value, iRow, iCol

applies only to a cell that is editable; this event fires after the edited cell is edited - i.e. after the element is inserted into the DOM

afterRestoreCell

rowid, value, iRow, iCol

applies only to a cell that is editable; this event fires after calling the method restoreCell or the user press ESC leaving the changes

afterSaveCell

rowid, cellname, value, iRow, iCol

applies only to a cell that is editable; this event fires after the cell has been successfully saved. This is the ideal place to change other content.

afterSubmitCell

serverresponse, rowid, cellname, value, iRow, iCol

applies only to a cell that is editable; this event Fires after the cell and other data is posted to the server Should return array of type [success(boolean),message] when return [true,“”] all is ok and the cellcontent is saved [false,“Error message”] then a dialog appears with the “Error message” and the cell content is not saved. servereresponse is the response from the server. To use this we should use serverresponse.responseText to obtain the text message from the server.

beforeEditCell

rowid, cellname, value, iRow, iCol

applies only to a cell that is editable; this event fires before editing the cell.

beforeSaveCell

rowid, cellname, value, iRow, iCol

applies only to a cell that is editable; this event fires before validation of values if any. This event can return the new value which value can replace the edited one
beforeSaveCell : function(rowid,celname,value,iRow,iCol) {
if( some_condition )
{ return “new value”; }
}
The value will be replaced with “new value”

beforeSubmitCell

rowid, cellname, value, iRow, iCol

applies only to a cell that is editable; this event fires before submit the cell content to the server (valid only if cellsubmit : 'remote'). Can return new array that will be posted to the server.
beforeSubmitCell : function(rowid,celname,value,iRow,iCol) {
if( some_condition ) {
return {name1:value1,name2:value2}
}
else
{ return {} }
}
The returned array will be added to the cellurl posted data.

errorCell

serverresponse, status

fires if there is a server error; servereresponse is the response from the server. To use this we should apply serverresponse.responseText to obtain the text message from the server. status is the status of the error. If not set a modal dialog apper.

formatCell

rowid, cellname, value, iRow, iCol

applies only to a cell that is editable; this event allows formatting the cell content before editing, and returns the formatted value

onSelectCell

rowid, celname, value, iRow, iCol

applies only to cells that are not editable; fires after the cell is selected

serializeCellData

postdata

If set this event can serialize the data passed to the ajax request when we save a cell. The function should return the serialized data. This event can be used when a custom data should be passed to the server - e.g - JSON string, XML string and etc. To this event is passed the data which will be posted to the server

Event calling stack

Depending on your cellSubmit setting set to 'remote' or 'clientArray' the following events will be fired in the following order:

cellSubmit setting 'remote'

formatCell (rowid, cellname, value, iRow, iCol) : You can change the cell value here which will be used in the edit mode

beforeEditCell (rowid, cellname, value, iRow, iCol) : Just before the cell jumps into edit mode this event is fired

afterEditCell (rowid, cellname, value, iRow, iCol) : Just after the input element is created this event will be fired

beforeSaveCell (rowid, cellname, value, iRow, iCol) : Fires before the cell is saved, you can save the value here which is send to the server.

beforeSubmitCell (rowid, cellname, value, iRow, iCol) : Fires before value will be send to the server, you can add extra parameters here to the request by returning an array

afterSubmitCell (serverresponse, rowid, cellname, value, iRow, iCol) : Fires only if saving cell is succesful, you can return a error message here that causes a dialog with the message

afterSaveCell (rowid, cellname, value, iRow, iCol) : Fires when the value of the cell is saved

Methods

All of the methods below should be applied to the jqGrid object and all of them return the same object.

Method

Parameters

Description

editCell

iRow, iCol, edit

edit a cell with the row index iRow( do not mix with rowid) in index column iCol. If the edit is set to false the cell is just selected and not edited. If set to true the cell is selected and edited.

getChangedCells

method

Returns an array of the changed cells depending on method (string, default 'all'). When 'all' this method returns all the changed rows; when 'dirty' returns only the changed cells with the id of the row

restoreCell

iRow, iCol

restores the edited content of cell with the row index iRow( do not mix with rowid) in index column iCol

saveCell

iRow, iCol

saves the cell with the row index iRow( do not mix with rowid) in index column iCol

Notes

How is the data organized

When the cell is edited and the input elements is created we set the following rules:

The id of the editable cell element is constructed as 'iRow_'+ the name from the colModel array - where the iRow is the index of the row (not rowid) Example if we edit cell with index=20 and editable element has name 'myname' (from colModel) then the id becomes 20_myname.

The name of the editable element is constructed from the name of the colModel array - property - name

What is posted to the server?

When the data is posted to the server we construct object {} that contain:

the name:value pair where the name is the name of the input element represented in the cell

additionally we add a pair id:rowid where the rowid is the id of the row

if the returned data from beforeSubmitCell event is not empty we extend this data with the posted data.

jqGrid cell edit is one large bug. Never use it. It is absolutely unfriednly to addRowData and delRowData methods. Cells just become unselectable…

David Paigen, 2011/07/08 15:16

I want to process new data on the server and hand back something slightly different, e.g. expanding abbreviation: 'django has an orm' becomes 'django has an object request manager'.

I tried adding a js function as afterSubmitCell where I check the return status from the server, but it only partly works. jqGrid.restoreCell works fine if there is a server failure, but calling jqGrid.setCell from inside afterSubmitCell apparently leads to some kind of recursion bug. I either get an explicit recursion error or a notification that 's' is undefined on line 206.

Any suggestions?

Tom St.Louis, 2011/09/01 17:46

I have read all the documentation regarding jqGrid cell editing and I've yet to see an example making use of the cell events afterEditCell, beforeSaveCell, beforeSubmitCell. I'm using a treegrid with the adjacency model and in setting up the grid seem to be controlling what rows and cell I actually want to make editable. I have yet to figure out or stumble on an example using the above grid options that works. In my case, I change the cell data and hit enter and get error: e is undefined. I would guess e is the event but I'm not sure where this is falling apart. Any help you can provide is appreciated. Thanks

If you use cell editing jqGrid examine cells for the 'not-editable-cell' class. If you use inline editing the jqGrid rows for the 'not-editable-row', but in the inline editing mode the 'not-editable-cell' class will be ignored.

Theron, 2011/12/08 21:33

I found it useful to select automatically the contents if the input box when selecting the inline cell edit. i put this in the Grid Options: