I have an MVC view of 3 Kendo UI Grids, along with an Export to Excel button to export the data from the 3 grids to 3 Excel worksheets.
The following is a sample of the page script (with column detail removed for simplicity).
When I execute the Export button, I get a Javascript error "Unable to get property 'saveAsExcel' of undefined or null reference" at the line identified below with //##errorLine##. So for some reason the grid references can not be determined.
What am I missing here?
<script type="text/javascript">
// used to sync the grid exports
var promises = [
$.Deferred(),
$.Deferred(),
$.Deferred()
];
</script>
<div style="text-align:center;">
@(Html.Kendo().Grid(Model)
.Name("HccWp")
.Columns(columns =>
{
...
})
.ClientDetailTemplateId("outOfBalanceDetailTemplate")
.Sortable(sortable => sortable.SortMode(GridSortMode.SingleColumn).AllowUnsort(true))
.Resizable(resizable => resizable.Columns(true))
.AutoBind(true)
.Events(e => e
.DataBound(@<text>function(e) {onDataBound(e, "HccWp")}</text>)
.ExcelExport(@<text>function(e) {e.preventDefault(); promises[0].resolve(e.workbook);}</text>)
)
.DataSource(dataSource => dataSource
.Ajax()
.Model(model =>
{
...
})
)
)
</div>
<div style="text-align:center;">
@(Html.Kendo().Grid(Model)
.Name("HccPr")
.Columns(columns =>
{
...
})
.ClientDetailTemplateId("outOfBalanceDetailTemplate")
.Sortable(sortable => sortable.SortMode(GridSortMode.SingleColumn).AllowUnsort(true))
.Resizable(resizable => resizable.Columns(true))
.AutoBind(true)
.Events(e => e
.DataBound(@<text>function(e) {onDataBound(e, "HccPr")}</text>)
.ExcelExport(@<text>function(e) {e.preventDefault(); promises[1].resolve(e.workbook);}</text>)
)
.DataSource(dataSource => dataSource
.Ajax()
.Model(model =>
{
...
})
)
)
</div>
<div style="text-align:center;">
@(Html.Kendo().Grid(Model)
.Name("PointWp")
.Columns(columns =>
{
...
})
.ClientDetailTemplateId("outOfBalanceDetailTemplate")
.Sortable(sortable => sortable.SortMode(GridSortMode.SingleColumn).AllowUnsort(true))
.Resizable(resizable => resizable.Columns(true))
.AutoBind(true)
.Events(e => e
.DataBound(@<text>function(e) {onDataBound(e, "PointWp")}</text>)
.ExcelExport(@<text>function(e) {e.preventDefault(); promises[2].resolve(e.workbook);}</text>)
)
.DataSource(dataSource => dataSource
.Ajax()
//.PageSize(20)
.Model(model =>
{
...
})
)
)
</div>
<br />
<div>
<button id="export" class="k-button"><span class="k-icon k-i-excel"></span> Export to Excel</button>
</div>
<script type="text/javascript">
$("#export").click(function(e){
// trigger export of the HccWp grid
$("#HccWP").data("kendoGrid").saveAsExcel(); //##errorLine##
// trigger export of the HccPr grid
$("#HccPR").data("kendoGrid").saveAsExcel();
// trigger export of the PointWp grid
$("#PointWP").data("kendoGrid").saveAsExcel();
// wait for all exports to finish
$.when.apply(null, promises)
.then(function (hccWpWorkbook, hccPrWorkbook, pointWpWorkbook) {
// create a new workbook using the sheets of the 3 workbooks
var sheets = [
hccWpWorkbook.sheets[0],
hccPrWorkbook.sheets[0],
pointWpWorkbook.sheets[0]
];
sheets[0].title = "HccWP";
sheets[1].title = "HccPR";
sheets[2].title = "PointWP";
var workbook = new kendo.ooxml.Workbook({
sheets: sheets
});
// save the new workbook
kendo.saveAs({
dataURI: workbook.toDataURL(),
fileName: "BCBalancing.xlsx"
})
});
});
</script>