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

Conditional template with ternary operator

1 Answer 2502 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Labhesh
Top achievements
Rank 1
Labhesh asked on 01 Jun 2016, 12:45 AM

Hi,

 

I am trying to create a template for grid in kendo-ui. Its formatting is conditional...as follows:

Dataset sample:

json: [{ "name" :"abc", "link":123 },{ "name" :"def", "link":null}...]

The template should be of the logic:

 

link === null ? <span>name</span> : <a target="_blank" href="http://mywebsite/name">#=name#</a>

i.e. conditionally make the text hyperlinked v/s display it as is ("abc" should be displayed with a hyperlink while "def" should not have a hyperlink).
I am able to get the unconditional way of template working with always make the text as hyperlink as follows:
var nameTemplate = '<a target="_blank" href="http://mywebsite/#=name#">#=name#</a>';

But cannot get the template with the above ternary operator logic to work

Thoughts?
Thanks

1 Answer, 1 is accepted

Sort by
0
Dimiter Madjarov
Telerik team
answered on 01 Jun 2016, 07:01 AM

Hello Labhesh,

I would suggest to use a standard if condition in the template, as it will make it more readable. Here is an example that demonstrates the approach.

Regards,
Dimiter Madjarov
Telerik
 
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
 
Tags
Grid
Asked by
Labhesh
Top achievements
Rank 1
Answers by
Dimiter Madjarov
Telerik team
Share this question
or