javascriptjqgridmvcjqgrid

jqGrid create a column conditionally


I've got a working jqGrid to which I need to add a column based on the result of a javascript function. The column will just include an href link directing to another page.

Is it possible to add a column this way when creating the jqGrid or would I have to create a whole new grid with the new column if the function evaluates to true?

I've tried...

$('grid').jqGrid({
    ...
    colNames: [..., (myFunction() == 'Value') ? 'Test' : null ],
    colModel: [..., (myFunction() == 'Value') ?
        { key: true, name: 'Test', formatter: myFormatter, editable: false, align: 'center' } : null ],
    ...

but to no avail.

Would appreciate any help, thanks!


Solution

  • Got it.

    $('grid').jqGrid({
        ...
        colNames: [..., (myFunction() == 'Value') ? 'Test' : "" ],
        colModel: [..., (myFunction() == 'Value') ?
            { key: true, name: 'Test', formatter: myFormatter, editable: false, align: 'center' } : { hidden: true } ],
        ...
    

    The nulls were causing the issue. Just added the column and set it to hidden if the condition returned from the function did not match the specified criteria.

    Hope this helps someone else. Cheers!