-1

I have written a code that uses a custom formatter for a jqgrid column . The formatter is using callabck. When trying to assign the value of the this function to the formatter,I am getting undefined value whereas when putting alert I am getting correct value. Here is my code.Any help shall be appreciated.

var str='';
function myFormatter(cellvalue, options, rowObject, callback) {

var loadUrl= 'someUrl';


    $.getJSON(loadUrl, function(jsnResponse) {

        if (typeof (callback) === "function") {
            alert('within callback ' + jsnResponse.logo);
            str= '<img width="20"  src=' + jsnResponse.logo + '  />';
            callback(str);
        }

    });

}

For calling this function and assigning to jqgrid formatter I am using the following code

formatter :  function(cellvalue, options, rowObject){
                                    return myFormatter
                                    (cellvalue, options, rowObject, function(str) {
                                        alert('in formatter '+str);


                                });

Thanks

1 Answer 1

0

I think that there is a misunderstanding what is custom formatter and how it will be used. Custom formatter is already callback function which you have to define if you want use the functionality. jqGrid calls your callback if the property formatter in defined for some column and its value is a function. Because jqGrid makes the call, jqGrid specify parameters which it uses.

On the other side the second parameter (options parameter) of custom formatter contains some properties which can be helpful for you. For example colModel property (options.colModel) is the element of colModel where custom formatter are used. The following code shows what I mean

function myFormatter(cellvalue, options, rowObject) {
    var myOption1 = options.colModel.formatoptions.myOption1;
    ...
}

...

colModel: [
    { name: "col1", formatoptions: { myOption1: "abc" }, formatter: myFormatter },
    { name: "col2", formatoptions: { myOption1: "xyz" }, formatter: myFormatter },
    ...
]

In the above way the same function myFormatter will be called with different myOption1 option ("abc" or "xyz") for different columns. You can define any set of options of any type (inclusive callback functions). If it's required you can access to rowid inside of custom formatter: you can just use options.rowId.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.