Kendo 网格 MVC / 导出到 Excel 无需额外请求

Kendo Grid MVC / export to Excel without additional request

如何设置 kendo 带有 MVC 扩展的网格在导出到 excel 时不做额外的请求?

这是网格定义。问题是当单击导出到 excel 按钮时网格向服务器发出额外请求,需要在没有请求的情况下导出显示的数据。

@(Html.Kendo()
    .Grid<VmTaskItem>()
    .Name("kTasks")
    .Columns(c => {
        c.Bound(m => m.TaskType).Title().Width(150);
    })
    .ToolBar(tb => tb.Excel())
    .Excel(e => e
        .AllPages(false) // must disable request when export
    )
    .DataSource(ds => ds
        .Ajax()
        .PageSize(30)
        .Read(r => r.Action("GetTaskItems", "Home")
        .Type(HttpVerbs.Get))
    ))

来自文档 https://docs.telerik.com/aspnet-mvc/helpers/grid/excel-export

When the AllPages option is set to true the Grid makes a read request for all data.

此外,在 MVC Grid 的演示页面上,在导出到 excel
时提出添加请求 http://demos.telerik.com/aspnet-mvc/grid/excel-export
在没有 MVC 的 Grid 演示页面没有
http://demos.telerik.com/kendo-ui/grid/excel-export

导出功能将在执行导出之前查询数据。

MVC 演示发出读取请求,因为如果您未指定 razor Grid 帮助器,则默认使用服务器操作,而 javascript 初始化默认为 false。

如果您明确将 .ServerOperation(false) 添加到您的 .DataSource 配置中,

.DataSource(ds => ds
    .Ajax()
    .PageSize(30)
    .Read(r => r.Action("GetTaskItems", "Home")
    .Type(HttpVerbs.Get))
    .ServerOperation(false)
))

应该不会在导出数据前发出请求,但只会导出当前页面。

如果您需要服务器操作并且在导出之前没有访问服务器...您可能需要覆盖内置导出并自己实现它。