I want to add a hyperlink / button in each row of jqgrid, that fires a custom javascript function. Tired of various trials.
jQuery('#ProductListGrid').jqGrid({
url: '/Product/ProductListGrid',
datatype: 'json',
multiselect: true,
height: 250,
autowidth: true,
mtype: 'GET',
loadComplete: addlinks,
colNames: ['ProductId', 'ProductName', 'edit'],
colModel: [
{ name: 'ProductId', index: 'ProductId',key:true, width: 70, hidden: true, editable: true, size: 5 },
{ name: 'ProductName', index: 'ProductName', width: 70, editable: true },
{ name: 'edit', index: 'edit', width: 70},
],
pager: jQuery('#ProductListGrid_pager'),
});
function addlinks() {
var ids = jQuery("#ProductListGrid").getDataIDs();
alert(ids);
for (var i = 0; i < ids.length; i++) {
be = "<a href='#' style='height:25px;width:120px;' type='button' title='Slet' onclick=\"ff()\" >Slet</>";
jQuery("#ProductListGrid").jqGrid('setCell', ids[i],'edit','', { act: be });
}
//for (var i = 0; i < ids.length; i++) {
// jQuery("#ProductListGrid").setCell(i, 'edit', '<a href="#">edit</edit>');
//}
}
function ff()
{
alert("hi");
}
The code in addlinks in is getting executed but there nothing appears in the column. I tried using custom formatting also but I couldnot show custom text "Edit" and link click doesnot fire the js method.
formatter
? if so can you share them or provide the demo... – ExhibitbeforeSelectRow
callback instead ofonclick
attribute to execute any JavaScript code on click on the link/button... – LainebeforeSelectRow
callback have the second parametere
which provide you information about the clicked element.$(e.target).closest("td")
will be the clicked cell,$(e.target).closest("tr")
will be the clicked row,$(e.target).closest("td").attr("id")
will be the rowid and so on.var iCol = $.jgrid.getCellIndex($(e.target).closest("td")[0]);
is the column number of the clicked cell. In any way I strictly recommend you to usegridview: true
option (see the answer) to improve performance. Usage ofaddlinks
makes grid slow. – Laine