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?
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?
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
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?
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