简体   繁体   English

条件模板kendo ui

[英]conditional template kendo ui

Is it possible to use a row template in a Kendo UI grid only if a specific condition is met (eg a field on that row having a specific value)? 仅当满足特定条件(例如,该行上的字段具有特定值)时,才可以在Kendo UI网格中使用行模板吗? If that condition is not met then it should not render the template and instead just render the row normally. 如果不满足该条件,则不应渲染模板,而应正常渲染该行。

I don't want to specify the condition within the template itself because, unless I'm mistaken, I would also have to include the "default" html within the template definition if the condition is not met. 我不想在模板本身中指定条件,因为除非我误会,否则如果条件不满足,我还必须在模板定义中包括“默认” html。

Here is an example of what I'm trying to achieve, which doesn't work. 这是我要实现的示例,但没有用。 For conciseness I've left out the other grid properties that aren't relevant to my question: 为简洁起见,我省略了与我的问题无关的其他网格属性:

$("#divGrid").kendoGrid({
    rowTemplate: function (data) {
        if (condition) kendo.template($("#myRowTemplate").html(data));
        // else render row without the template, but how?
    }
});

First of all, kendo.template returns a Function which needs to be called (with template data as argument) in order to return HTML code. 首先, kendo.template返回一个函数 ,该函数需要被调用(以模板数据作为参数)才能返回HTML代码。 So, for your example to work it needs to be modified like this: 因此,为使您的示例正常工作,需要进行如下修改:

$("#divGrid").kendoGrid({
    rowTemplate: function (data) {
        if (condition) {
            return kendo.template($("#myRowTemplate").html())(data);
        } // else render row without the template, but how?
    }
});

Now, unfortunately there is is no way to " render the row normally " as you've already specified rowTemplate function. 现在,不幸的是,由于已经指定了rowTemplate函数,因此无法“ 正常渲染行 ”。 You can only specify template (or string) which needs to be displayed in else condition: 您只能指定在其他情况下需要显示的模板(或字符串):

$("#divGrid").kendoGrid({
    rowTemplate: function (data) {
        if (condition) {
            return kendo.template($("#myRowTemplate").html())(data);
        } else {
            return '<tr>Normal row</tr>';
            // or return kendo.template($("#myRowTemplate2").html())(data)
            // or return "<tr>" + data.name + ": " + data.age + "</tr>"
        }
    }
});

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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