Kendo 网格 Json 数据 Header 和内容
Kendo Grid Json Data for Header and Content
我正在处理 Asp.Net MVC 5 项目,我需要用 Kendo 网格 Jquery.
更改表格
目前我从 Api 控制器返回数据,如下所示:
[
headers:{{
"label": "Name",
"id": "name",
"isfixed": true,
"width": "200px"
},
{
"width": "200px",
"isfixed": false,
"id": "city",
"label": "City"
},
},
rows:{
{name:"Joe",city:"Berlin"},
{name:"Doe",city:"London"},
...
}
]
Kendo 好像不太喜欢这种格式。只有当我像这样发送 JSON 时它才有效:
[
{ name: "Joe", City: "Berlin" },
{ name: "Doe", City: "Londo" }
]
问题:
我可以让 Kendo 网格使用第一种类型的数据,因为我需要不同表的动态列和数据吗?
这里详细介绍了如何完成此操作:https://docs.telerik.com/kendo-ui/knowledge-base/create-with-dynamic-columns-and-data-types
完成它的代码:
<div id="grid" style="width:1000px;"></div>
<script>
var isDateField =[];
$.ajax({
url: "https://www.mocky.io/v2/5835e736110000020e0c003c",
dataType: "jsonp",
success: function(result) {
generateGrid(result);
}
});
function generateGrid(response) {
var model = generateModel(response);
var columns = generateColumns(response);
var grid = $("#grid").kendoGrid({
dataSource: {
transport:{
read: function(options){
options.success(response.data);
}
},
pageSize: 5,
schema: {
model: model
}
},
columns: columns,
pageable: true,
editable:true
});
}
function generateColumns(response){
var columnNames = response["columns"];
return columnNames.map(function(name){
return { field: name, format: (isDateField[name] ? "{0:D}" : "") };
})
}
function generateModel(response) {
var sampleDataItem = response["data"][0];
var model = {};
var fields = {};
for (var property in sampleDataItem) {
if(property.indexOf("ID") !== -1){
model["id"] = property;
}
var propType = typeof sampleDataItem[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(sampleDataItem[property]);
if (parsedDate) {
fields[property] = {
type: "date",
validation: {
required: true
}
};
isDateField[property] = true;
} else {
fields[property] = {
validation: {
required: true
}
};
}
} else {
fields[property] = {
validation: {
required: true
}
};
}
}
model.fields = fields;
return model;
}
</script>
我正在处理 Asp.Net MVC 5 项目,我需要用 Kendo 网格 Jquery.
更改表格目前我从 Api 控制器返回数据,如下所示:
[
headers:{{
"label": "Name",
"id": "name",
"isfixed": true,
"width": "200px"
},
{
"width": "200px",
"isfixed": false,
"id": "city",
"label": "City"
},
},
rows:{
{name:"Joe",city:"Berlin"},
{name:"Doe",city:"London"},
...
}
]
Kendo 好像不太喜欢这种格式。只有当我像这样发送 JSON 时它才有效:
[
{ name: "Joe", City: "Berlin" },
{ name: "Doe", City: "Londo" }
]
问题: 我可以让 Kendo 网格使用第一种类型的数据,因为我需要不同表的动态列和数据吗?
这里详细介绍了如何完成此操作:https://docs.telerik.com/kendo-ui/knowledge-base/create-with-dynamic-columns-and-data-types
完成它的代码:
<div id="grid" style="width:1000px;"></div>
<script>
var isDateField =[];
$.ajax({
url: "https://www.mocky.io/v2/5835e736110000020e0c003c",
dataType: "jsonp",
success: function(result) {
generateGrid(result);
}
});
function generateGrid(response) {
var model = generateModel(response);
var columns = generateColumns(response);
var grid = $("#grid").kendoGrid({
dataSource: {
transport:{
read: function(options){
options.success(response.data);
}
},
pageSize: 5,
schema: {
model: model
}
},
columns: columns,
pageable: true,
editable:true
});
}
function generateColumns(response){
var columnNames = response["columns"];
return columnNames.map(function(name){
return { field: name, format: (isDateField[name] ? "{0:D}" : "") };
})
}
function generateModel(response) {
var sampleDataItem = response["data"][0];
var model = {};
var fields = {};
for (var property in sampleDataItem) {
if(property.indexOf("ID") !== -1){
model["id"] = property;
}
var propType = typeof sampleDataItem[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(sampleDataItem[property]);
if (parsedDate) {
fields[property] = {
type: "date",
validation: {
required: true
}
};
isDateField[property] = true;
} else {
fields[property] = {
validation: {
required: true
}
};
}
} else {
fields[property] = {
validation: {
required: true
}
};
}
}
model.fields = fields;
return model;
}
</script>