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

[Solved] Client Template paradox

2 Answers 153 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.
Bob
Top achievements
Rank 1
Bob asked on 18 May 2012, 11:55 PM
I am trying to get a client template working.

I have the following client template which works:

var printTemplate = "<a href="
                 + Url.Action("Print", new { id = "<#= Key #>" })
                 + "><img alt='Print' src='"
                 + Url.Content("~/Content/images/print.gif" + "'/></a>");

During runtime it evals to:

<a href=/Amsi-v7.5.0/eFinancials/checkruncontrol/Print/%3C%23%3d%20Key%20%23%3E><img alt='Print' src='/Amsi-v7.5.0/Content/images/print.gif'/></a>

I want to make it conditionally displayed based on the Status value, so I modified it to:

var printTemplate = "<# Status == \"R\" || Status == \"D\" ? "
                     + "\"<a href="
                     + Url.Action("Print", new { id = "<#= Key #>" })
                     + "><img alt='Print' src='"
                     + Url.Content("~/Content/images/print.gif" + "'/></a>")
                     + "\" : '' #>";

Which evals at runtime to:

<# Status == "R" || Status == "D" ? "<a href=/Amsi-v7.5.0/eFinancials/checkruncontrol/Print/%3C%23%3d%20Key%20%23%3E><img alt='Print' src='/Amsi-v7.5.0/Content/images/print.gif'/></a>" : '' #>


However, I get the following javascript error when I run that.

http://localhost/Amsi-v7.5.0/Scripts/2012.1.214/telerik.grid.min.js
Line 1

missing ; before statement

I can add a semicolon to my expression... which prevents the error...however, it doesn't work. 

I feel I'm so close. If I simplify it and just put in simple strings it displays the correct string. Any help would be appreciated.

thanks,
BOb

2 Answers, 1 is accepted

Sort by
0
Petur Subev
Telerik team
answered on 22 May 2012, 08:44 AM
Hello Bob,

Basically you can use an external function to handle more complicated templates. Here is an example:
column definition :
.ClientTemplate("<#= myfunc(PersonID,Name) #>");
JavaScript function:
function myfunc(PersonID,Name) {
        if (Name=="R"||Name=="D") {
            
            return "<a href='" +   '@Url.Action("print")'+"/" + PersonID +"'>"
                     + "<img alt='Print' src='" + '@(Url.Content("~/Content/images/print.gif"))' + "'/></a>";
        } else {
            return "";
        }
    }
 

Regards,
Petur Subev
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the Telerik Extensions for ASP.MET MVC, subscribe to their blog feed now.
0
Bob
Top achievements
Rank 1
answered on 22 May 2012, 05:37 PM
Thanks Peter...

I assume because you are using an @ in your javascript that you expect that to be in the razor view? We use unubtrusive js so we don't define script in our views.. but all in separate js files.

I ended up changing my syntax a bit to do the following which worked:

var printTemplate = "<# if (Status == \"R\" || Status == \"D\") { #> "
                        + "<a href="
                        + Url.Action("Print", new { id = "<#= Key #>" })
                        + ">"
                        + "<img alt=Print src="
                        + Url.Content("~/Content/images/print.gif" + ">")
                        + "</a>" 
                        + "<# } #>";

It would be really good if the documentation gave some info on how client templates work and what syntax is expected. 

BOb
Tags
Grid
Asked by
Bob
Top achievements
Rank 1
Answers by
Petur Subev
Telerik team
Bob
Top achievements
Rank 1
Share this question
or