如何将过滤后的kendo网格内容转换为mvc控制器

时间:2014-05-14 10:24:09

标签: jquery asp.net-mvc kendo-ui kendo-grid

我有一个Kendo Grid,在过滤网格之后,我想导出可用的行来导出CSV Format.Filter工作正常,但是当点击导出按钮时,ajax调用会将所有行发送到控制器。我只需要过滤的行。

2 个答案:

答案 0 :(得分:1)

试试这个,

  <div id="example" class="k-content">
      <button type="button"id="btnExport"  >Export to csv!</button>
<div id="grid"></div>
</div>

<强>脚本

<script>
$(function(){
 var grid = $("#grid").kendoGrid({
            dataSource: {
                type           : "odata",
                transport      : {
                    read: "http://demos.kendoui.com/service/Northwind.svc/Orders"
                },
                schema         : {
                    model: {
                        fields: {
                            OrderID  : { type: "number" },
                            Freight  : { type: "number" },
                            ShipName : { type: "string" },
                            OrderDate: { type: "date" },
                            ShipCity : { type: "string" }
                        }
                    }
                },
                pageSize       : 10
            },
            filterable: true,
            sortable  : true,
            pageable  : true,
            columns   : [
                {
                    field     : "OrderID",
                    filterable: false
                },
                "Freight",
                {
                    field : "OrderDate",
                    title : "Order Date",
                    width : 100,
                    format: "{0:MM/dd/yyyy}"
                },
                {
                    field: "ShipName",
                    title: "Ship Name",
                    width: 200
                },
                {
                    field: "ShipCity",
                    title: "Ship City"
                }
            ]
        }).data("kendoGrid");

 $("#btnExport").click(function(e) {

 var dataSource =  $("#grid").data("kendoGrid").dataSource; 
     var filteredDataSource = new kendo.data.DataSource( { 
         data: dataSource.data(), 
         filter: dataSource.filter() 
     }); 

     filteredDataSource.read();
     var data = filteredDataSource.view();

     var result = "data:application/vnd.ms-excel,";

     result += "<table><tr><th>OrderID</th><th>Freight</th><th>Order Date</th><th>Ship Name</th><th>Ship City</th></tr>";

     for (var i = 0; i < data.length; i++) {
         result += "<tr>";

         result += "<td>";
         result += data[i].OrderID;
         result += "</td>";

         result += "<td>";
         result += data[i].Freight;
         result += "</td>";

         result += "<td>";
         result += kendo.format("{0:MM/dd/yyyy}", data[i].OrderDate);
         result += "</td>";

         result += "<td>";
         result += data[i].ShipName;
         result += "</td>";

         result += "<td>";
         result += data[i].ShipCity;
         result += "</td>";

         result += "</tr>";
     }

     result += "</table>";
     if (window.navigator.msSaveBlob) {
            window.navigator.msSaveBlob(new Blob([result]),'export.csv');
        } else {
            window.open(result);
        }


     e.preventDefault();
});
});
</script>

演示http://jsfiddle.net/SZBrt/236/

答案 1 :(得分:0)

您可以制作控制器方法并按如下方式传递网格的请求

public void Export([DataSourceRequest] DataSourceRequest gridRequest){
    // your logic goes here
}