JqSuite PHP: get column name or id?
Asked Answered
M

1

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.

Margo answered 17/12, 2013 at 12:23 Comment(3)
$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 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.Liquescent
@Liquescent I have edited my question according to your request. hope you can get more valuable info now.Margo
In grid.php I am not using loadonce: true , and i just have this line regarding colmodel: $grid->setColModel()Margo
L
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> ";
}
Liquescent answered 17/12, 2013 at 13:44 Comment(18)
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. :)Margo
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 functionMargo
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...Margo
Something similar to this, which unfortunately is not working for meMargo
@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].Liquescent
this solution would be perfect, but it doesn't work for me. what do you think of it?Margo
@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. It's very simple to use and very flexible. See the answer for example for the code example.Liquescent
@JessStone: What exactly "not work"? You don't posted till now the code of formatPdfLink or unformatPdfLink which you use. Where is your code?Liquescent
I added this code to my question above, as well: function formatPdfLink(cellValue, options, rowObject) { var cellHtml = "<a href='" + cellValue + "' title='" + rowObject.columnName + "' ><img src='../img/PDF_icon.png ' /></a> "; return cellHtml; }Margo
@JessStone: Is the column with the name columnName defined in colModel like the column pdf_1?Liquescent
columnName is something i have seen for the first time here. I do not define any colModel. I do not set up anything.Margo
function formatPdfLink(cellValue, options, rowObject) { var cellHtml = "<a href='" + cellValue + "' title='" + [show column Name here] + "' ><img src='../img/PDF_icon.png ' /></a> "; return cellHtml; }Margo
@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 of documantation describe that you can use getColModel to get the columns or you can use setColModel with parameters (see here).Liquescent
@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.nameLiquescent
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?Margo
@JessStone: You are welcome! jqGrid is open source solution which mostly recent full code you can find of github. 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.Liquescent
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 :)Margo
@JessStone: I agree. I posted UPDATED part to my answer.Liquescent

© 2022 - 2024 — McMap. All rights reserved.