条件模板剑道 UI

conditional template kendo ui

本文关键字:UI 条件      更新时间:2023-09-26

是否只有在满足特定条件(例如,该行上的字段具有特定值)时才可以在剑道UI网格中使用行模板?如果不满足该条件,则它不应呈现模板,而应仅正常呈现行。

不想在模板本身中指定条件,因为除非我弄错了,否则如果不满足条件,我还必须在模板定义中包含"默认"html。

这是我试图实现的目标的一个例子,这是行不通的。为了简洁起见,我省略了与我的问题无关的其他网格属性:

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

首先,kendo.template返回一个函数,需要调用该函数(以模板数据作为参数)才能返回 HTML 代码。因此,为了使您的示例正常工作,需要像这样对其进行修改:

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

现在,不幸的是,没有办法"正常呈现行",因为您已经指定了rowTemplate函数。您只能指定需要在 else 条件下显示的模板(或字符串):

$("#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>"
        }
    }
});

希望这有帮助。