I found myself in need of what i guess should be a trivial requirement. i have a jqGrid in which i have added a custom button in each row. now i am able to associate a client side click event with it but i want to know the key value (Id) in my case of the row whose custom button was clicked, so that i can proceed with this id and do whatever i want to do.
My code for jqGrid is as below
jQuery("#JQGrid").jqGrid({
url: 'http://localhost:55423/JQGrid/JQGridHandler.ashx',
datatype: "json",
colNames: ['', 'Id', 'First Name', 'Created Date', 'Member Price', 'Non Member Price', 'Complete', 'customButton'],
colModel: [
{ name: '', index: '', width: 20, formatter: "checkbox", formatoptions: { disabled: false} },
{ name: 'Id', index: 'Id', width: 20, stype: 'text', sortable: true, key: true },
{ name: 'FirstName', index: 'FirstName', width: 120, stype: 'text', sortable: true },
{ name: 'CreatedDate', index: 'CreatedDate', width: 120, editable: true, sortable: true, hidden: true, editrules: { edithidden: true} },
{ name: 'MemberPrice', index: 'MemberPrice', width: 120, editable: true, sortable: true },
{ name: 'NonMemberPrice', index: 'NonMemberPrice', width: 120, align: "right", editable: true, sortable: true },
{ name: 'Complete', index: 'Complete', width: 60, align: "right", editable: true, sortable: true },
{ name: 'customButton', index: 'customButton', width: 60, align: "right" }
],
rowNum: 10,
loadonce: true,
rowList: [10, 20, 30],
pager: '#jQGridPager',
sortname: 'Id',
viewrecords: true,
sortorder: 'desc',
caption: "List Event Details",
gridComplete: function () {
jQuery(".jqgrow td input", "#JQGrid").click(function () {
//alert(options.rowId);
alert("Capture this event as required");
});
}
});
jQuery('#JQGrid').jqGrid('navGrid', '#jQGridPager',
{
edit: true,
add: true,
del: true,
search: true,
searchtext: "Search",
addtext: "Add",
edittext: "Edit",
deltext:"Delete"
},
{/*EDIT EVENTS AND PROPERTIES GOES HERE*/ },
{/*ADD EVENTS AND PROPERTIES GOES HERE*/},
{/*DELETE EVENTS AND PROPERTIES GOES HERE*/},
{/*SEARCH EVENTS AND PROPERTIES GOES HERE*/}
);
Help or any pointers would be much appreciated.
The main solution of your problem in the following: you need include parameter, for example
e
, in the callback ofclick
handle. The parameter has Event object type which contain target property. Alternatively you can usethis
in the most cases. Havinge.target
you can goes to the closest parent<tr>
element. It'sid
is the value which you need:Additionally you should make some other modifications in your code to fix some bugs. The usage of
is wrong in
colModel
. You should specify any non-empty unique name. For examplename: 'mycheck'
.Next I recommend you to remove all
index
properties fromcolModel
. If you useloadonce: true
you have to useindex
properties with the same values as the correspondingname
values. If you don't specify anyindex
properties you will have smaller and better readable code. The corresponding values ofindex
properties will be copied by jqGrid internally from the correspondingname
values. In the same way you can remove properties likestype: 'text', sortable: true
which values are default values (seeDefault
column of the documentation)The next problem is that you include probably HTML data in the JSON response from the server. (One can't see any formatter for
customButton
for example). It's not good. In the way you can have problems if the texts of the grid contains special HTML symbols. I find better to use pure data in JSON response from the server. One can use formatters, custom formatters etc on the client side. In the case one can useautoencode: true
option of jqGrid which make HTML encoding of all texts displayed in the grid. In the way you will have more safe code which will don't allow any injection (for example no including of JavaScript code during editing of data).Next remark: I don't recommend you to use
gridComplete
. The usage ofloadComplete
is better. See my old answer about the subject.The last important remark. To handle
click
events on the buttons placed inside of grid one don't need to bind separateclick
handle to every button. Instead of that one can use onebeforeSelectRow
oronCellSelect
callback. The answer and this one describe this. The answers use<a>
in custom formatter, but<input>
works exactly in the same way.