Not understanding this kendo template that generates a checkbox in a grid

后端 未结 1 518

I\'m not understanding something basic about Kendo templates, so maybe someone can explain this to me. This example of a template for a cell in a grid comes from Telerik s

相关标签:
1条回答
  • 2021-01-20 05:41

    It's JavaScript. Using this template string:

    "<input type='checkbox' #= isAdmin ? checked='checked' : '' # />"
    

    Template.compile will generate a function like this one:

    function anonymous(data) {
        var o, e = kendo.htmlEncode;
        with(data) {
            o = '<input type=\'checkbox\' ' + (isAdmin ? checked = 'checked' : '') + ' />';
        }
        return o;
    }
    

    As you can see, your template section is used without changes. When your cell is rendered, this function is executed (your data item, e.g. the grid's row model, is passed in) and the resulting string is used as content for the cell.

    From the perspective of the rendered HTML, this here:

    "<input type='checkbox' #= isAdmin ? checked='checked' : '' # />"
    

    is equivalent to:

    "<input type='checkbox' #= isAdmin ? 'checked' : '' # />"
    

    since checked='checked' will simply evaluate to 'checked'. Using the first variant however will also set the checked attribute on the data which is passed into the template function (see demo for illustration).

    0 讨论(0)
提交回复
热议问题