Чтобы получить динамические столбцы, я предлагаю вам использовать альтернативный подход, а не клиентские шаблоны, потому что вам нужно будет изменить строку заголовка HTML, что может привести к ошибкам при сортировке и фильтрации.
<div id="grid"></div>
<script>
var products = [{
ProductID : 1,
ProductName : "Chai",
SupplierID : 1,
CategoryID : 1,
QuantityPerUnit : "10 boxes x 20 bags",
UnitPrice : 18.0000,
UnitsInStock : 39,
UnitsOnOrder : 0,
ReorderLevel : 10,
Discontinued : false,
Category : [{
CategoryID : 1,
CategoryName : "Beverages",
Description : "Soft drinks, coffees, teas, beers, and ales"
},{
CategoryID : 2,
CategoryName : "Condiments",
Description : "Sweet and savory sauces, relishes, spreads, and seasonings"
}]
},
{
ProductID : 2,
ProductName : "Chang",
SupplierID : 1,
CategoryID : 1,
QuantityPerUnit : "24 - 12 oz bottles",
UnitPrice : 19.0000,
UnitsInStock : 17,
UnitsOnOrder : 40,
ReorderLevel : 25,
Discontinued : false,
Category : [{
CategoryID : 1,
CategoryName : "Beverages",
Description : "Soft drinks, coffees, teas, beers, and ales"
},
{
CategoryID : 3,
CategoryName : "Confections",
Description : "Desserts, candies, and sweet breads"
}]
}]
$(document).ready(function() {
$("#grid").kendoGrid({
dataSource: {
data: products,
schema: {
model: {
fields: {
ProductName: { type: "string" },
UnitPrice: { type: "number" },
UnitsInStock: { type: "number" },
Discontinued: { type: "boolean" }
}
}
},
pageSize: 20
},
height: 550,
pageable: {
input: true,
numeric: false
},
columns: [
"ProductName",
{ field: "UnitPrice", title: "Unit Price", format: "{0:c}", width: "130px", sortable: true, filterable: true },
{ field: "UnitsInStock", title: "Units In Stock", width: "130px" , sortable: true, filterable: true},
{ field: "Discontinued", width: "130px" , sortable: true, filterable: true},
{ field: "Category", sortable: false, filterable: false,
template: function iterate(dataItem)
{
var template = "";
for (var i = 0; i < dataItem.Category.length; i++) {
template = template + "<td role='gridcell'>" + dataItem.Category[i].CategoryName
+ "</td><td role='gridcell'>"
+ dataItem.Category[i].Description + "</td>";
}
return template;
}
}],
dataBound:function(e)
{
$("#grid thead tr").append('<th scope="col" role="columnheader" data-field="Category" aria-haspopup="true" class="k-header">Category Desc</th><th scope="col" role="columnheader" data-field="Category" aria-haspopup="true" class="k-header">Category</th><th scope="col" role="columnheader" data-field="Category Desc" aria-haspopup="true" class="k-header">Category Desc</th>');
}
});
});
</script>
Альтернативы могут быть: -