In this question Oleg describes how to retrieve row data from the row id of a jqGrid.
In my code I want to show more data about a row in a subGridRowExpanded:
standardGrid.subGridRowExpanded = function (subgridId, rowId) {
var dataFromTheRow = jQuery('#CompanyGrid').jqGrid('getRowData', rowId);
var html = '<span style="padding-left:80px">' + dataFromTheRow.CompanyName + ' ' +
dataFromTheRow.StatusDesc + '</span>';
$("#" + subgridId).html(html);
}
One of my main grid's columns has a custom formatter which returns the data formatted as a link:
var colModel = [
{ name: 'CompanyID', label: 'Id', width: 30, align: "right" },
{ name: 'CompanyName', label: 'Company Name', search: true, formatter: CompanyLink },
...
and
function CompanyLink(cellValue, options, rowdata, action) {
return '<a href="CompanyProfile.aspx?CompanyId=' + rowdata.CompanyID + '">' +
rowdata.CompanyName + '</a>';
}
When I retrieve the data using the aforementioned answer, it shows up in my subgrid formatted, anchor and all. I was hoping that it would return just the raw data.
So, I tried adding an unformat to the col model. Based on the custom formatter code, I added
function CompanyUnLink(cellValue, options) {
return $(cellValue).text();
}
and changed the colModel of the CompanyName row to
{ name: 'CompanyName', label: 'Company Name', search: true, formatter: CompanyLink, unformat: CompanyUnLink },
But the unformat routine passes a cellValue that is not formatted with the anchor.
I changed the unformat to
function CompanyUnLink(cellValue, options) {
// weird, the unformat actually passes in the raw data, and not the decorated one
return cellValue;
}
and it works. But it seems wrong to me. Is there a better or correct way to do this?
Here is a small example of complete code:
<table id="datalist"></table>
<script type="text/javascript">
function CompanyLink(cellValue, options, rowdata, action) {
return '<a href="CompanyProfile.aspx?CompanyId=' + rowdata.CompanyID + '">' + rowdata.CompanyName + '</a>';
}
function CompanyUnLink(cellValue, options, rowdata, action) {
// weird, the unformat actually passes in the raw data, and not the decorated one
return cellValue;
}
$("#datalist").jqGrid({
datatype: 'local',
colModel: [
{ name: 'CompanyID', label: 'Id', width: 30, align: "right" },
{ name: 'CompanyName', label: 'Company Name', search: true, formatter: CompanyLink },
],
caption: "SubGrid Format Test",
subGrid: true,
subGridRowExpanded: function (subgridId, rowId) {
var dataFromTheRow = $('#datalist').jqGrid('getRowData', rowId);
var html = '<span style="padding-left:10px">' + dataFromTheRow.CompanyName + ' ' + dataFromTheRow.Info + '</span>';
$("#" + subgridId).html(html);
}
});
var mydata = [
{ CompanyID: 1, CompanyName: "Company1", Info: "Info on Company 1" },
{ CompanyID: 2, CompanyName: "Company2", Info: "Info on Company 2" },
{ CompanyID: 3, CompanyName: "Company3", Info: "Info on Company 3" },
{ CompanyID: 4, CompanyName: "Company4", Info: "Info on Company 4" },
];
for (var i = 0; i <= mydata.length; i++)
$("#datalist").jqGrid('addRowData', i + 1, mydata[i]);
</script>
The subGrid row shows the CompanyName decorated with an anchor, i.e. it is not the raw row data.
if you change the colModel to call the unformat routine
colModel: [
{ name: 'CompanyID', label: 'Id', width: 30, align: "right" },
{ name: 'CompanyName', label: 'Company Name', search: true, formatter: CompanyLink, unformat: CompanyUnLink },
],
the text is no longer decorated. But the unformat routine's cellValue is passed in undecorated, which strikes me as wrong.