Pass variable from kendo datasource to template

2019-09-09 09:49发布

问题:

I am trying to pass a string from the kendo datasource, into a template within the grid, that is acting as the Print button. The button fires a function that opens a new link to the report server, and I need to pass a string value into the function, so that string value is then sent in the url.

I have it working if I send the datasource Id field, but if I try to send a string value (Physician), I get an 'Unexpected identifier' error. I have tried changing the coluns: field: to Physician, but get the same error.

EDIT: I can pass any int value, but it breaks if I try to send a string.

How do I pass a value other than the Id to my template?

    schema: {
        model: {
            id: "Id",
            fields: {
                "ClinicName": { type: "string" },
                "Physician": { type: "string" },
                "Phone": { type: "string" },
                "Fax": { type: "string" },
                "Specialty": { type: "string" },
                "Consent": { type: "date" }
            }
        }
    },
    pageSize: 10
});


function printForm(Physician) {
    var stuid = $('#sisid').html().match(/\d+/);
    var user = $('#user').val();
    var sid = $('#sess').val();
    window.open("http://@reportServer/ReportServer/Pages/ReportViewer.aspx?/SHPN/Treatment%20Form&rs:Command=Render&StudentId=" + stuid + "&SessionId=" + sid + "&CurrentUser=" + user + "&Physician=" + Physician);
};

$(document).ready(function () {

        columns: [
            {
                field: "Id",
                width: "38px",
                title: "Print",
                filterable: false,
                sortable: false,
                template: "<a class='change-image' href='javascript:void(0);' title='Print Treatment Form' onclick='printForm(#= Id #)'><img alt='Student Info' src='@Url.Content("~/Content/Images/printer-icon.png")' /></a>"
            },

回答1:

Since Physician is a string you are probably not correctly escaping it. Try defining the template as:

template: "<a class='change-image' href='javascript:void(0);' title='Print Treatment Form' onclick='printForm(\"#= Physician #\")'><img alt='Student Info' src='@Url.Content("~/Content/Images/printer-icon.png")' /></a>"

Check it here: http://jsfiddle.net/OnaBai/ZwXa2/



回答2:

columns.Bound(m => m.Name).Title("Subsys #").ClientTemplate("<a href='javascript: void(0);' onclick=\"return str('ImAString');\">#= Name #</a>");

This is razor syntax, you can insert some arbitrary string like this 'stringValue'