I have created dynamic kendo grid.In grid there is some columns which required formatting. I have one field "valueFormat" in database according that format I want to format other columns.
How to achieve above thing Please help.
link for sample grid
var data = [{
"Dept": "Dev",
"CalculateValue": 0 ,
"AcualValue": 341.917,
"ValueFormat": "#.###"
},
{
"Dept": "Dev",
"CalculateValue": 0,
"AcualValue": 5333.083,
"ValueFormat": "#.###"
},
{
"Dept": "Dev",
"CalculateValue":0 ,
"AcualValue": 8735.666,
"ValueFormat": "{0:c2}"
},
{
"Dept": "Dev",
"CalculateValue":0,
"AcualValue": 126.000,
"ValueFormat": "{0:c2}"
}];
var dateFields = [];
generateGrid(data)
function generateGrid(gridData) {
if ($(".k-header").length > 0) {
grid = $("#grid").data("kendoGrid");
grid.destroy();
$("#grid").empty();
}
$("#grid").kendoGrid({
toolbar: ["excel"],
excel: {
allPages: true,
filterable: true,
fileName: 'Aggregate Expenditure/Utilization Report_.xlsx'
},
dataSource: {
pageSize: 100,
schema: {
data: function () { return gridData; },
total: function () {
return gridData.length;
}
}
},
sortable: true,
filterable: true,
groupable: true,
pageable: true,
columns: generateModel(gridData[0]),
autoBind: true
});
$("#grid").data("kendoGrid").refresh();
}
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;
}
You can enable column resizing for the Kendo UI grid via a simple configuration setting. All you have to do is set its resizable attribute to true, which will make its columns resizable by displaying drag handles/hints when you hover in between their headers.
The Kendo UI grid widget supports static/frozen columns by a single configuration setting. Simply set the locked attribute of the corresponding column to true, and this will bring the column in the locked columns group positioned on the left in the grid.
Returns the data item to which the specified table row is bound. The data item is a Kendo UI Model instance.
You can do it in 3 simple steps: create a variable that will keep row number. drop the value to 0 in the dataBinding. pass function to the template which will increment your variable for each row.
The Kendo grid's column formatting option won't work for you, because it applies the same formatting to all cells within a column, but you are going for different formatting in every row.
In that case you should specify a custom template for the ActualValue column. Inside that template you can use a formatting function to process your value. Since you are already using Kendo, you can take advantage of kendo.format(), like this:
template: "#: kendo.format(ValueFormat, AcualValue)#"
However, that does not work with all of your data, because some of the format strings are already wrapped in brackets and have an index like "{0:c2}", and others are not - "#.###". So you should create a function that makes sure they are consistent, before you pass them to kendo.format(). I've done this with fixValueFormatString(ValueFormat) below.
Here's a link to a working sample: http://jsbin.com/jejixediga/edit?js,console,output
And here's the code:
var data = [{
"Dept": "Dev",
"CalculateValue": 0 ,
"AcualValue": 341.917,
"ValueFormat": "#.###"
}, {
"Dept": "Dev",
"CalculateValue": 0,
"AcualValue": 5333.083,
"ValueFormat": "#.###"
}, {
"Dept": "Dev",
"CalculateValue":0 ,
"AcualValue": 8735.666,
"ValueFormat": "{0:c2}"
}, {
"Dept": "Dev",
"CalculateValue":0,
"AcualValue": 126.000,
"ValueFormat": "{0:c2}"
}];
var dateFields = [];
// Make sure all format strings are consistent
function fixValueFormatString(ValueFormat){
if(!ValueFormat.startsWith("{")){
ValueFormat = "{0:" + ValueFormat + "}";
}
return ValueFormat;
}
generateGrid(data)
function generateGrid(gridData) {
if ($(".k-header").length > 0) {
grid = $("#grid").data("kendoGrid");
grid.destroy();
$("#grid").empty();
}
$("#grid").kendoGrid({
toolbar: ["excel"],
excel: {
allPages: true,
filterable: true,
fileName: 'Aggregate Expenditure/Utilization Report_.xlsx'
},
dataSource: {
pageSize: 100,
schema: {
data: function () { return gridData; },
total: function () {
return gridData.length;
}
}
},
sortable: true,
filterable: true,
groupable: true,
pageable: true,
autoBind: true,
columns: [{
field: "Dept"
}, {
field: "CalculateValue",
}, {
template: "#: kendo.format(fixValueFormatString(ValueFormat), AcualValue)#",
field: "AcualValue"
}, {
field: "ValueFormat"
}]
});
$("#grid").data("kendoGrid").refresh();
}
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;
}
UPDATE - ASSIGN TEMPLATE TO DYNAMICALLY GENERATED COLUMNS
In response to OPs request for a solution that works with dynamic columns, here's the revised code (http://jsbin.com/jinowamosa/edit?js,console,output):
var data = [{
"Dept": "Dev",
"CalculateValue": 0 ,
"AcualValue": 341.917,
"ValueFormat": "#.###"
}, {
"Dept": "Dev",
"CalculateValue": 0,
"AcualValue": 5333.083,
"ValueFormat": "#.###"
}, {
"Dept": "Dev",
"CalculateValue":0 ,
"AcualValue": 8735.666,
"ValueFormat": "{0:c2}"
}, {
"Dept": "Dev",
"CalculateValue":0,
"AcualValue": 126.000,
"ValueFormat": "{0:c2}"
}];
var dateFields = [];
function fixValueFormatString(ValueFormat){
if(!ValueFormat.startsWith("{")){
ValueFormat = "{0:" + ValueFormat + "}";
}
return ValueFormat;
}
generateGrid(data)
function generateGrid(gridData) {
if ($(".k-header").length > 0) {
grid = $("#grid").data("kendoGrid");
grid.destroy();
$("#grid").empty();
}
$("#grid").kendoGrid({
toolbar: ["excel"],
excel: {
allPages: true,
filterable: true,
fileName: 'Aggregate Expenditure/Utilization Report_.xlsx'
},
dataSource: {
pageSize: 100,
schema: {
data: function () { return gridData; },
total: function () {
return gridData.length;
}
}
},
sortable: true,
filterable: true,
groupable: true,
pageable: true,
autoBind: true,
columns: generateColumns(gridData[0])
});
$("#grid").data("kendoGrid").refresh();
}
function generateColumns(gridData) {
var fields = {};
var columns = [];
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
}
};
}
}
for (var field in fields) {
if(field == 'AcualValue'){
columns.push({ field: field, template: "#: kendo.format(fixValueFormatString(ValueFormat), AcualValue)#" });
} else {
columns.push({ field: field });
}
}
return columns;
}
Hope this helps!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With