views:

187

answers:

2

Some quick searching only turns up adding a new row to a jQGrid via a modal popup with the editable fields.

Can anyone point me to a sample or show me some code that allows you to add a new empty row into the grid itself, at the top?

I have an action column at the rightmost end of the grid in which onRowSelect() I have a save button appear and I can make that button do the save and refresh the grid I think..

I can't figure out how to click on a 'Add Row' button and add an empty row inside the grid at the top.

One option that I can see is to style the current add row modal to look like a horizontal row and place it to appear like its a row at the top of the grid.

jQGrid Documentation: http://www.trirand.com/jqgridwiki/

+1  A: 

If you use datatype:'local' then you can use addRowData method to insert the row with position parameter set to 'first'. See some examples under http://www.trirand.com/jqgridwiki/doku.php?id=wiki:retrieving_data#array_data.

Oleg
I'm using datatype json at the moment. Is there a way I can use addRowData without changing to local?
Moin Zaman
@Moin Zaman: I am not sure, that I understand correct what you want to do. The only assumption is that you use inline editing mode and want not use form editing to and new data. You want to add an empty row and allow user to edit it in the inline editing mode. Is it what you about want? If not, please explain more in detail why do you want add any data in grid which display the data from the server.
Oleg
@Oleg: I am populating the grid from a database, extracting the data as json. I also plan to save new rows or edited rows back to the database. I want to add an empty row when the user wants to create a new row, the user can then fill in the values and hit save in the action column to save this row back into the database. I can then refresh the grid and retrieve the json data again, and it should contain the newly added row.
Moin Zaman
@Mon Zaman: The standard way to add new record in jqGrid is using "New" button from the Navigator (see http://www.trirand.com/jqgridwiki/doku.php?id=wiki:navigator and http://www.trirand.com/jqgridwiki/doku.php?id=wiki:form_editing). You can just call `jQuery("#grid_id").jqGrid('navGrid','#gridpager');` after the grid creation. After the user fill the form and click "submit", the form submit data to the server to save it in the database. If all will be without error the server should send `id` of the new record back. After that the data will be inserted in the grid. Is the way is OK for you?
Oleg
+1  A: 

This answer is courtesy of Oleg in my previous question here:

use $("#grid").addRowData(rowid,data, position, srcrowid);

Inserts a new row with id = rowid containing the data in data (an object) 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 object is: {name1:value1,name2: value2…} where name is the name of the column as described in the colModel and the value is the value. This method can insert multiple rows at once. In this case the data parameter should be array defined as [{name1:value1,name2: value2…}, {name1:value1,name2: value2…} ] and the first option rowid should contain the name from data object which should act as id of the row. It is not necessary that the name of the rowid in this case should be a part from colModel.

Regards, Byron Cobb

P.S. Have a look at my profile for a number of jqgrid questions and answers.

Byron Cobb