简体   繁体   English

构建动态剑道网格时模板无效

[英]Invalid template when constructing Dynamic Kendo Grid

I am constructing a dynamic Kendo UI grid with dynamic columns. 我正在使用动态列构建动态Kendo UI网格。 It fails to render the data to the grid. 它无法将数据呈现给网格。

I am using ASP.NET Core webAPI which returns dynamic columns and its data. 我正在使用ASP.NET Core webAPI,它返回动态列及其数据。

I followed this article . 我跟着这篇文章

On my API call success event, I do the following logic: 在我的API调用成功事件中,我执行以下逻辑:

onSuccess: function (readings) {
    console.log(readings);
    var myCoulmns = [];
    var myFields = {}; // I am not using this
    var model = generateModel(readings[0]);
    readings.forEach(function (obj) {
        for (key in obj) {
            console.log(key + ': ' + obj[key]);
            if (key === "Id") {
                myCoulmns.push({
                    field: key,
                    title: key,
                    width: 240,
                    hidden: true
                });
                myFields["Id"] = { type: "number" };
            }
            else if (key === "RadioChannel") {
                myCoulmns.push( {
                    field: "RadioChannel",
                    title: "Radio Channel",
                    width: 240
                });
                myFields["RadioChannel"]= { type: "string" };
            }
            else if (key === "Units") {
                myCoulmns.push( {
                    field: "Units",
                    title: "Units",
                    width: 150
                });
                myFields["Units"] = { type: "string" };
            } else {
                myCoulmns.push( {
                    field: key,
                    title: getFormatedDate(key),
                    width: 200
                    //format: "{0:n}"
                });
                myFields[key] = { type: "number" };
            }
        }
    });

    $("#monitorReadingGrid").kendoGrid({
        dataSource: {
            schema: {
                model: model
            },
            data: readings,
            pageSize: 20
        },
        columns: myCoulmns,
        height: 550,
        scrollable: true,
        sortable: false,
        filterable: false,
        //columMenu:true,
        pageable: {
            input: true,
            numeric: false
        }
    });
    //$("#montior-data").show();
}

Other functions 其他功能

var isDateField = [];
function getFormatedDate(key) {
    var day = key.substr(0, 2);
    var month = key.substr(2, 3);
    var year = key.substr(5, 4);
    var hour = key.substr(9, 2);
    var minute = key.substr(11, 2);
    var tt = key.substr(13, 2);
    return month + " " + day + ", " + year + " " + hour + ":" + minute + " " + tt;
}

function generateModel(dataItem) {
    var model = {};
    var fields = {};
    for (var property in dataItem) {
        if (property.indexOf("Id") !== -1) {
            model["id"] = property;
        }
        var propType = typeof dataItem[property];

        if (propType === "number") {
            fields[property] = {
                type: "number",
                validation: {
                    required: true
                }
            };
            if (model.id === property) {
                fields[property].editable = false;
                fields[property].validation.required = false;
            }
        } else if (propType === "boolean") {
            fields[property] = {
                type: "boolean"
            };
        } else if (propType === "string") {
            var parsedDate = kendo.parseDate(dataItem[property]);
            if (parsedDate) {
                fields[property] = {
                    type: "date",
                    validation: {
                        required: true
                    }
                };
                isDateField[property] = true;
            } else {
                fields[property] = {
                    type: "string",
                    validation: {
                        required: true
                    }
                };
            }
        } else {
            fields[property] = {
                validation: {
                    required: true
                }
            };
        }
    }

    model.fields = fields;

    return model;
}

Here's the data that is returned from the webAPI: 这是从webAPI返回的数据:

