Kendo Grid,根据条件在细节网格中显示不同的模板

时间:2015-02-09 05:21:09

标签: javascript json kendo-ui kendo-grid kendo-datasource

我正在使用kendo网格,它的工作正常。我需要检查网格的每一行,如果它包含特定值,则使用一个模板绑定数据,否则模板将不同。这可能吗 ?这是我的代码

function detailInit(e) {
        var isCreateGrid = true;
        var masterRowId = e.data.uid;
        var data = [];
        $.ajax({
            type: "GET",
            url: 'https://www.domain.com/details?&transactionId=' + e.data.TransactionId,
            contentType: "application/json; charset=utf-8",
            dataType: 'json',
            headers: { 'ccode': compCode },
            cache: false,
            async: false,
            success: function (result) {
                var jsonResult = JSON.parse(result);
                for (var i = 0; i < jsonResult.length; i++) {
                    if (jsonResult[i]["OldValue"] == null || jsonResult[i]["OldValue"] == '')
                        isCreateGrid = true;
                    else {
                        isCreateGrid = false;
                        break;
                    }
                }
                if (isCreateGrid) {
                    for (var i = 0; i < jsonResult.length; i++) {
                        data.push({ FieldUpdated: jsonResult[i]["ChangeColumns"], Value: jsonResult[i]["NewValue"] });
                    }
                } else {
                    for (var i = 0; i < jsonResult.length; i++) {
                        data.push({ FieldUpdated: jsonResult[i]["ChangeColumns"], Was: jsonResult[i]["OldValue"], Now: jsonResult[i]["NewValue"] });
                    }
                }
            }
        });
        var dataSource = new kendo.data.DataSource({ data: data });
        if (data.length == 0) {
            var grid = $("#logs").data("kendoGrid");
            grid.collapseRow("[data-uid='" + masterRowId + "']");
            grid.dataSource.read();
        } else {
            if (isCreateGrid) {
                $("<div/>").appendTo(e.detailCell).kendoGrid({
                    dataSource: dataSource,
                    filter: { field: e.data.Log, operator: "contains", value: 'has created' },
                    columns:
                        [{ field: "FieldUpdated", title: "Field Updated", width: "50px" },
                        { field: "Value", title: "Value", width: "50px" }]
                });
            } else {
                $("<div/>").appendTo(e.detailCell).kendoGrid({
                    dataSource: dataSource,
                    filter: { field: e.data.Log, operator: "contains", value: 'has created' },
                    columns:
                        [{ field: "FieldUpdated", title: "Field Updated", width: "50px" },
                        { field: "Was", title: "Was", width: "50px" },
                        { field: "Now", title: "Now", width: "50px" }]
                });

            }
        }
    }

但是这个isCreateGrid条件似乎没用,因为如果他找到isCreateGrid条件为假的任何行,则所有行都将具有针对错误条件的模板。

1 个答案:

答案 0 :(得分:2)

是的,你可以。请参考下面的代码。

  $(gridId).kendoGrid({
    dataSource: {
        data: datasource
    },
    scrollable: true,
    sortable: true,
    resizable: true,
    columns: [
     { field: "MetricName", title: "Metric", width: "130px" },
     { field: "OnTrack", title: "On Track", template:'#:changeTemplate(OnTrack)#', width: "130px", attributes: { style: "text-align: center !important;" } },
     { field: "CurrentAmount", title: "Current", template: '$ #:parseFloat(CurrentAmount).toFixed(2)#', width: "130px" },
     { field: "RequiredAmount", title: "Required", template: '$ #:parseFloat(RequiredAmount).toFixed(2)#', width: "130px" }
    ]
});

function changeTemplate(value)
{
   Conditions depending on Your Business Logic
if ()
    return "HTML Here";
else
    return "HTML Here";
}