I am trying to send my filters from a grid in order to export an excel file with the filters applied. I am new to kendo, and I am having difficulties sending the filters,page,sorting to the controller. Here is what i have so far :
Controller
public JsonResult List([DataSourceRequest] DataSourceRequest request)
{
//generating list to send to the grid
return Json(list.ToDataSourceResult(request), JsonRequestBehavior.AllowGet);
}
View
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();
}
And this is my JS function for getting parameters(I saw this piece of code somewhere else, and I am trying to adapt it, here is where i think i need help)
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);
}
I just want to send the filters to the controller. Can you help me ?