随笔 - 21  文章 - 0 评论 - 5 阅读 - 18294
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

Create Grids with Dynamic Columns and Data Types

Create Grids with Dynamic Columns and Data Types
Environment
Product Progress? Kendo UI? Grid for jQuery
Operating System Windows 10 64bit
Preferred Language JavaScript
Description
In some cases, the server returns a response with different fields and values depending on user input or another external variable. Thus, the developer doesn't know what columns and fields will be available in the Grid, nor what the type of these fields will be.

This article showcases how to dynamically generate the Kendo UI Grid by using the response data and without knowing the names and types of the columns and fields.

Solution
Prefetch the dynamic Grid data by making an ajax request to the server.
Create the dataSource.model by using the first record in the response as a sample.
Create the Grid columns by using the names of the fields returned in the server response.
Generate the Grid by using the model and columns that were created in the previous two steps.

<div id="grid" style="width:1000px;"></div>

<script>
var isDateField =[];
$.ajax({
url: "https://run.mocky.io/v3/073ca8a1-6fe6-44f5-a33d-182a7788d3a4",
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>

posted on   daconglee  阅读(82)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
点击右上角即可分享
微信分享提示