ClientTemplate with Javascript function

1 Answer 5732 Views
Grid
Siobhan McTear
Top achievements
Rank 1
Siobhan McTear asked on 30 Sep 2014, 10:55 AM

I have a Kendo Grid with a column that displays a persons name. I want the person's name to be a link and when clicked calls some javascript passing the name as a parameter. However I receive an error when I attempt to call this function with a string parameter, numeric paramters are ok. The code is as follows:

<div id="grid">
        @(Html.Kendo().Grid(Model.clients)
                .Name("Search_grdResults")
                .Columns(columns =>
                {
                    columns.Bound(client => client.Hcn);
 
                    columns.Bound(client => client.Hcn).ClientTemplate("<a onclick=showTest('#=FullName#')>#=FullName#</a>");
                })
                    .Pageable()
                    .Sortable()
                    .DataSource(dataSource => dataSource
                       .Ajax()
                       .PageSize(5)
                       .Read(read => read.Action("GetClients", "ClientLookup"))
               )
        )
 
    </div>

The javascript is a simple alert just for testing purposes for now:

function showTest(fullname) {
 
        alert(fullname);
 
    }

How can I correct to allow string parameters to be passed to the javascript function?

Siobhan McTear
Top achievements
Rank 1
commented on 01 Oct 2014, 09:17 AM

Appears I need to escape quotation with a \ like below:

columns.Bound(client => client.Hcn).ClientTemplate("<a onclick=\"showTest('#=FullName#')\">#=FullName#</a>");


I would preferably like to pass the model into the javascript function rather than just the property FullName. Is this possible?
Dimiter Madjarov
Telerik team
commented on 01 Oct 2014, 10:08 AM

Hi Siobhan,


Yes, you could achieve this by using the data keyword, which refers to the current dataItem.
E.g.
columns.Bound(client => client.Hcn).ClientTemplate("<a onclick=\"showTest('#=data#')\">#=FullName#</a>");

Regards,
Dimiter Madjarov
Telerik
 
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
 
Siobhan McTear
Top achievements
Rank 1
commented on 01 Oct 2014, 01:39 PM

Using the code above I either get only the string value [object Object] passed through to the javascript function or when I remove the single quotations from the parameter I can an error:
JavaScript critical error in (unknown source location)\n\nSCRIPT1007: Expected ']'

Below are the variations of the code I used:
columns.Bound(client => client.FullName).ClientTemplate("<a onclick=\"showTest('#=data#')\">#=FullName#</a>");
columns.Bound(client => client.FullName).ClientTemplate("<a onclick=\"showTest(#=data#)\">#=FullName#</a>"); //removed single quotations from parameter

Must I do something else before I can pass this as a parameter?
Dimiter Madjarov
Telerik team
commented on 02 Oct 2014, 08:53 AM

Hello Siobhan,


Please excuse me for the inconvenience. A more suitable approach in the current case would be to directly retrieve the item model in the showTest function.
E.g.
columns.Bound(client => client.Hcn).ClientTemplate("<a onclick=\"showTest()\">#=FullName#</a>");
function showTest() {
    var grid = $("#grid").data("kendoGrid");
    var model = grid.dataItem($(event.target).closest("tr"));
}

I hope this information helps.

Regards,
Dimiter Madjarov
Telerik
 
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
 
Siobhan McTear
Top achievements
Rank 1
commented on 02 Oct 2014, 10:38 AM

Using the code above the model variable is always null. Are there other code amendments required? If this is not possible maybe we can pass the index of the row and get the model from that index in the collection.

1 Answer, 1 is accepted

Sort by
0
Siobhan McTear
Top achievements
Rank 1
answered on 02 Oct 2014, 10:48 AM
The issue appears to be with 'event'. I changed to pass in event arguments and then access target from that and it seems to work:
columns.Bound(client => client.Hcn).ClientTemplate("<a onclick=\"showTest(event)\">#=FullName#</a>");
 
 function showTest(e) {
 
        var grid = $("#Search_grdResults").data("kendoGrid");
        var model = grid.dataItem($(e.target).closest("tr"));
Dimiter Madjarov
Telerik team
commented on 02 Oct 2014, 11:17 AM

Hi Siobhan,


Thanks for the update. Let us know if further assistance is required.

Regards,
Dimiter Madjarov
Telerik
 
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
 
Tags
Grid
Asked by
Siobhan McTear
Top achievements
Rank 1
Answers by
Siobhan McTear
Top achievements
Rank 1
Share this question
or