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

Conditional check of DateTime in x-kendo-template<li>

4 Answers 493 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Joe
Top achievements
Rank 1
Joe asked on 19 Jan 2016, 07:07 PM

Approved is a DateTime in my model. approvedDate is a Datetime variable in my Razor view.

I want to test Approved to see if it is before approvedDate and replace it with a empty string "" if it is inside the x-kendo-template detail-template.

 @{ var approvedDate = DateTime.Parse("01-01-2016"); }

 <script id="detail-template" type="text/x-kendo-template">

    <ul>

        < li>#if (# #= Approved # # < @approvedDate) {# ""#}else{# #= kendo.toString(Approved, "MM-dd-yyyy") # #}#</li>

     </ul>

</script>

 The above syntax causes the detail-template script to fail.

What is the syntax to make this if statement work?

 

 

 

4 Answers, 1 is accepted

Sort by
0
Petyo
Telerik team
answered on 21 Jan 2016, 01:01 PM
Hello,

please check this help article - it includes a througough description of how template works and has several examples, including if/else.

Regards,
Petyo
Telerik
 
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
 
0
Joe
Top achievements
Rank 1
answered on 22 Jan 2016, 05:03 AM

There were two problems, a syntax one and using a Razor DateTime variable in the conditional.

I have both an if else and a ternary syntax working and have converted the Razor variable to a javascript Date.

@{ var approvedDate = "01-01-2015"; }

<script id="tab_template" type="text/kendo-tmpl">
# var approvedDate = new Date(Date.parse(@approvedDate)); #

...

<li><label>Test1: </label>#if (Applied < approvedDate ) {# #: kendo.toString(Applied, "MM-dd-yyyy") # #}else{# Fail #}#</li>
<li><label>Test2: </label>#if (Applied > approvedDate ) {# #: kendo.toString(Applied, "MM-dd-yyyy") # #}else{# Fail #}#</li>
<li><label>Test3:</label>#= Applied < approvedDate ? kendo.toString(Applied, "MM-dd-yyyy") : "Fail" #</li>
<li><label>Test4:</label>#= Applied > approvedDate ? kendo.toString(Applied, "MM-dd-yyyy") : "Fail" #</li>

...

</script>

The following was displayed:

Test1:  Fail           Test2:  08-22-2015            Test3: Fail             Test4: 08-22-2015

 Applied is a DateTime field in the model and its value is 08-22-2015.

0
Petyo
Telerik team
answered on 25 Jan 2016, 09:47 AM
Hi,

most likely, the code generated by the server-side is not valid JavaScript. You may check that by using the web developer tools of your browser.

Regards,
Petyo
Telerik
 
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
 
0
Joe
Top achievements
Rank 1
answered on 25 Jan 2016, 03:30 PM

You misunderstood my last post. 

I said I had it working, explained the problems and showed the working code.

The problem is resolved.

 

Tags
General Discussions
Asked by
Joe
Top achievements
Rank 1
Answers by
Petyo
Telerik team
Joe
Top achievements
Rank 1
Share this question
or