I have more than one jqGrids
on my page. These were created dynamically. I want to get the caption of the grid from row click. I see setCaption
method but no getCaption. So was wondering if there is a good way to get this value?
I didn't find any helpful link here in SO or outside. Appreciate any help.
UPDATE:
@Oleg's solution helped me to fix the issue, his comment on how to find the grid id made me re-think about my problem and got the solution:
zLoadLabResult()
generates grid dynamically. And when user clicks on the checkbox, I wanted to know the caption of the grid for some other logic that i am building.
function zLoadLabResults(fulldata) {
fulldata = $.parseJSON(fulldata);
$.each(fulldata, function (i, item) {
var data = item.resultData;
var colNames = item.colNames;
var colModelsArray = [];
for (var i = 0; i < colNames.length; i++) {
var str;
if (i === 0) {
str = {
name: 'id',
width: 25,
editoptions: { value: "True:False" },
editrules: { required: true },
edittype: 'checkbox',
formatter: zCheckboxFormatter,
formatoptions: { disabled: false },
editable: true,
sortable: false
};
} else {
str = {
name: colNames[i],
index: colNames[i],
width: '100px'
};
}
colModelsArray.push(str);
}
var gridName = "grid-" + item.testName;
$("#gridcontainer").append("<table id='grid-" + item.testName + "' class='grid'></table>");
$("#" + gridName).jqGrid({
//url: "user.json",
//datatype: "json",
datatype: "local",
colNames: colNames,
colModel: colModelsArray,
height: "auto",
width: "auto",
caption: item.testName
});
var names = colNames;
var mydata = [];
for (var i = 0; i < data.length; i++) {
mydata[i] = {};
for (var j = 0; j < data[i].length; j++) {
mydata[i][names[j]] = data[i][j];
}
}
for (var i = 0; i <= mydata.length; i++) {
$("#" + gridName).jqGrid('addRowData', i + 1, mydata[i]);
}
for (var i = 0; i <= colNames.length; i++) {
$("#" + gridName).jqGrid("setLabel", colNames[i], "", { "text-align": "left" });
}
});
};
function zCheckboxFormatter(cellvalue, options, rowObject) {
return '<input type="checkbox" class="selectedId" name="Id-' + cellvalue + '">';
};
I have the following event hooked to checkbox that does the trick by adding Oleg's solution:
$('table[id^="grid-"]').on("click", "td > input", function (e) {
var grid = "#" + $(e.target).closest("table").attr("id");
var caption = $(grid).jqGrid("getGridParam", "caption");
var checked = $(e.target).is(":checked")
var rowId = $(e.target).closest("tr").attr("id")
rowData = $(grid).getRowData(rowId);
});