0

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.

Jess Stone
  • 677
  • 8
  • 21
  • `$grid->setColModel()` gives no information. In the same way you could post: "some code...". Which format exactly have input data which you use? You can use [Fiddler](http://fiddler2.com/get-fiddler) or developer Tools of IE to trace HTTP traffic. You should include as example one row of input data. Do you use `loadonce: true` option or not? Do you use `jsonmap` in the column or `jsonReader` option in jqGrid? How exactly the column are defined in `colModel`? You can open source code in web browser to see which code are executed. – Oleg Dec 17 '13 at 12:38
  • @Oleg I have edited my question according to your request. hope you can get more valuable info now. – Jess Stone Dec 17 '13 at 13:38
  • In grid.php I am not using `loadonce: true` , and i just have this line regarding colmodel: `$grid->setColModel()` – Jess Stone Dec 17 '13 at 13:43

1 Answers1

2

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> ";
}
Oleg
  • 220,925
  • 34
  • 403
  • 798
  • Yes, `rowObject.pdf_1` works. Actually, I am using the same function `formatPdfLink` for 4 columns. So i need to dynamically get the column name and use it in the `formatPdfLink` function. :) – Jess Stone Dec 17 '13 at 13:51
  • I am really sorry, but I did not understand how to get Column name in `formatPdfLink`. I don't know the column name: i need to know it dynamically in the function – Jess Stone Dec 17 '13 at 14:25
  • What I am looking for is something similar to this (I will use it in my custom-format function): `var colName = "Column name = " + rowObject.columnName; ` . but `rowObject.columnName` doesn't exist.. I am wondering what the right syntax is... – Jess Stone Dec 17 '13 at 14:32
  • Something similar to [this](http://stackoverflow.com/a/9384424/3048495), which unfortunately is not working for me – Jess Stone Dec 17 '13 at 14:37
  • @JessStone: Sorry, but I don't understand what you mean. What you mean under "`rowObject.columnName` doesn't exist"? `columnName` or `columnid` are names of the columns. If you have JavaScript variable `columName` (like `var columnName = "pdf_1";`) then you should use `[]` syntax: `rowObject[columnName]`. – Oleg Dec 17 '13 at 14:40
  • [this solution](http://stackoverflow.com/a/9384424/3048495) would be perfect, but it doesn't work for me. what do you think of it? – Jess Stone Dec 17 '13 at 14:45
  • @JessStone: probaly it would be easy for you to use `formatter: "dynamicLink"` instead of writing custom formatter. You can download `jQuery.jqGrid.dynamicLink.js` from [here](https://github.com/OlegKi/jqGrid-plugins). It's very simple to use and very flexible. See [the answer](http://stackoverflow.com/a/9048483/315935) for example for the code example. – Oleg Dec 17 '13 at 14:46
  • @JessStone: What exactly "not work"? You don't posted till now the code of `formatPdfLink` or `unformatPdfLink` which you use. Where is *your code*? – Oleg Dec 17 '13 at 14:47
  • I added this code to my question above, as well: `function formatPdfLink(cellValue, options, rowObject) { var cellHtml = " "; return cellHtml; }` – Jess Stone Dec 17 '13 at 14:52
  • @JessStone: Is the column with the name `columnName` defined in `colModel` like the column `pdf_1`? – Oleg Dec 17 '13 at 15:00
  • `columnName` is something i have seen for the first time [here](http://stackoverflow.com/a/9384424/3048495). I do not define any colModel. I do not set up anything. – Jess Stone Dec 17 '13 at 15:02
  • `function formatPdfLink(cellValue, options, rowObject) { var cellHtml = " "; return cellHtml; }` – Jess Stone Dec 17 '13 at 15:07
  • @JessStone: You *do* define columns in `colModel` either directly on indirectly. You can open the source code of your page to see JavaScript code which really work. [The page](http://www.trirand.net/documentation/php/_2v70w0lkn.htm) of documantation describe that you can use `getColModel` to get the columns or you can use `setColModel` with parameters (see [here](http://www.trirand.net/forum/default.aspx?g=posts&m=4193)). – Oleg Dec 17 '13 at 15:08
  • 1
    @JessStone: Which *column* you need here: `[show column Name here]`? If the *the same* column where `formatPdfLink` are used or *another* column? To get name **the same column** you can use `options` parameter which have properties `rowId`, `colModel`, `gid` and `pos`. For example try `options.colModel.name` – Oleg Dec 17 '13 at 15:10
  • `options.colModel.name` is what I was looking for !! Oleg, you are a **Genius** !!! PS: how could I have find it out (on myself) in the Documentation or by Google? – Jess Stone Dec 17 '13 at 15:22
  • @JessStone: You are welcome! jqGrid is open source solution which mostly recent full code you can find of [github](https://github.com/tonytomov/jqGrid/tree/master). You can analyse the source code to find all details. Moreover you can debug the JavaScript code. If you set breakpoint inside of custom formatter you can see in debugger all properties of all parameters you can look in "call stack" to see from which place of jqGrid code the formatter was called you can go some lines before to see how the input parameters were initialized. – Oleg Dec 18 '13 at 09:27
  • yes, the answer to my question is: `options.colModel.name`. I would be happy if you could add it to your answer, in order to help other people find the solution quickly when and if needed :) – Jess Stone Dec 18 '13 at 09:33
  • @JessStone: I agree. I posted **UPDATED** part to my answer. – Oleg Dec 18 '13 at 09:52