Search code examples
kendo-uikendo-gridkendo-clienttemplate

Concatenate 2 columns in KENDO grid using client template


I have my column bind on the grid as follows:

columns.Bound(p => p.LastName)
     .ClientTemplate($"#=IsSpecialPerson ? 'Special Message' : ''##=LastName#")

And now I have to add middle name with a space to the last name column if exists. I have tried the following, but no luck.

columns.Bound(p => p.LastName)
   .ClientTemplate($"#=IsSpecialPerson ? 'Special Message' : ''##=LastName# ##= MiddleName != null ? MiddleName : ''#") 

I am getting invalid template error on the html.


Solution

  • A quick way to check your template is to count the number of #'s. It should be an even number. If I write your template over separate lines it is easier to see where the problem is:

    #=IsSpecialPerson ? 'Special Message' : ''#
    #=LastName#
    ##= MiddleName != null ? MiddleName : ''#  // You have 3 #'s here
    

    May I suggest this:

    #=IsSpecialPerson ? 'Special Message' : ''#
    #=LastName#
    #if (data.MiddleName != null) {#  // I forget if the data prefix is needed here or not
         
        #=MiddleName#
    #}#
    

    Put that all into one line and it should give you the result you are looking for.