Conditional template with ternary operator

2 posts, 0 answers
  1. Labhesh
    Labhesh avatar
    26 posts
    Member since:
    Sep 2015

    Posted 31 May 2016 Link to this post



    I am trying to create a template for grid in kendo-ui. Its formatting is 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


  2. Dimiter Madjarov
    Dimiter Madjarov avatar
    2312 posts

    Posted 01 Jun 2016 Link to this post

    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.

    Dimiter Madjarov
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
Back to Top