[  
   {  
      "Id":1000,
      "RadioChannel":"0D_A1",
      "Units":"Gs",
      "22May20190522PM":-367.25849333773,
      "22May20190521PM":-367.25849333773,
      "22May20190520PM":-368.24099331991,
      "22May20190519PM":-367.84799332704,
      "22May20190513PM":0.0,
      "22May20190509PM":-367.84799332704,
      "22May20190502PM":-367.45499333417,
      "22May20190501PM":-367.25849333773,
      "22May20190500PM":-366.37424335377,
      "22May20190459PM":-367.94624332526
   },
   {  
      "Id":1010,
      "RadioChannel":"0D_S1",
      "Units":"uStrain",
      "22May20190522PM":-236.57619777032,
      "22May20190521PM":-235.62476990612,
      "22May20190520PM":-235.66136328552,
      "22May20190519PM":-239.68663501865,
      "22May20190513PM":0.0,
      "22May20190509PM":-239.39388798352,
      "22May20190502PM":-237.56421901391,
      "22May20190501PM":-239.0279541896,
      "22May20190500PM":-237.27147197877,
      "22May20190459PM":-237.63740577269
   },
   {  
      "Id":1020,
      "RadioChannel":"Channel 0_Monitoring Temperature",
      "Units":"Degree Farenheit",
      "22May20190522PM":81.0,
      "22May20190521PM":81.0,
      "22May20190520PM":81.0,
      "22May20190519PM":81.0,
      "22May20190509PM":80.0,
      "22May20190502PM":81.0,
      "22May20190501PM":81.0,
      "22May20190500PM":81.0,
      "22May20190459PM":81.0
   },
   {  
      "Id":1030,
      "RadioChannel":"Channel 1_Monitoring Temperature",
      "Units":"Degree Farenheit",
      "22May20190522PM":81.0,
      "22May20190521PM":81.0,
      "22May20190520PM":81.0,
      "22May20190519PM":81.0,
      "22May20190509PM":80.0,
      "22May20190502PM":81.0,
      "22May20190501PM":81.0,
      "22May20190500PM":81.0,
      "22May20190459PM":81.0
   },
   {  
      "Id":1040,
      "RadioChannel":"AFE 0_Model and SN",
      "Units":"General",
      "22May20190522PM":16777219.0,
      "22May20190521PM":16777219.0,
      "22May20190520PM":16777219.0,
      "22May20190519PM":16777219.0,
      "22May20190509PM":16777219.0,
      "22May20190502PM":16777219.0,
      "22May20190501PM":16777219.0,
      "22May20190500PM":16777219.0,
      "22May20190459PM":16777219.0
   },
   {  
      "Id":1050,
      "RadioChannel":"Ambient Temp",
      "Units":"Degree Farenheit",
      "22May20190522PM":79.0,
      "22May20190521PM":79.0,
      "22May20190520PM":79.0,
      "22May20190519PM":79.0,
      "22May20190513PM":0.0,
      "22May20190509PM":79.0,
      "22May20190502PM":79.0,
      "22May20190501PM":79.0,
      "22May20190500PM":79.0,
      "22May20190459PM":79.0
   },
   {  
      "Id":1060,
      "RadioChannel":"Core Temp",
      "Units":"Degree Farenheit",
      "22May20190522PM":79.0,
      "22May20190521PM":79.0,
      "22May20190520PM":79.0,
      "22May20190519PM":79.0,
      "22May20190513PM":0.0,
      "22May20190509PM":79.0,
      "22May20190502PM":79.0,
      "22May20190501PM":79.0,
      "22May20190500PM":79.0,
      "22May20190459PM":79.0
   }
]

I get this error: 我收到此错误:

VM48006:3 Uncaught SyntaxError: Invalid or unexpected token
    at Function (<anonymous>)
    at getter (kendo.all.js:2067)
    at Object.<anonymous> (kendo.all.js:6082)
    at each (jquery.js:360)
    at new init (kendo.all.js:6068)
    at new init (kendo.all.js:6384)
    at Function.re.create (kendo.all.js:7990)
    at init._dataSource (kendo.all.js:60997)
    at new init (kendo.all.js:57304)
    at HTMLDivElement.<anonymous> (kendo.all.js:2449)

If I change data source (commend schema) then I get invalid template error, but I am not using any templates in the grid. 如果我更改数据源(推荐架构)然后我得到无效的模板错误,但我没有在网格中使用任何模板。

dataSource: {
    //schema: {
    //    model: model
    //},
    data: readings,
    pageSize: 20
},

Problem is with your JSON data property names [{"Id":1000,"RadioChannel":"0D_A1","Units":"Gs","22May20190522PM":-367.25849333773,"22May20190521PM":-367.25849333773,"22May20190520PM":-368.24099331991,"22May20190519PM":-367.84799332704,"22May20190513PM":0.0,"22May20190509PM":-367.84799332704,"22May20190502PM":-367.45499333417,"22May20190501PM":-367.25849333773,"22May20190500PM":-366.37424335377,"22May20190459PM":-367.94624332526}] 问题在于您的JSON数据属性名称[{"Id":1000,"RadioChannel":"0D_A1","Units":"Gs","22May20190522PM":-367.25849333773,"22May20190521PM":-367.25849333773,"22May20190520PM":-368.24099331991,"22May20190519PM":-367.84799332704,"22May20190513PM":0.0,"22May20190509PM":-367.84799332704,"22May20190502PM":-367.45499333417,"22May20190501PM":-367.25849333773,"22May20190500PM":-366.37424335377,"22May20190459PM":-367.94624332526}]

if you change your property names to start with string instead of digits, then all your code works. 如果您将属性名称更改为以字符串而不是数字开头,那么您的所有代码都可以正常工作。

22May20190520PM => May2220190520PM

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

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