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

Conditionals embedded in client templates

3 Answers 499 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Robert
Top achievements
Rank 1
Robert asked on 03 Apr 2013, 02:52 PM
I have model class like so:
public class ServerModel
{
    public Guid Id { get; set; }
    public CustomerModel Customer { get; set; }
    public string Name { get; set; }
}

Inside a Kendo Grid, I'd like to display a link to the Customer, but as the Customer can be null, I'm having trouble getting this link to only show when the customer is not null. Below is the client template I'm trying to use:
@(Html.Kendo().Grid(Model)
      .Name("Grid")
      .Columns(columns =>
      {
          columns.Bound(s => s.Name)
                 .ClientTemplate("<a href='" + Url.Action("Details", "Server") + "/#= ID #'>#= Name #</a>");
          columns.Bound(s => s.Hostname);
          columns.Bound(s => s.Customer.Name).Title("Customer")
                 .ClientTemplate("#= Customer ? '<a href=\'" + Url.Action("Details", "Customer") + "/#= ID #\'>#= Customer.Name #</a>' : 'Unknown'#");
          columns.Bound(s => s.Reported).Title("Last Reported (UTC)").Format("{0:yyyy-MM-dd hh:mm:ss tt}");
      })

This just gives me a template error, though.

3 Answers, 1 is accepted

Sort by
0
Dimiter Madjarov
Telerik team
answered on 04 Apr 2013, 03:05 PM
Hello Robert,


To include the check for the null value in your ClientTemplate, you could modify it like this
E.g.
columns.Bound(p => p.ProductName).ClientTemplate("#if(Name != null){#" +
           "<a href='" + Url.Action("Details", "Server") + "/#=ID#'>#=Name#</a>" +
           "#} else {#" +
           "another template" +
           "# } #");

 

Greetings,
Dimiter Madjarov
the Telerik team
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
Nathan
Top achievements
Rank 1
answered on 07 Apr 2014, 01:57 PM
How would I attempt to do this if I'm building the grid on the server (using Razor) and doing a footer template?  Sometimes the values may be null.  So if it's null, I want to add a 0 or string.empty

columns.Bound(c => c.Flowtime).Title("Time").Format(numericalformat).FooterTemplate(@<text>@item.Sum.Format(numericalformat)</text>);
.....
agg.Add(p => p.Flowtime).Sum();
0
Dimiter Madjarov
Telerik team
answered on 08 Apr 2014, 02:48 PM
Hi Nathan,

I see that my colleague Alexander covered the issue in the support ticket on the same topic. Do not hesitate to contact us again if further problems arise.


Regards,
Dimiter Madjarov
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
Tags
General Discussions
Asked by
Robert
Top achievements
Rank 1
Answers by
Dimiter Madjarov
Telerik team
Nathan
Top achievements
Rank 1
Share this question
or