I’m exploring jqGrid in my journey of learning Javascript and jQuery and I manged to put a checkbox in a grid cell, awesome!
Here’s what I have:
$("#myTable").jqGrid({
colModel:[
name:'cb', index:'cb', width:40, sorttype:"text", align:"center",
edittype:"checkbox", editoptions:{value:"Yes:No"}, formatter: "checkbox",
formatoptions: {disabled : false}},
other stuff...
]
When a checkbox is clicked, how do I catch the event and determine the corresponding row data?
Also, when I click the checkbox is the underlying data on the client side updated (does the cb field switch "Yes"/"No")? How do I achieve this?
First of all you should not use
'cb'as thenameof the column because it is reserved column name. Other two reserved column names are'subgrid'and'rn'. Just use any other name if you don’t want to have strange problems.You have to bind the
clickevent manually to your event handler. To do this you have some options.You can bind
clickto all checkboxes inside of theloadCompletecallback. See the answer where are used jQuery to enumerate all checkboxes. Another answer shows a little more effective way which use the fact that DOM of<table>,<tr>and<td>supports already native implementedrowsandcellscollections. So you can access<td>bythis.rows[iRow].cells[iCol]inside ofloadComplete.One more way will be to use custom formatter instead of
formatter: 'checkbox'and useonclickattribute for binding.UPDATED: If you use local data in the grid you have to update the corresponding value manually. See the answer for example. It describes how to use
getLocalRowor usedataand_indexinternal parameters of jqGrid.To get
idof the row on which the user clicked you can use target of the current event$(e.target).closest("tr.jqgrow").attr('id').