This is a migrated thread and some comments may be shown as answers.

if statement in clienttemplate not working

5 Answers 1758 Views
Grid
This is a migrated thread and some comments may be shown as answers.
sjarl
Top achievements
Rank 1
sjarl asked on 16 Mar 2018, 11:09 AM

Hi,

In my clientTemplate I try to do the following:

"<div style=\"display:#if(MimeType == 'application/pdf') {#block#} else {#none#}# \">" +

But when I compare to string it keept giving me an error
"Error: Invalid template:'"

and I treid a lot if different ways to do this.

If I compare to a boolean or integer it goes ok, but for some reason I can't get string compare to work.

Can someone help me out?

thank you.

 

 

 

5 Answers, 1 is accepted

Sort by
0
Accepted
Alex Hajigeorgieva
Telerik team
answered on 19 Mar 2018, 11:58 AM
Hello,

Thank you for the provided code snippet. I used it to attempt to get the same error as you have, however to no avail:

.Columns(columns =>
{
 columns.Bound(p => p.ShipName).ClientTemplate("<div style=\"display:#if(MimeType  == 'application/pdf') {#block#} else {#none#}# \">Test</div>");
})

Here is the resulting HTML:



Something you can try is to pass a JavaScript function instead:

https://docs.telerik.com/aspnet-mvc/helpers/grid/faq#how-to-use-javascript-functions-in-client-column-templates

If this does not help, please provide additional information - the model and grid declarations as well as a sample data item from the response so I can test with your scenario specifics. Look forward to your response.

Kind Regards,
Alex Hajigeorgieva
Progress Telerik
Try our brand new, jQuery-free Angular components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
0
sjarl
Top achievements
Rank 1
answered on 19 Mar 2018, 06:38 PM

Hello Alex,
the Javascript approach works for now.

thank you.

0
Muhammad
Top achievements
Rank 1
answered on 12 Mar 2021, 12:53 PM

 columns.Bound(o => o.RecordingPath).Width(115).HeaderTemplate("Recording").ClientTemplate("<audio controls='controls' onplay='RemoveMarker(<#=ID#>,<#=IsSeen#>)' id='player-<#=ID#>' style='width:100%' ><source src='<#=RecordingPath#>'' type='audio/mp3'></audio>").Sortable(true);

 

 

How i can add condition if RecordingPath is null then show a message no recording 

0
Georgi Denchev
Telerik team
answered on 16 Mar 2021, 09:28 AM

Hi Muhammad,

I'd recommend using a function to return either 'No Record' or the html string to make it a bit clearer:

columns.Bound(p => p.RecordingPath).ClientTemplate("#=renderPlayer(data)#");
<script>
    function renderPlayer(data) {
        if (data.RecordingPath) {
            return `<audio controls='controls' onplay='RemoveMarker(${data.ID}, ${data.IsSeen})'  id='player-<${data.ID}>' style='width:100%' ><source src='<${data.RecordingPath}>' type='audio/mp3'></audio>`;
        } else {
            return 'No recording';
        }
    }
</script>

 

Best Regards,
Georgi Denchev
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

0
Muhammad
Top achievements
Rank 1
answered on 16 Mar 2021, 11:58 AM
Thank you :) 
Tags
Grid
Asked by
sjarl
Top achievements
Rank 1
Answers by
Alex Hajigeorgieva
Telerik team
sjarl
Top achievements
Rank 1
Muhammad
Top achievements
Rank 1
Georgi Denchev
Telerik team
Share this question
or