JQGrid Documentation on Properties and Methods
Requirements
- jqGrid plugin,
- jQuery library, version 1.1.4 or later, and
- A web browser To manipulate and represent local (static) data – i.e. array data, data stored in an xml file, or data stored in a JSON file – that’s all you need. But the primary purpose of jqGrid is to manipulate and represent dynamic data over the web, and for this you will also need
- A web server (e.g., IIS, Apache, Tomcat),
- A database back-end (e.g., Postgre SQL, Oracle, MSSQL, MySQL), and
- A server-side scripting language (e.g., PHP, ASP)
JQGrid is a component that helps you, in an easy way, to represent database information on the client side using a server-side technology. Moreover it helps you to manipulate that data back into the database.
JQGrid uses Ajax calls to retrieve the requested information and represent it to client using Java Script language.
JQGrid constructs the representation (tabular data) described by you in what is called the Column Model.
The constructed tabular data at the client side has:
- Caption layer
- Header layer
- Body layer
- Navigation layer
Caption layer contains common information for the represented data.
Header layer contains information about the columns: labels, width, etc.
Body layer is the data requested from the server and displayed according to the settings in the column model.
Navigation layer contains additional information from the requested data and actions for requesting little pieces of information – in the literature called paging. Note that the navigation layer can be placed not only at bottom of the grid, but anywhere on the page. The Navigation layer is also the place for adding buttons or links for editing, deleting, adding to and searching your grid data.
First, we need to include the files required to construct the grid. This is done between the head tags in the html document.
StyleSheet grid.css
Scripts jquery.js jquery.jqGrid.js
- The link tag loads the style sheet for jqGrid,
- The first script tag loads the jquery library,
- The second script tag loads the required jqGrid plugins,
- The third and fourth script tags load the additional modules
required for some functions, and - The last script tag is where we write the commands needed to construct the grid.
- Between the body tags you define where you want to place the grid.
- table tag to define grid
- div tag to define grid pager
The syntax for constructing the grid is:
jQuery('#grid_selector').jqGrid(options )
- grid_selector is the unique id of the grid table
- jqGrid is the plugin, and
- options is an array.
Let’s begin with the options array, which looks like this: (These options can appear in any order)
{ url:'@Url.Action(ActionName, ControllerName)', datatype: 'xml', mtype: 'GET', colNames:['Col1','Col2','Col3','Col4','Col5','Col6'], colModel :[ {name:'Col1',index:'Col1', width:55}, {name:'Col2',index:'Col2', width:90}, {name:'Col3',index:'Col3', width:80, align:'right'}, {name:'Col4',index:'Col4', width:80, align:'right'}, {name:'Col5',index:'Col5', width:80,align:'right'}, {name:'Col6',index:'Col6', width:150, sortable:false} ], pager: jQuery('#pager'), rowNum:10, rowList:[10,20,30], sortname: 'id', sortorder: 'desc', viewrecords: true, imgpath: 'themes/basic/images', caption: 'My firstgrid' }
Retrieving Data
datatype: the possible options are – ‘xml’, ‘json’,’clientSide’ or ‘local’, ‘xmlstring’, ‘jsonstring’ and ‘function (…)’
Properties
colModel
The colModel property defines the individual grid columns as an array of
properties.
Syntax:
colModel:
[ {name:’name1′, index:’index1’…}, {…}, … ]
Properties
Events
onSelectRow: function(id){ if(id && id!==lastSel){ jQuery('#tbleditable').restoreRow(lastSel); lastSel=id; } jQuery('#tbleditable').editRow(id, true); }, onSortCol: function( index, colindex, sortorder) { // here is the code }
Methods
jQuery(“#grid_id”).jqGridMethod(
parameter1,…parameterN )
- grid_id is the id of the already constructed jqGrid.
- jqGridMethod is a method applied to this jqGrid.
- parameter1,…parameterN – a list of parameters
Where a method is not designed to return a requested value, then what is returned is the jqGrid object and a set of such methods can be chained, e.g., jQuery(“#grid_id”).setGridParam({..}).hideCol(“somecol”).trigger(“reloadGrid”)
Method | Parameters | Returns | Description |
addJSONData | data | true on success, otherwise false | Populates a grid with the passed data (an array). Suppose we have data from a particular webservice (jsonresponse), then var mygrid = jQuery(“#”+grid_id)[0]; var myjsongrid = eval(“(“+jsonresponse.responseText+”)”); mygrid.addJSONData(myjsongrid); myjsongrid = null; j sonresponse =null; Will populate the data to the grid. And, of course, the data in myjsongrid can be manipulated before being passed to addJSONData. addJSONData is a privileged method. |
addRowData | rowid, data, position (first, last, before, after – default last), srcrowid (source row, applies only when position is either before or after) | true on success, otherwise false | Inserts a new row with id = rowid containing the data in data (an array) at the position specified (first in the table, last in the table or before or after the row specified in srcrowid). The syntax of the data array is: {name1:value1,name2: value2…} where name is the name of the column as described in the colModel and the value is the value. |
addXmlData | xmlresponse | true on success, otherwise false | Populates a grid with the passed data. Suppose we have data from a particular webservice (xmlresponse), then var mygrid = jQuery(“#”+grid_id)[0]; mygrid.addXmlData(xmlresponse.responseXML); Will populate the data to the grid. And, of course, the data in xmlresponse can be manipulated before being passed to addXmlData. addXmlData is a privileged method. |
clearGridData | none | jqGrid object | Clears the currently loaded data from grid |
delRowData | rowid | true on success, otherwise false | Deletes the row with the id = rowid. This operation does not delete a data from the server. |
FormToGrid | rowid, formid | jqGrid object | Reads data from a form (previously defined in html) identified by formid and loads data into the grid in row with rowid. If the names of both grid and form are the same the data from the form replaces the data in the grid. Note that all fields from grid can be replaced, including hidden. This is the opposite of GridToForm |
getCell | rowid, iCol | the content of the cell | iCol can be either the column index or the name. |
getDataIDs | none | array of the id’s in the current grid view. Empty array if no data is available. | |
getGridParam | name | the value of the requested parameter. | Name is the name from options array. For a particular options, see below. If the name is not set the entry options are returned. |
getRowData | rowid | array with data of the requested id = rowid. | The returned array is of type name:value, where the name is a name from colModel and the value is a actual value. Returns empty array if the row can not be found. |
GridToForm | rowid, formid | jqGrid object | Reads data from the given rowid and fills the form (previously defined in html) identified by formid. If the names of both grid and form are the same the data from the grid replaces the data in the form. Note that all fields from grid can be used, including hidden. |
hideCol | colname | jqGrid object | Hides a column with a given colname. If the colname is a string, only the specified column is hidden. If the colname is array of type [“name1″,”name2”] then the columns with names ‘name1’ and ‘name2’ will be hidden at the same time. The names in colname must be valid names from the colModel. The width of the grid is changed according to the following rules: if the grid currently has no horizontal scroll bar, the width of the grid is decreased by the width of the hidden column(s). If a scrollbar is visible, the width is adjusted which may or may not change the width of the grid. |
resetSelection | none | jqGrid object | Resets (unselects) the selected row(s). Also works in multiselect mode. |
setCaption | caption | jqGrid object | Sets a new caption of the grid. If the Caption layer was hidden, it is shown. |
setCell | rowid, colname, data, class, properties | jqGrid object | This method can change the content of particular cell and can set class or style properties. Where: rowid: the id of the row, colname: the name of the column (this parameter can be a number beginning from 0) data: the content that can be put into the cell. If empty string the content will not be changed class: if class is string then we add a class to the cell using addClass; if class is an array we set the new css properties via css properties: sets the attribute properies of the cell Example : setCell(“10”, “tax”, “, {color:’red’,’text-align’:’center’}’,{title:’Sales Tax’}) will set the contents of the tax field in row 10 to red and centered and change the title to ‘Sales Tax’. |
setGridParam | object | jqGrid object | Sets a particular parameter. Note – for some parameters to take effect a trigger(“reloadGrid”) should be executed. Note that with this method we can override events like onSelectRow, etc. Example: setGridParam({ url:”newurl”, page:1, onSelectRow:function(id){/*here is the new code*/} }); The name (in the name:value pair) is the name from options array. If the name is not set the entry options are returned. |
setGridHeight | new_height | jqGrid object | Sets the new height of the grid dynamically. Note that the height is set only to the grid cells and not to the grid. <>new_height<> can be in pixels, percentage, or ‘auto’ |
setGridWidth | new_width, shrink | jqGrid object | Sets a new width to the grid dynamically. The parameters are: new_width is the new width in pixels. shrink (default true) has the same behavior as shrinkToFit |
setLabel | colname, newlabel, sattr | jqGrid object | Sets a new label in the header for the specified column; can also set attributes and classes (sattr). The parameters are: colname(mixed) is either the name of the column (from colModel) or the number of the column in colModel beginning from 0. newlabel(string) is the label that we want to change. Can be a empty string. sattr(mixed) – if this parameter is array we add this as attributes to this header element. if the parameter is string we add a class to this element |
setRowData | rowid, data | true on success, otherwise false | Updates the values (using the data array) in the row with rowid. The syntax of data array is: {name1:value1,name2:value2…} where the name is the name of the column as described in the colModel and the value is the new value. |
setSelection | rowid, onsetselection | jqGrid object | Toggles a selection of the row with id = rowid; if onsetselection is true (the default) then the event onSetRow is launched, otherwise it is not |
showCol | colname | jqGrid object | Shows a column with a given colname. If the colname is a string we show only the specified column. If colname is array of type [“name1″,”name2”] then the columns with names ‘name1’ and ‘name2’ will be shown at the same time The names in colname must be valid names from colModel. The width of the grid changes by the width of the newly-shown columns. |
.trigger(“reloadGrid”); | none | jqGrid object | Reloads the grid with the current settings. This means that a new request is send to the server if datatype is xml or json. This method should be applied to an already-constructed grid – e.g., jQuery(“#grid_id”).trigger(“reloadGrid”); |
getColProp | colname | an array of the properties of the given column from colModel | |
GridDestroy | grid_id | true on success, otherwise false | Destroys the entry grid from the DOM (clears all the html associated with the grid and unbinds all events) |
GridUnload | grid_id | true on success, otherwise false | The only difference to previous method is that the grid is destroyed, but the table element and pager (if any) are left ready to be used again. |
setColProp | colname, properties | jGrid object | Sets new properties in colModel. This method is ideal for dynamically changing properties of the column. Note that some properties – like width and align – have no effect. For example: jQuery(“#grid_id”). setColProp(‘colname’,{editoptions:{value:”True:False”}}) will change the editoptions values. |
sortGrid | d colname,reload | jGrid object | Sorts the given colname and shows the appropriate sort image. The same (without sorting image) can be done using setGridParam({sortname:’myname’}).trigger(‘reloadGrid’) If the reload is set to true, the grid reloads with the current page and sortorder settings. |
Please click here for more information on JQGrid.