Следуя этому примеру из документации Kendo UI
Я пытаюсь воссоздать это, чтобы сгенерировать динамическую сетку c. Однако, когда я удаляю столбец даты из объекта JSON, сетка не генерируется.
Почему это происходит? и как я могу обойти эту проблему?
Это блок кода в вопросе:
<div id="grid"></div>
<script>
//example data received from remote source via jQuery ajax merthod
var data = [{
"Name": "John",
"Role": "Developer",
"Dept": "Dev",
"Date": "\/Date(836438400000)\/",
"Balance": 23
}, {
"Name": "Jane",
"Role": "Developer",
"Dept": "Dev",
"Date": "\/Date(836438400000)\/",
"Balance": 23
}, {
"Name": "James",
"Role": "QA",
"Dept": "Dev",
"Date": "\/Date(836438400000)\/",
"Balance": 23
}, {
"Name": "Jimmy",
"Role": "Designer",
"Dept": "Dev",
"Date": "\/Date(836438400000)\/",
"Balance": 23
}];
//in the success handler of the AJAX method call the function below with the received data:
var dateFields = [];
generateGrid(data);
function generateGrid(gridData) {
var model = generateModel(gridData[0]);
var parseFunction;
if (dateFields.length > 0) {
parseFunction = function (response) {
for (var i = 0; i < response.length; i++) {
for (var fieldIndex = 0; fieldIndex < dateFields.length; fieldIndex++) {
var record = response[i];
record[dateFields[fieldIndex]] = kendo.parseDate(record[dateFields[fieldIndex]]);
}
}
return response;
};
}
var grid = $("#grid").kendoGrid({
dataSource: {
data: gridData,
schema: {
model: model,
parse: parseFunction
}
},
editable: true,
sortable: true
});
}
function generateModel(gridData) {
var model = {};
model.id = "ID";
var fields = {};
for (var property in gridData) {
var propType = typeof gridData[property];
if (propType == "number") {
fields[property] = {
type: "number",
validation: {
required: true
}
};
} else if (propType == "boolean") {
fields[property] = {
type: "boolean",
validation: {
required: true
}
};
} else if (propType == "string") {
var parsedDate = kendo.parseDate(gridData[property]);
if (parsedDate) {
fields[property] = {
type: "date",
validation: {
required: true
}
};
dateFields.push(property);
} else {
fields[property] = {
validation: {
required: true
}
};
}
} else {
fields[property] = {
validation: {
required: true
}
};
}
}
model.fields = fields;
return model;
}
</script>
Пример данных
var data = [{
"Name": "String",
"Role": "String",
"Dept": "String",
"Date": "\/Date(836438400000)\/",
"Balance": "String"
}, {
"Name": "String",
"Role": "String",
"Dept": "String",
"Date": "\/Date(836438400000)\/",
"Balance": "String"
}];