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

[Solved] conditianally showing content of a ClientTemplate

6 Answers 297 Views
Grid
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Ronald
Top achievements
Rank 1
Ronald asked on 07 Oct 2010, 11:00 AM
I'm trying to display or hide the content of a column with a ClientTemplate depending on the conten of it, but I can't get it to work.  Is there any way to do this?

I have the folowwing:

public class Person
{
    public Guid Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Function { get; set; }
    public bool ShowFunction { get; set; }
}

and

<%= Html.Telerik().Grid<Person>()
    .Name("Employees")
    .DataBinding(dataBinding => dataBinding.Ajax().Select("Employees", "Persons"))
    .Columns(columns =>
    {
        columns.Bound(e => e.Id);
    })
    .DetailView(dv => dv.ClientTemplate(
        "<#= LastName #>, <#= FirstName #> (<#= Function #>)"
    ))
%>

I want the (<# Function #>) part only to be shown when ShowFunction is true, so I tried something like the following, in al sorts of variations:

"<#= LastName #>, <#= FirstName #> <# if (ShowFunction) { #>(<#= Function #>)<# } #>"

but I can't get it to work :-(

Anyone any suggestions?

6 Answers, 1 is accepted

Sort by
0
Ronald
Top achievements
Rank 1
answered on 07 Oct 2010, 03:03 PM
Conditionally showing content is no problem, my problem was something else.

The problem that I was having, was because I created the ClientTemplate with String.Format() and a Html.ActionLink() statement.  I was the Id property to the RouteValues, but this caused errors when it contained a hash symbol (like "<#= Id #>").

I created a workaround for this with some javascript, so my problems are gone now.

The strange thing is that this doesn't give any problem when I remove the conditional statement from the string format.
0
Wai Kei
Top achievements
Rank 1
answered on 01 Dec 2010, 03:57 PM
I would also like to put the conditional statements inside a clientTemplate as well. Does anyone know how to perform a condition check based on another field perhaps?

Thank you.
0
Atanas Korchev
Telerik team
answered on 01 Dec 2010, 05:01 PM
Hi Wai Kei,

 You can use if statements:

ClientTemplate("<# if (CustomerID > 0) { #><#= CustomerID #> <# } #> ");

Client side expressions are very similar to server one:

<# is the same as <% whereas <#= is the same as <%=

Regards,
Atanas Korchev
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Rick
Top achievements
Rank 1
answered on 18 Dec 2010, 02:27 AM
Where are these client-side expressions documented?

Is it essentially just any JavaScript goes in between the <# #> delimeters?
0
Rick
Top achievements
Rank 1
answered on 20 Dec 2010, 08:55 PM
Never mind.  I answered my own question about the client-side expressions.  It's fairly straight-forward once you start playing with it.

This is a powerful feature, and I commend  you for having it.  You should promote it more though.
0
piers
Top achievements
Rank 1
answered on 08 Apr 2011, 11:41 AM
I tried the conditional logic syntax, but as I was determining whether or not to produce an actionlink, it all went horribly wrong.

My solution was to apply my conditional value as the name of a class, then have a rowbound function with some jquery magic, in this case to replace the <a>s with <div>s.

columns.Bound(o => o.Id).ClientTemplate(Html.ActionLink("<#=OrderId#>", "Index", new { controller = "Orders", OrderId = "<#=Id#>" }, new { @class = "<#=HasData#>" }).ToHtmlString());
...snip...
.ClientEvents(events => events.OnRowDataBound("missingList_onRowDataBound"))
...snip...

function missingList_onRowDataBound(e) {
             $(".false").each(function (a) {
                 $(this).replaceWith('<div>' + $(this).text() + '</div>');
             });
         }
Tags
Grid
Asked by
Ronald
Top achievements
Rank 1
Answers by
Ronald
Top achievements
Rank 1
Wai Kei
Top achievements
Rank 1
Atanas Korchev
Telerik team
Rick
Top achievements
Rank 1
piers
Top achievements
Rank 1
Share this question
or