简体   繁体   中英

using kendo.template (external template) in KendoUI MVC Grid ClientTemplate (column)

I want to use an external template in a column's ClientTemplate like this:

@(Html.Kendo().Grid<Project>().Name("Projects").Columns(
    cols =>
    {
        cols.Bound(m => m.LetterNumber);
        cols.Bound(m => m.CityName);
        cols.Bound(m => m.OrganName);
        cols.Bound(m => m.DateString);
        cols.Bound(m => m.EngineersCount);
        cols.Bound(m => m.ExpertTypeString);
        cols.Bound(m=>m.ProjectId).Title("").ClientTemplate("#buttonsTemplate(data)#");
    }).DataSource(
         ds => ds.Ajax().Read("GetList", "Projects"))
)


<script type="kendo/x-template" id="buttonsTempate">
    #switch(State){
    case 0:#
        <a href="@Url.Action("SelectEngineers")?pid=#=ProjectId#">انتخاب مهندسین</a>
    #break;
    case 1:#
        <a href="@Url.Action("Print")?pid=#=ProjectId#">چاپ نامه</a>
        <a href="@Url.Action("SelectEngineers")?pid=#=ProjectId#">انتخاب مهندسین</a>
    #break;
    }#
</script>

<script>
    var buttonsTemplate = kendo.template($("#buttonsTempate").html());
</script>

But I get an empty column.

If I use internal (inline) template, it works fine:

@(Html.Kendo().Grid<Project>().Name("Projects").Columns(
        cols =>
        {
            cols.Bound(m => m.LetterNumber);
            cols.Bound(m => m.CityName);
            cols.Bound(m => m.OrganName);
            cols.Bound(m => m.DateString);
            cols.Bound(m => m.EngineersCount);
            cols.Bound(m => m.ExpertTypeString);
            cols.Bound(m => m.ProjectId).Title("").ClientTemplate("#switch(State){case 0:#"
 + "<a href='" + @Url.Action("SelectEngineers") + "?pid=#=ProjectId#'>انتخاب مهندسین</a>"
 + "#break;case 1:#"
 + "<a href='" + @Url.Action("Print") + "?pid=#=ProjectId#'>چاپ نامه</a>"
 + "<a href='" + @Url.Action("SelectEngineers") + "?pid=#=ProjectId#'>انتخاب مهندسین</a>"
 + "#break;}#");
        }).DataSource(
             ds => ds.Ajax().Read("GetList", "Projects")).DoConfig()
)

But I prefer external template for such a long template. Any idea what is the problem?

The external templates created by kendo.template() , are in fact functions with a data input parameter. So in your template you should prepend all your columns with a data. eg:

<a href="@Url.Action("SelectEngineers")?pid=#=data.ProjectId#">

Also, in the grid options, you should use the #= syntax in order to see the output:

.ClientTemplate("#=buttonsTemplate(data)#")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM