Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Specific renderer (currently only supported for jqGrid) not being rendered #95

Closed
webdevilopers opened this issue Apr 28, 2014 · 7 comments

Comments

@webdevilopers
Copy link
Contributor

In my controller I set this rendererParameter:

        $col = new Column\Select('number', 'c');
        $col->setLabel('Number');
        //only jqGrid currently (custom display)
        $col->setRendererParameter('formatter', '
            function (value, options, rowObject) {
                // do something custom
                return "<b><u>" + value + "</u></b>";
            }
        ', 'jqgrid');        
        $grid->addColumn($col);

But it seems like the code is not generated inside the view as long as I don't remove the third parameter 'jqgrid'.

BTW: Is there a recommended way to escape the javascript?
I would recommend to put the javascript functions into the view and simply parse a string as second parameter so that jqgrid can use it to callback my view function.

Unfortunately parsing a string to the second parameter will cause the code to generate a string in double quotes:

{name: "c_number",index: "c_number",label: "Number",width: "5",hidden: false,sortable: true,search: true,formatter: "formatNumber"}],

But to make jqgrid use it as a callback the quotes need to be removed:

{name: "c_number",index: "c_number",label: "Number",width: "5",hidden: false,sortable: true,search: true,formatter: formatNumber}],
@ThaDafinser
Copy link
Owner

This function currently only works with jqgrid...

I use is also with functions:

$colTypeIcon->setRendererParameter('formatter', 'currencyFmatter ', 'jqGrid');

I extra escape it, because i use common formatter functions:

jQuery.extend($.fn.fmatter , {
    currencyFmatter : function(cellvalue, options, rowdata) {
    return "$"+cellvalue;
}
});
jQuery.extend($.fn.fmatter.currencyFmatter , {
    unformat : function(cellvalue, options) {
    return cellvalue.replace("$","");
}
});

https://github.com/ThaDafinser/ZfcDatagrid/blob/master/src/ZfcDatagrid/Renderer/JqGrid/View/Helper/Columns.php#L149-L151

@ThaDafinser
Copy link
Owner

Can u try it with build in function, custom functions (with extend) and normal custom functions without the escape (see link above)

Then we can change it.

@webdevilopers
Copy link
Contributor Author

I will be back next week and then try it. Thanks so far!

@webdevilopers
Copy link
Contributor Author

I started testing. Using the build in function works fine:

$col->setRendererParameter('formatter', "function (value, options, rowObject) { return '<b><u>' + value + '</u></b>'; }");
// or
$col->setRendererParameter('formatter', "function (value, options, rowObject) { return '<b><u>' + value + '</u></b>'; }", 'jqGrid');
// dito
$col->setRendererParameter('formatter', 'function (value, options, rowObject) { return "<b><u>" + value + "</u></b>"; }');
// or
$col->setRendererParameter('formatter', 'function (value, options, rowObject) { return "<b><u>" + value + "</u></b>"; }', 'jqGrid');

custom functions are next.

@webdevilopers
Copy link
Contributor Author

I tried:

$col->setRendererParameter('formatter', 'formatNumber', 'jqGrid');
#$col->setRendererParameter('formatter', "formatNumber", 'jqGrid');
#$col->setRendererParameter('formatter', 'formatNumber');
#$col->setRendererParameter('formatter', "formatNumber");

The JS generated is always:

{name: "c_number",index: "c_number",label: "Number",width: "5",hidden: false,sortable: true,search: true,formatter: "formatNumber"},

I added each formatter functions standalone using inline and / or headScript:

var formatNumber = function (value, options, rowObject) {
    console.log('formatNumber1');
    return '<b><u>' + value + '</u></b>';
}

jQuery.extend($.fn.fmatter , {
    formatNumber : function(cellvalue, options, rowdata) {
    console.log('formatNumber2');
    return "$"+cellvalue;
}
});

Unfortunately their is no formatting and nothing logged into console.

@webdevilopers
Copy link
Contributor Author

I commented out the escape line in https://github.com/ThaDafinser/ZfcDatagrid/blob/master/src/ZfcDatagrid/Renderer/JqGrid/View/Helper/Columns.php#L150-L153:

                } elseif ($key == 'formatter') {
                    if (stripos($value, 'formatter') === false && stripos($value, 'function') === false) {
                        #$value = '"' . $value . '"';
                    }

The JS must not be placed via inline script but headscript!

The formatter is now called successfully!

But you have to use the non-extend code:

var formatNumber = function (value, options, rowObject) {
    console.log('formatNumber1');
    return '<b><u>' + value + '</u></b>';
}

Otherwise you get an error:
ReferenceError: formatNumber is not defined

@ThaDafinser
Copy link
Owner

This issue was moved to zfc-datagrid/zfc-datagrid#7

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants