Kendo Grid calculate difference between two row values - kendo-ui

I am trying to get the row item values of the first and second row and calculate a difference und display it as an custom aggregate. This should happen after every sort so it should check if there are only two rows showing. So for example if the turnover for the year 2017 is 10000€ and 5000€ for 2018 the result should be -50% .
my code:
$(document).ready(function() {
$("#grid").kendoGrid({
toolbar: ["excel"],
excel: {
fileName: "turnover.xlsx",
allPages: true
},
dataSource: {
transport: {
read: "data/turnovers.php",
dataType: "jsonp"
},
schema: {
data: "data",
total: function(result) {
var data = this.data(result);
return data ? data.length : 0;
},
model: {
fields: {
January: { type: "number" },
February: { type: "number" },
March: { type: "number" },
April: { type: "number" },
May: { type: "number" },
June: { type: "number" },
July: { type: "number" },
August: { type: "number" },
September: { type: "number" },
October: { type: "number" },
November: { type: "number" },
December: { type: "number" },
sum: { type: "number" },
jahr: { type: "string" }
}
}
},
pageSize: 50,
aggregate: [ { field: "January", aggregate: "sum" },
{ field: "February", aggregate: "sum" },
{ field: "March", aggregate: "sum" },
{ field: "April", aggregate: "sum" },
{ field: "May", aggregate: "sum" },
{ field: "June", aggregate: "sum" },
{ field: "July", aggregate: "sum" },
{ field: "August", aggregate: "sum" },
{ field: "September", aggregate: "sum" },
{ field: "October", aggregate: "sum" },
{ field: "November", aggregate: "sum" },
{ field: "December", aggregate: "sum" },
{ field: "sum", aggregate: "sum" }
]
},
height: 870,
scrollable: true,
sortable: true,
filterable: true,
pageable: {
refresh: true,
pageSizes: true
},
columns: [
{
field: "January",
title: "January",
filterable: false,
format: "{0:c2}",
footerTemplate: "sum: #: kendo.toString(sum, '\\#\\#,\\# €') #"
},
{
field: "February",
title: "February",
filterable: false,
format: "{0:c2}",
footerTemplate: "sum: #: kendo.toString(sum, '\\#\\#,\\# €') #"
},
{
field: "March",
title: "March",
filterable: false,
format: "{0:c2}",
footerTemplate: "sum: #: kendo.toString(sum, '\\#\\#,\\# €') #"
},
{
field: "April",
title: "April",
filterable: false,
format: "{0:c2}",
footerTemplate: "sum: #: kendo.toString(sum, '\\#\\#,\\# €') #"
},
{
field: "May",
title: "May",
filterable: false,
format: "{0:c2}",
footerTemplate: "sum: #: kendo.toString(sum, '\\#\\#,\\# €') #"
},
{
field: "June",
title: "June",
filterable: false,
format: "{0:c2}",
footerTemplate: "sum: #: kendo.toString(sum, '\\#\\#,\\# €') #"
},
{
field: "July",
title: "July",
filterable: false,
format: "{0:c2}",
footerTemplate: "sum: #: kendo.toString(sum, '\\#\\#,\\# €') #"
},
{
field: "August",
title: "August",
filterable: false,
format: "{0:c2}",
footerTemplate: "sum: #: kendo.toString(sum, '\\#\\#,\\# €') #"
},
{
field: "September",
title: "September",
filterable: false,
format: "{0:c2}",
footerTemplate: "sum: #: kendo.toString(sum, '\\#\\#,\\# €') #"
},
{
field: "October",
title: "October",
filterable: false,
format: "{0:c2}",
footerTemplate: "sum: #: kendo.toString(sum, '\\#\\#,\\# €') #"
},
{
field: "November",
title: "November",
filterable: false,
format: "{0:c2}",
footerTemplate: "sum: #: kendo.toString(sum, '\\#\\#,\\# €') #"
},
{
field: "December",
title: "December",
filterable: false,
format: "{0:c2}",
footerTemplate: "sum: #: kendo.toString(sum, '\\#\\#,\\# €') #"
},
{
field: "sum",
title: "sum",
format: "{0:c2}",
filterable: false,
footerTemplate: "sum: #: kendo.toString(sum, '\\#\\#,\\# €') #"
}
]
});
});
approach:
function calculatediff(){
var entityGrid = $("#grid").data("kendoGrid");
var data = entityGrid.dataSource.data();
var totalNumber = data.length;
if(totalNumber = 2) {
var currentDataItem = data[0];
var currentDataItem = data[1];
row1 = currentDataItem.January;
row2 = currentDataItem.January;
diff = ((row1-row2)/((row1+row2)/2))*100
}
}

Data source supported aggregate functions are only average, count, max, min, sum and does not have an framework for adding custom functions that work in the same manner.
The aggregates that are computed become available to the footerTemplate as a data object that is available at template resolution time. Thus, the footer can contain a function that is passed this object and perform the custom computation.
This dojo snippet demonstrates the technique.
The dataSource was tweaked to be two rows, and both count and sum (supported aggregate functions) aggregation is specified for each column.
data: [
{ January: 20, February: 30, March: 140, sum: 190 },
{ January: 30, February: 20, March: 50, sum: 100 }
],
pageSize: 50,
aggregate: [ { field: "January", aggregate: "count" }, { field: "January", aggregate: "sum" },
{ field: "February", aggregate: "count" }, { field: "February", aggregate: "sum" },
{ field: "March", aggregate: "count" }, { field: "March", aggregate: "sum" },
{ field: "sum", aggregate: "count" }, { field: "sum", aggregate: "sum" },
]
},
In the columns declaration all the footer templates were tweaked to invoke a custom function that is passed the aggregate data object and the field.
{
field: "January",
/* ... */
footerTemplate: "#= customFooter(data,'January') #"
},
{
field: "February",
/* ... */
footerTemplate: "#= customFooter(data,'February') #"
},
{
field: "March",
/* ... */
footerTemplate: "#= customFooter(data,'March') #"
},
{
field: "sum",
/* ... */
footerTemplate: "#= customFooter(data,'sum') #"
}
Code running inside the footerTemplates does not know the field, so you have to repeat the field name inside the template.
Finally, customFooter() function uses the field aggregates 1) to determine if the special result should be computed and 2) as a partial contribution to the computation. The difference is computed directly from the data source.
function customFooter (ds_agg, field) {
if (ds_agg[field].count == 2) {
var ds = $("#grid").data("kendoGrid").dataSource;
var diff = ds.view()[0][field] - ds.view()[1][field];
var ratio = ( diff / ( ds_agg[field].sum / 2 ) ) * 100;
return "diff: " + diff + "<br />sum: " + ds_agg[field].sum + "<br /> ratio: " + ratio;
}
}

Related

Subtracting two fields in Kendo grid

I am working with kendo grid and now I want to show a third column in kendo grid by subtracting two other fields. Is this possible in kendo grid. For eg: I want to show the field
"Allocated"= "TotalAmount-TotalDepriciated"
Code:
$("#grid").kendoGrid({
dataSource: DataSource,
autoBind: false,
scrollable: false,
sortable: {
allowUnsort: false
},
filterable: { mode: "row", style: "max-width:100px;" },
groupable: true,
pageable: {
refresh: true,
buttonCount: 5,
pageSizes: GlobalPagingDDL
},
//rowTemplate: kendo.template($("#template").html()),
dataBound: gridDataBound,
columns:
[
//field: "Name", title: "Licensee", width: 200, filterable: { cell: { showOperators: false, suggestionOperator: "contains" } }, template: "<a href='javascript:void(0)' onclick='RedirectToOrgDetail("#:LicenseeId#" , "#:PublicName#" )' >#:LicenseeName# </a>" },
{ field: "AgreementName", title: "Agreement Name", width: 200, filterable: { cell: { showOperators: false, suggestionOperator: "contains" } } },
{
field: "Count", title: "Related Agreement", width: 150,
filterable: { cell: { showOperators: true, suggestionOperator: "contains" } }
},
{
field: "Status", title: "Status", width: 150, filterable: {
cell: {
template: function (args) {
args.element.kendoDropDownList({
dataSource: new kendo.data.DataSource({
data:
[
{ Status: "Approved" },
{ Status: "Pending" },
]
}),
dataTextField: "Status",
optionLabel: "All",
dataValueField: "Status",
valuePrimitive: true
});
}, showOperators: false, suggestionOperator: "contains"
}
}
},
{
field: "Type", title: "Type", width: 150, filterable: {
cell: {
template: function (args) {
args.element.kendoDropDownList({
dataSource: new kendo.data.DataSource({
data:
[
{ Type: "4" },
{ Type: "3" },
{ Type: "2" },
{ : "1" }
]
}),
dataTextField: "Type",
optionLabel: "All",
dataValueField: "Type",
valuePrimitive: true
});
}, showOperators: false, suggestionOperator: "contains"
}
}
},
{ field: "StartDate", title: "All Periods Start Date", width: 150, format: "{0:MM/dd/yyyy}", filterable: { cell: { showOperators: true } } },
{ field: "EndDate", title: "All Periods End Date", width: 150, format: "{0:MM/dd/yyyy}", filterable: { cell: { showOperators: true } } },
{ field: "TotalAmount", title: "Invoiced", format: "{0:c2}", footerTemplate: "$ #= sum # ", width: 200, filterable: { cell: { showOperators: false, suggestionOperator: "contains" } } },
{ field: "TotalDepriciated", title: "Allocated", format: "{0:c2}", width: 200, footerTemplate: "$ #= sum # " },
{ field: "Allocated", title: "Balance", format: "{0:c2}", filterable: { cell: { showOperators: false, suggestionOperator: "contains" } } },
//{ field: "LastUpdatedDate", title: "Last Updated Date", width: 150, format: "{0:MM/dd/yyyy}", filterable: { cell: { showOperators: true } } }
]
});
This code works for me. I hope that helps
{ field: "Allocated", title: "Allocated",
template: '<div>#= TotalAmount-TotalDepriciated#</div>'
}
I'am trying also to create a footerTemplate to show the sum of this result but i don't understand how ill achieve this for the moment
Please try with the below code snippet.
Below code adds a third column in kendo grid by subtracting two other fields
Allocated: function () {
return this.TotalAmount - this.TotalDepriciated;
},
.....
.....
{ field: "Allocated", title: "Allocated", template: "#= Allocated() #", footerTemplate: "<label id='sumAllocated'></label>" },
Full code:
<!DOCTYPE html>
<html>
<head>
<title>Jayesh Goyani</title>
<link rel="stylesheet" href="http://kendo.cdn.telerik.com/2016.1.412/styles/kendo.common.min.css">
<link rel="stylesheet" href="http://kendo.cdn.telerik.com/2016.1.412/styles/kendo.rtl.min.css">
<link rel="stylesheet" href="http://kendo.cdn.telerik.com/2016.1.412/styles/kendo.default.min.css">
<link rel="stylesheet" href="http://kendo.cdn.telerik.com/2016.1.412/styles/kendo.mobile.all.min.css">
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script src="http://kendo.cdn.telerik.com/2016.1.412/js/angular.min.js"></script>
<script src="http://kendo.cdn.telerik.com/2016.1.412/js/jszip.min.js"></script>
<script src="http://kendo.cdn.telerik.com/2016.1.412/js/kendo.all.min.js"></script>
</head>
<body>
<div id="grid">
</div>
<script>
var products = [{
ProductID: 1,
ProductName: "Chai",
TotalAmount: 100,
TotalDepriciated: 10,
}, {
ProductID: 2,
ProductName: "Chang",
TotalAmount: 100,
TotalDepriciated: 10,
}, {
ProductID: 3,
ProductName: "Aniseed Syrup",
TotalAmount: 100,
TotalDepriciated: 10,
}, {
ProductID: 4,
ProductName: "Chef Anton's Cajun Seasoning",
TotalAmount: 100,
TotalDepriciated: 10,
}, {
ProductID: 5,
ProductName: "Chef Anton's Gumbo Mix",
TotalAmount: 100,
TotalDepriciated: 30,
}];
$(document).ready(function () {
$("#grid").kendoGrid({
dataSource: {
data: products,
schema: {
model: {
id: "ProductID",
fields: {
ProductName: {
type: "string"
},
TotalAmount: {
type: "number"
},
TotalDepriciated: {
type: "number"
}
},
Allocated: function () {
return this.TotalAmount - this.TotalDepriciated;
},
}
},
aggregate: [{ field: "TotalAmount", aggregate: "sum" },
{ field: "TotalDepriciated", aggregate: "sum" }],
pageSize: 10
},
sortable: true,
dataBound: function (e) {
var sumTotalAmount = parseFloat($("#sumTotalAmount").html());
var sumTotalDepriciated = parseFloat($("#sumTotalDepriciated").html());
$("#sumAllocated").html(sumTotalAmount - sumTotalDepriciated);
},
filterable: true,
pageable: {
input: true,
numeric: false
},
columns: [
{ field: "ProductID", title: "ProductID" },
{ field: "ProductName", title: "ProductName" },
{ field: "TotalAmount", title: "TotalAmount", aggregates: ["sum"], footerTemplate: "<label id='sumTotalAmount'> #=sum# </label>" },
{ field: "TotalDepriciated", title: "TotalDepriciated", aggregates: ["sum"], footerTemplate: "<label id='sumTotalDepriciated'> #=sum# </label>" },
{ field: "Allocated", title: "Allocated", template: "#= Allocated() #", footerTemplate: "<label id='sumAllocated'></label>" },
{ command: ["edit", "destroy"], title: " " }
],
editable: "inline"
});
});
</script>
</body>
</html>
Let me know if any concern.

KendoUI Date field is not filtering. It's Making grid blank

I tried with sample data using below JS code:
$(document).ready(function () {
var PraksysDateFormats = ["dd-MM-yyyy", "dd/MM/yyyy", "dd-MM-yy", "dd/MM/yy", "dd.MM.yy", "ddMMyy", "ddMM", "dd.MM.yyyy", "ddMMyyyy"];
relationDataSource = new kendo.data.DataSource({
data: [
{ ProductName: "Computer", UnitPrice: 100, UnitsInStock: 5, Discontinued: false, FromDate: new Date(kendo.toString(kendo.parseDate("10-11-2016", 'dd-MM-yyyy'), 'yyyy-MM-dd')), ToDate: new Date(kendo.toString(kendo.parseDate("11-11-2016", 'dd-MM-yyyy'), 'yyyy-MM-dd')) },
{ ProductName: "TV", UnitPrice: 1000, UnitsInStock: 5, Discontinued: false, FromDate: new Date(kendo.toString(kendo.parseDate("20-10-2016", 'dd-MM-yyyy'), 'yyyy-MM-dd')), ToDate: new Date(kendo.toString(kendo.parseDate("22-10-2016", 'dd-MM-yyyy'), 'yyyy-MM-dd')) },
],
schema: {
model: {
fields: {
ProductName: { type: "string" },
UnitPrice: { type: "number" },
UnitsInStock: { type: "number" },
Discontinued: { type: "boolean" },
FromDate: { type: "date" },
ToDate: { type: "date" }
}
}
},
});
var grid = $("#grid").kendoGrid({
dataSource: relationDataSource,
scrollable: true,
sortable: true,
//editable: true,
filterable: true,
columns: [
{ field: "ProductName", title: "Product Name" },
{ field: "UnitPrice", title: "Unit Price", format: "{0:c}" },
{ field: "UnitsInStock", title: "Units In Stock" },
{ field: "Discontinued", title: "Discontinued" },
{
field: "FromDate", title: "From Date", editor: FromDatePicker, width: 200, format: "{0:dd-MM-yyyy}", filterable: {
ui: dateFilter
}
},
{
field: "ToDate", title: "To Date", editor: ToDatePicker, width: 200, format: "{0:dd-MM-yyyy}", filterable: {
ui: dateFilter
}
}
],
edit: function (e) {
var grid = this;
var fieldName = grid.columns[e.container.index()].field;
// alert(fieldName)
},
save: function (e) {
var grid = this;
var fieldName = grid.columns[e.container.index()].field;
// alert(e.container.index());
// alert(fieldName)
var productName = e.values.ProductName || e.model.ProductName;
var relation = e.values.Relation || e.model.Relation;
var dataItem = this.dataSource.getByUid(e.model.uid);
dataItem.set("ProductName", productName);
dataItem.set("UnitPrice", 9000);
dataItem.set("UnitsInStock", 99);
dataItem.set("Discontinued", true);
},
update: function (e) {
alert("Update")
}
}).data("kendoGrid");
function FromDatePicker(container, options) {
alert(options.field);
$('<input id="BrugergyldigFradato" name="FromDate" dynamicfield="71" data-bind="value:' + options.field + '"/>')
.appendTo(container)
.kendoDatePicker({
format: "dd-MM-yyyy",
parseFormats: PraksysDateFormats,
culture: "da-DK"
});
$('<span class="k-invalid-msg" data-for="FromDate"></span>').appendTo(container);
};
function ToDatePicker(container, options) {
alert(options.field);
$('<input id="BrugergyldigTildato" name="ToDate" dynamicfield="71" data-bind="value:' + options.field + '"/>')
.appendTo(container)
.kendoDatePicker({
format: "dd-MM-yyyy",
parseFormats: PraksysDateFormats,
culture: "da-DK"
});
$('<span class="k-invalid-msg" data-for="ToDate"></span>').appendTo(container);
};
function dateFilter(element) {
element.kendoDatePicker({
format: "dd-MM-yyyy",
culture: "da-DK"
});
};
});
I am able to filter all columns, except last 2 date fields. On entering a date field filter value, it's making the grid blank. My date format is "dd-MM-yyyy". Any clue here would be appreciated.
Don't use new Date in your datasource.
On my browser(chrome):
new Date("2016-11-11")
Fri Nov 11 2016 02:00:00 GMT+0200
As you see date is converted to local time here. But when you filter with 11-11-2016 your filter will not be converted. In that case your filter value will be
Fri Nov 11 2016 00:00:00 GMT+0200
Therefore equality check fails.
Instead you can use only kendo.parseDate to assign your date values and it will work.
data: [
{ ProductName: "Computer", UnitPrice: 100, UnitsInStock: 5, Discontinued: false, FromDate: kendo.parseDate("10-11-2016", 'dd-MM-yyyy'), ToDate: kendo.parseDate("11-11-2016", 'dd-MM-yyyy') },
{ ProductName: "TV", UnitPrice: 1000, UnitsInStock: 5, Discontinued: false, FromDate: kendo.parseDate("20-10-2016", 'dd-MM-yyyy'), ToDate: kendo.parseDate("22-10-2016", 'dd-MM-yyyy') },
],
This turnout to be a formatting issue and now working as expected

Kendo Grid does not aggregate

I'm using Kendo UI Complete v2013.2.918, trying to show a grid with aggregates by many columns related to one group column, the aggregates is only summing the latest row, I follow the examples on the kendo page and it looks the same but is not doing the sum. Any idea what is the issue?
this is my code:
detailGrid = $("#details_paginated_grid_table").kendoGrid({
columns: [
{ field: "CallDate", title: translate('DATE') },
{ field: "CallTime", title: translate('TIME') },
{ field: "From", title: translate('FROM') },
{ field: "Destination", title: translate('DESTINATION') },
{ field: "MaskedCalledNumber", title: translate('DIALNUMBER') },
{ field: "CallType", title: translate('CALLTYPE'), groupHeaderTemplate: "Name: #= value # total Call Type: #=count#" },
{ field: "CallDurationDisplay", title: translate('MINUTES'), footerTemplate: "Total: #=sum#", groupFooterTemplate: "Minutes: #=sum#", aggregates: ["sum"] },
{ field: "CallRateDisplay", title: translate('RATE'), footerTemplate: "Total: #=sum#", groupFooterTemplate: "Rate: #=sum#", aggregates: ["sum"] },
{ field: "LongDistanceChargeDisplay", title: translate('LD'), footerTemplate: "Total: #=sum#", groupFooterTemplate: "LD: #=sum#", aggregates: ["sum"] },
{ field: "PktDtVolDisplay", title: translate('DATA'), footerTemplate: "Total: #=sum#", groupFooterTemplate: "Data: #=sum#", aggregates: ["sum"] },
{ field: "TotalChargesDisplay", title: translate('TOTAL'), footerTemplate: "Total: #=sum#", groupFooterTemplate: "Charges: #=sum#", aggregates: ["sum"] }
],
dataSource: {
data: [],
group: {
field: "CallType",
aggregates: [
{ field: "CallDurationDisplay", aggregate: "sum" },
{ field: "CallRateDisplay", aggregate: "sum" },
{ field: "LongDistanceChargeDisplay", aggregate: "sum" },
{ field: "PktDtVolDisplay", aggregate: "sum" },
{ field: "TotalChargesDisplay", aggregate: "sum" },
{ field: "CallType", aggregate: "count" }
]
},
aggregate: [
{ field: "CallDurationDisplay", aggregate: "sum" },
{ field: "CallRateDisplay", aggregate: "sum" },
{ field: "LongDistanceChargeDisplay", aggregate: "sum" },
{ field: "PktDtVolDisplay", aggregate: "sum" },
{ field: "TotalChargesDisplay", aggregate: "sum" }
]
},
//sortable: false,
scrollable: false,
//pageable: false,
groupable: true,
height: 300,
});
Here an screenshot about the aggregates:

Export customize data to excel in kendo Grid

I am using kendo grid plugin to show data.
Have used the inbuilt functionality export to excel of kendo grid,to export grid data.
http://demos.telerik.com/kendo-ui/grid/excel-export
But I want to remove 2 columns before exporting the data to excel.
Please let me know how can I acheive it
Thanks,
Nupur
Please find the fiddle here, hope it will help to solve your issue.
You can read the documentation here.
<div id="grid" style="width: 900px"></div>
<script>
$("#grid").kendoGrid({
toolbar: ["Export"],
dataSource: {
type: "odata",
transport: {
read: "http://demos.telerik.com/kendo-ui/service/Northwind.svc/Products"
},
schema:{
model: {
fields: {
UnitsInStock: { type: "number" },
ProductName: { type: "string" },
UnitPrice: { type: "number" },
UnitsOnOrder: { type: "number" },
UnitsInStock: { type: "number" }
}
}
},
pageSize: 7,
group: {
field: "UnitsInStock", aggregates: [
{ field: "ProductName", aggregate: "count" },
{ field: "UnitPrice", aggregate: "sum"},
{ field: "UnitsOnOrder", aggregate: "average" },
{ field: "UnitsInStock", aggregate: "count" }
]
},
aggregate: [
{ field: "ProductName", aggregate: "count" },
{ field: "UnitPrice", aggregate: "sum" },
{ field: "UnitsOnOrder", aggregate: "average" },
{ field: "UnitsInStock", aggregate: "min" },
{ field: "UnitsInStock", aggregate: "max" }
]
},
sortable: true,
pageable: true,
groupable: true,
filterable: true,
columnMenu: true,
reorderable: true,
resizable: true,
columns: [
{ width: 300, locked: true, field: "ProductName", title: "Product Name", aggregates: ["count"], footerTemplate: "Total Count: #=count#", groupFooterTemplate: "Count: #=count#" },
{ width: 300, field: "UnitPrice", title: "Unit Price", aggregates: ["sum"] },
{ width: 300, field: "UnitsOnOrder", title: "Units On Order", aggregates: ["average"], footerTemplate: "Average: #=average#",
groupFooterTemplate: "Average: #=average#" },
{ width: 300, field: "UnitsInStock", title: "Units In Stock", aggregates: ["min", "max", "count"], footerTemplate: "Min: #= min # Max: #= max #",
groupHeaderTemplate: "Units In Stock: #= value # (Count: #= count#)" }
]
});
$(".k-grid-Export").on('click', function(e){
var grid = $("#grid").getKendoGrid();
var rows = [{
cells: [
{ value: "ProductName" },
{ value: "UnitPrice" }
]
}];
var trs = $("#grid").find('tr');
for (var i = 0; i < trs.length; i++) {
var dataItem = grid.dataItem(trs[i]);
rows.push({
cells: [
{ value: dataItem.ProductName },
{ value: dataItem.UnitPrice }
]
})
}
excelExport(rows);
e.preventDefault();
});
function excelExport(rows) {
var workbook = new kendo.ooxml.Workbook({
sheets: [
{
columns: [
{ autoWidth: true },
{ autoWidth: true }
],
title: "Orders",
rows: rows
}
]
});
kendo.saveAs({dataURI: workbook.toDataURL(), fileName: "Test.xlsx"});
}
</script>
[UPDATE]
To Export only filtered rows then use the following code. Refer the fiddle for working example
<div id="example">
<div id="grid" style="width: 900px"></div>
<script>
$("#grid").kendoGrid({
toolbar: ["Export"],
dataSource: {
type: "odata",
transport: {
read: "http://demos.telerik.com/kendo-ui/service/Northwind.svc/Products"
},
schema:{
model: {
fields: {
UnitsInStock: { type: "number" },
ProductName: { type: "string" },
UnitPrice: { type: "number" },
UnitsOnOrder: { type: "number" },
UnitsInStock: { type: "number" }
}
}
},
pageSize: 7,
group: {
field: "UnitsInStock", aggregates: [
{ field: "ProductName", aggregate: "count" },
{ field: "UnitPrice", aggregate: "sum"},
{ field: "UnitsOnOrder", aggregate: "average" },
{ field: "UnitsInStock", aggregate: "count" }
]
},
aggregate: [
{ field: "ProductName", aggregate: "count" },
{ field: "UnitPrice", aggregate: "sum" },
{ field: "UnitsOnOrder", aggregate: "average" },
{ field: "UnitsInStock", aggregate: "min" },
{ field: "UnitsInStock", aggregate: "max" }
]
},
sortable: true,
pageable: true,
groupable: true,
filterable: true,
columnMenu: true,
reorderable: true,
resizable: true,
columns: [
{ width: 300, locked: true, field: "ProductName", title: "Product Name", aggregates: ["count"], footerTemplate: "Total Count: #=count#", groupFooterTemplate: "Count: #=count#" },
{ width: 300, field: "UnitPrice", title: "Unit Price", aggregates: ["sum"] },
{ width: 300, field: "UnitsOnOrder", title: "Units On Order", aggregates: ["average"], footerTemplate: "Average: #=average#",
groupFooterTemplate: "Average: #=average#" },
{ width: 300, field: "UnitsInStock", title: "Units In Stock", aggregates: ["min", "max", "count"], footerTemplate: "Min: #= min # Max: #= max #",
groupHeaderTemplate: "Units In Stock: #= value # (Count: #= count#)" }
]
});
$(".k-grid-Export").on('click', function(e){
var grid = $("#grid").getKendoGrid();
var rows = [{
cells: [
{ value: "ProductName" },
{ value: "UnitPrice" }
]
}];
//var trs = $("#grid").find('tr');
var trs = grid.dataSource;
var filteredDataSource = new kendo.data.DataSource({
data: trs.data(),
filter: trs.filter()
});
filteredDataSource.read();
var data = filteredDataSource.view();
for (var i = 0; i < data.length; i++) {
var dataItem = data[i];
rows.push({
cells: [
{ value: dataItem.ProductName },
{ value: dataItem.UnitPrice }
]
})
}
excelExport(rows);
e.preventDefault();
});
function excelExport(rows) {
var workbook = new kendo.ooxml.Workbook({
sheets: [
{
columns: [
{ autoWidth: true },
{ autoWidth: true }
],
title: "Orders",
rows: rows
}
]
});
kendo.saveAs({dataURI: workbook.toDataURL(), fileName: "Test.xlsx"});
}
</script>
you can try the excelExport event of grid options.
excelExport: function(e) {
var sheet = e.workbook.sheets[0];
for (var i = 0; i < sheet.rows.length; i++) {
sheet.rows[i].cells.reverse();
for (var ci = 0; ci < sheet.rows[i].cells.length; ci++) {
sheet.rows[i].cells[ci].hAlign = "right";
}
}
},
http://docs.telerik.com/kendo-ui/controls/data-management/grid/excel-export#what-is-exported

Kendo grid conditions

dataSource: {
pageSize: 5,
serverPaging: false,
serverSorting: false,
schema: {
model: {
id: "EmploymentHistoryId",
fields: {
EmployerName: { validation: { required: true } },
JobTitle: { validation: { required: true } },
PrimaryPlaceOfPractice: { validation: { required: true } },
StartDate: { validation: { required: true } },
EndDate: { validation: { required: true } },
}
}
}
},
sortable: true,
pageable: true,
toolbar: ["create"],
editable: "popup",
autoSync: true,
columns: [
{
field: "EmployerName",
title: "Name of Employer"
},{
field: "JobTitle",
title: "Job Title"
},{
field: "PrimaryPlaceOfPractice",
title: "Primary Place Of Practice"
},
{
field: "StartDate",
title: "Start Date",
template: "#= kendo.toString(kendo.parseDate(StartDate, 'yyyy-MM-dd'), 'MM/dd/yyyy') #"
}, {
field: "EndDate",
title: "End Date",
template: "#= kendo.toString(kendo.parseDate(EndDate, 'yyyy-MM-dd'), 'MM/dd/yyyy') #"
},{
command: ["destroy", "edit"],
title: "Action"
}
]
here by "PrimaryPlaceOfPractice" db return 1 or 0. But what I want to do is I want to show "Yes" in Grid if it returns 1 and "No" if 0. How can I do this. Is there a way to handle conditions. Is there a way to show a different value instead of a retrieved value from database.
/* ... */
columns: [
/* ... */
},{
field: "PrimaryPlaceOfPractice",
title: "Primary Place Of Practice",
template: "#=PrimaryPlaceOfPractice == 1 ? 'Yes' : 'No'#"
},{
/* ... */
]
/* ... */

Resources