kendo-gridkendo-template

Conditionally formatting kendo grid column issue with if statement in template


I am running into an issue trying to add a percentage sign to my value in one of my columns in the kendo grid using a template, what I am using is..

template: "#if(Markup != null){ #=kendo.format('{0:p}', Markup / 100)# }#"

So if the Markup value is not null then I want it to show the percentage sign, but when I run the grid all I get returning is my column is

=kendo.format('{0:p}', Markup / 100)

Solution

  • In this case, I like to use template as a function:

    template: function(item) {
        if(item.markup) {
            return kendo.format('{0:p0}', item.markup / 100);
        }
    
        return item.name;
    }
    

    Simple example: template as a function

    Or you can use it your way:

    template: "#= data.markup ? kendo.format('{0:p}', data.markup / 100): 'N/A' #"
    

    Dojo: inline template

    NOTE:

    "#if(Markup != null){ #=kendo.format('{0:p}', Markup / 100)# }#"
                           \--- this hash closes script, 
                                everithing after that is string.
                                Thats why you see
                                =kendo.format('{0:p}', Markup / 100) in grid