问题
How can I get column info (name or ID) in my custom-format function?
Some code in grid.php:
$grid->dataType = 'json';
$grid->setColModel();
My custom format function
function formatPdfLink(cellValue, options, rowObject) {
var cellHtml = "<a href='" + cellValue + "' title='" + [show column Name here] + "' ><img src='../img/PDF_icon.png ' /></a> ";
return cellHtml; }
Javascript code excerpts, found in generated page (view source):
jQuery(document).ready(function($) {
jQuery('#grid').jqGrid({
"jsonReader": {
"repeatitems": false,
"subgrid": {
"repeatitems": false
}
},
"xmlReader": {
"repeatitems": false,
"subgrid": {
"repeatitems": false
}
},
"colModel": [{ {
"name": "pdf_1",
"index": "pdf_1",
"sorttype": "string",
"label": "C",
"sortable": false,
"width": 25,
"align": "center",
"search": false,
"formatter": formatPdfLink,
"unformat": unformatPdfLink,
"editoptions": {
"size": 100
},
"editable": true
}
}]
I have tried to use rowObject.columnName
but it won't work!
NB: I am not using loadonce: true
PS: please let me know if more details are needed.
回答1:
Because you use repeatitems: false
format of data then the input data for the grid should be items with named properties which names are the same as the values of name
property in colModel
. So formatPdfLink
function used as formatter
will get third parameter rowObject
in the same simple format as original data. For example rowObject.pdf_1
for example can be used. To access to another column you should just use the value of name
property used in colModel
for the column.
UPDATED: If you use the same custom formatter multiple times you can need to access properties of the current column. The options
parameter will help you here.
function formatPdfLink(cellValue, options, rowObject) {
return "<a href='" + cellValue +
"' title='" + options.colModel.name +
"' ><img src='../img/PDF_icon.png ' /></a> ";
}
The parameter options
contains properties rowId
, colModel
, gid
and pos
. this
inside of custom formatter are initialized to the DOM of the grid so you can use for example $(this).jqGrid("getGridParam", "parameterName")
or just this.p.parameterName
to access to other options of jqGrid. The property colModel
contains the column definition of the current column only and not the full colModel
parameter.
For example you can rewrite the code above to set the next from colNames
instead of name
propertiy in the tooltip:
function formatPdfLink(cellValue, options, rowObject) {
//var colNames = $(this).jqGrid("getGridParam", "colNames");
var colNames = this.p.colNames;
return "<a href='" + cellValue +
"' title='" + colNames[options.pos] +
"' ><img src='../img/PDF_icon.png ' /></a> ";
}
来源:https://stackoverflow.com/questions/20634037/jqsuite-php-get-column-name-or-id