asp.net-mvc – 无法导出Kendo Grid中的隐藏列

前端之家收集整理的这篇文章主要介绍了asp.net-mvc – 无法导出Kendo Grid中的隐藏列前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想在Kendo Grid上隐藏一些列,并将它们作为可见列导出到excel.但是,使用隐藏(true)或Visible(false)没有任何意义,并且不会导出这些字段. this页面上的变通方法无效.任何的想法?

视图:

  1. @(Html.Kendo().Grid<Contactviewmodel>()
  2. .Name("Grid")
  3. .Columns(columns =>
  4. {
  5. columns.Bound(m => m.NameSurname).Title("Name Surname").Width("%100");
  6. columns.Bound(m => m.InstituteName).Title("Institute Name").Width("250px");
  7. columns.Bound(m => m.CityName).Title("City").Width("145px");
  8. columns.Bound(m => m.RegionName).Title("Region").Width("145px");
  9. columns.Bound(m => m.ContactMobile).Title("Mobile").Width("125px");
  10. columns.Bound(m => m.ContactAddress).Title("Address").Hidden(true); //I want to export these fields
  11. columns.Bound(m => m.ContactAddress).Title("Address").Visible(false); //I want to export these fields
  12. })
  13. .ToolBar(toolbar =>
  14. {
  15. toolbar.Template(@<text>
  16. <div class="toolbar">
  17. <button class="btn btn-primary btn-xs pull-right k-button k-button-icontext k-grid-excel">
  18. <span class="k-icon k-excel"></span>
  19. Liste (xls)
  20. </button>
  21. </div>
  22. </text>);
  23. })
  24.  
  25. .Excel(excel => excel
  26. .FileName("List.xlsx")
  27. .Filterable(true)
  28. .AllPages(true)
  29. .ProxyURL(Url.Action("Excel_Export_Save","Controller"))
  30. )
  31. .DataSource(dataSource => dataSource
  32. .Ajax()
  33. .Read(read => read.Action("Index_Read","Controller"))
  34. .ServerOperation(false)
  35. .PageSize(12)
  36. )
  37. )
  38. )

解决方法

请参阅此解决方Plunker,建议在 Telerik网站上找到解决方案.
要在导出功能显示列,请绑定该网格的“excelExport”事件.
  1. var exportFlag = false;
  2. $("#grid").data("kendoGrid").bind("excelExport",function (e) {
  3. if (!exportFlag) {
  4. // e.sender.showColumn(0); for demo
  5. // for your case show column that you want to see in export file
  6. e.sender.showColumn(5);
  7. e.sender.showColumn(6);
  8. e.preventDefault();
  9. exportFlag = true;
  10. setTimeout(function () {
  11. e.sender.saveAsExcel();
  12. });
  13. } else {
  14. e.sender.hideColumn(5);
  15. e.sender.hideColumn(6);
  16. exportFlag = false;
  17. }
  18. });

演示:隐藏第一列并在导出文件显示

  1. <!DOCTYPE html>
  2. <html>
  3.  
  4. <head>
  5. <base href="http://demos.telerik.com/kendo-ui/grid/excel-export">
  6. <style>
  7. html {
  8. font-size: 12px;
  9. font-family: Arial,Helvetica,sans-serif;
  10. }
  11. </style>
  12. <title></title>
  13. <link rel="stylesheet" href="http://cdn.kendostatic.com/2015.1.318/styles/kendo.common-material.min.css" />
  14. <link rel="stylesheet" href="http://cdn.kendostatic.com/2015.1.318/styles/kendo.material.min.css" />
  15. <link rel="stylesheet" href="http://cdn.kendostatic.com/2015.1.318/styles/kendo.dataviz.min.css" />
  16. <link rel="stylesheet" href="http://cdn.kendostatic.com/2015.1.318/styles/kendo.dataviz.material.min.css" />
  17.  
  18. <script src="http://cdn.kendostatic.com/2015.1.318/js/jquery.min.js"></script>
  19. <script src="http://cdn.kendostatic.com/2015.1.318/js/jszip.min.js"></script>
  20. <script src="http://cdn.kendostatic.com/2015.1.318/js/kendo.all.min.js"></script>
  21. </head>
  22.  
  23. <body>
  24. <div id="example">
  25. <div id="grid" style="width: 900px"></div>
  26. <script>
  27. $("#grid").kendoGrid({
  28. toolbar: ["excel"],excel: {
  29. fileName: "Kendo UI Grid Export.xlsx",proxyURL: "http://demos.telerik.com/kendo-ui/service/export",filterable: true
  30. },dataSource: {
  31. type: "odata",transport: {
  32. read: "http://demos.telerik.com/kendo-ui/service/Northwind.svc/Products"
  33. },schema: {
  34. model: {
  35. fields: {
  36. UnitsInStock: {
  37. type: "number"
  38. },ProductName: {
  39. type: "string"
  40. },UnitPrice: {
  41. type: "number"
  42. },UnitsOnOrder: {
  43. type: "number"
  44. },UnitsInStock: {
  45. type: "number"
  46. }
  47. }
  48. }
  49. },pageSize: 7
  50. },sortable: true,pageable: true,columns: [{
  51. width: "10%",field: "ProductName",title: "Product Name",hidden: true
  52. },{
  53. width: "10%",field: "UnitPrice",title: "Unit Price"
  54. },field: "UnitsOnOrder",title: "Units On Order"
  55. },field: "UnitsInStock",title: "Units In Stock"
  56. }]
  57. });
  58. var exportFlag = false;
  59. $("#grid").data("kendoGrid").bind("excelExport",function (e) {
  60. if (!exportFlag) {
  61. e.sender.showColumn(0);
  62. e.preventDefault();
  63. exportFlag = true;
  64. setTimeout(function () {
  65. e.sender.saveAsExcel();
  66. });
  67. } else {
  68. e.sender.hideColumn(0);
  69. exportFlag = false;
  70. }
  71. });
  72. </script>
  73. </div>
  74.  
  75.  
  76. </body>
  77.  
  78. </html>

猜你在找的asp.Net相关文章