Using field value in column grid template

2 Answers 31 Views
Grid Templates
Alban
Top achievements
Rank 1
Iron
Alban asked on 28 Oct 2021, 02:22 PM

Hi, 

within the columns definition of my grid I want to be able to call a function as the template.

currently I have:

 {field: 'recommended', title: 'Recommended', template: '# if(recommended)' + '{# <span class=\"glyphicon glyphicon-ok\"></span> #}' + 'else{# <span class=\"glyphicon glyphicon-ok\"></span> #}#'},

and I would like to have

 {field: 'recommended', title: 'Recommended', template: generateFlagTemplate(recommendedFieldValue)},

defining the function as

function generateFlagTemplate(field){
    return kendo.template("# if (field)" + "{# <span class=\"glyphicon glyphicon-ok text-success\"></span> #}" + "else {# <span class=\"glyphicon glyphicon-remove text-grey\"></span> #}#");
}

how can I pass the 'recommended' field value to the function?

I would like to have the function because the same template will be used in different places and saves me from having to duplicate my code.

 

thanks

2 Answers, 1 is accepted

Sort by
0
Alban
Top achievements
Rank 1
Iron
answered on 28 Oct 2021, 02:34 PM

I managed to do it like this:


{field: 'watch', title: 'Watch', template: "#= generateFlagTemplate(watch)#"},

function generateFlagTemplate(field){
    if (field)
        return "<span class=\"glyphicon glyphicon-ok text-success\"></span>";
    else
        return "<span class=\"glyphicon glyphicon-remove text-grey\"></span>";
}

0
Neli
Telerik team
answered on 02 Nov 2021, 11:20 AM

Hi Alban,

Indeed the approach described in your second reply is the correct one.

template:'#= generateFlagTemplate(data.recommended) #'

As it could be helpful to the other users in the forum here is a Dojo example where this is demonstrated. 

Regards,
Neli
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Tags
Grid Templates
Asked by
Alban
Top achievements
Rank 1
Iron
Answers by
Alban
Top achievements
Rank 1
Iron
Neli
Telerik team
Share this question
or