Reputation: 4692
I want to be able to get an ID value from a row in a jqGrid when a button click event occurs that is not bound to the grid.
In other words, if a user has a row selected on the grid and also clicks a button not associated with the grid, I'd like the onCellSelect event to be fired where that button click event could pass down the ID retrieved from the grid (via the onCellSelect event).
So, here would be a sample of the code I'm trying to execute, but don't know how to wire this up. If someone could please help, I would greatly appreciate it.
$('#btnSPStop').on('click', function (e)
{
// Somehow, I need to fire an event here (similiar to below) where I can retrieve the selected grid row
onCellSelect: function (rowid, iCol, cellcontent) {
var grid = $('#fuelTicketsGrid');
var ID = grid.jqGrid('getCell', rowid, 'ID');
if (ID != "")
saveStopComments(ID);
else
alert("User must first have row selected in grid!");
},
return false;
});
Oleg, after implementing your code, I get the following error message:
I get a valid "selRowId" when debugging and selecting the second row in the grid (ie a "2").
I checked the JS for the jqGrid and "rowid" is defined. Am I missing something on the definition of the grid that I need to include for the rowed to be defined?
I get the error on the below line:
ID = grid.jqGrid('getCell', rowid, 'ID');
This is the entire modified function:
$('#btnSPStop').on('click', function (e)
{
var grid = $('#StopPenalizeGrid-table'), ID,
selRowId = grid.jqGrid('getGridParam', 'selrow');
if (selRowId !== null)
{
ID = grid.jqGrid('getCell', rowid, 'ID');
if (ID != "")
$('#txtStopGridID').val(ID);
else
alert("You must have a row selected on the grid before clicking the Stop button!");
}
else
alert("selRowId is equal to null for btnSPStop onClick method.");
return false;
});
Upvotes: 0
Views: 3650
Reputation: 221997
It seems to me that you don't need to execute onCellSelect
event. What you need to do is just to get the id of selected row and then the get the content of "ID"
column of the selected row. On can do all above steps just using methods of jqGrid. The corresponding code could be like below:
$('#btnSPStop').on('click', function (e) {
var grid = $('#fuelTicketsGrid'), ID,
selRowId = grid.jqGrid('getGridParem', 'selrow');
if (selRowId !== null) {
ID = grid.jqGrid('getCell', selRowId, 'ID');
if (ID != "") {
saveStopComments(ID);
} else {
alert("some other error message");
}
} else {
alert("User must first have row selected in grid!");
}
return false;
});
Upvotes: 1