发送 kendoGrid 参数(过滤器、排序)进行文件下载



我正在尝试从网格发送过滤器,以便导出应用过滤器的 excel 文件。我是剑道的新手,在将过滤器,页面,排序发送到控制器时遇到困难。这是我到目前为止所拥有的:

控制器

public JsonResult List([DataSourceRequest] DataSourceRequest request)
    {
        //generating list to send to the grid
        return Json(list.ToDataSourceResult(request), JsonRequestBehavior.AllowGet);
    }

视图

Html.Kendo().Grid(Model)
    .Name("tokenList")
    .Columns(columns =>
    {
     // columns for the model   
    })
    .DataSource(d => d.Ajax()
        .ServerOperation(true)
        .Read(read => { read.Action("List", "Settings", null); })
    )
    .ToolBar(toolbar =>
    {
        toolbar.Template(
            @<text>
                         <a href="@Url.Action("ExportToExcel", "Settings", new { page = 1, pageSize = "~", filter = "~", sort = "~" })" class="export" onclick="onDataBound()">EXPORT</a>

             </text>
            );
    })
    .DefaultSetupForApp(<-this adds filtering, sortable, pageable).
Render(); 
}

这是我用于获取参数的 JS 函数(我在其他地方看到了这段代码,我正在尝试改编它,这是我认为我需要帮助的地方)

function onDataBound(e) {
    var grid = this;
    // ask the parameterMap to create the request object for you
    **I need some info on this part, nothing i found was very helpful**
    var requestObject = (new kendo.data.transports["aspnetmvc-server"]({ prefix: "" }))
    .options.parameterMap({
        page: grid.dataSource.page(),
        sort: grid.dataSource.sort(),
        filter: grid.dataSource.filter()
    });

    // Get the export link as jQuery object
    var $exportLink = grid.element.find('.export');
    // Get its 'href' attribute - the URL where it would navigate to
    var href = $exportLink.attr('href');
    // Update the 'page' parameter with the grid's current page
    href = href.replace(/page=([^&]*)/, 'page=' + requestObject.page || '~');
    // Update the 'sort' parameter with the grid's current sort descriptor
    href = href.replace(/sort=([^&]*)/, 'sort=' + requestObject.sort || '~');
    // Update the 'pageSize' parameter with the grid's current pageSize
    href = href.replace(/pageSize=([^&]*)/, 'pageSize=' + grid.dataSource.total());
    //update filter descriptor with the filters applied
    href = href.replace(/filter=([^&]*)/, 'filter=' + (requestObject.filter || '~'));
    // Update the 'href' attribute
    $exportLink.attr('href', href);
}

我只想将过滤器发送到控制器。你可以帮我吗?

我设法做到了。我所要做的就是给var grid来纠正网格,就像这样:

var grid= $('#tokensList').data('kendoGrid');

我这样做:

这是我的网格工具栏模板。

    <a class="k-button" id="export" href="Equipment/Export?page=~&amp;pageSize=~&amp;filter=~&amp;sort=~" title="Export to Excel">
        <span class="k-print" />
    </a>

如果有任何问题,请告诉我。

最新更新