目前对csv的导出工作正常并且目前全部导出。反正我是否可以根据所选行将过滤器导出为导出?请指教。 OnChange事件它将ID传递给控制器。但我只需要找出按钮导出是否单击,它还会点击OnChange事件
查看:
@(Html.Kendo().Grid<HAp.Models.SessionModel>()
.Name("List")
.HtmlAttributes(new { @Style = "align:center; font-size:10.5px; length:100%" })
.Columns(columns =>
{
columns.Bound(p => p.ReftID).Visible(false);
columns.Bound(p => p.CreateDate);
columns.Bound(p => p.CreateBy)
columns.Bound(p => p.Description);
})
.ToolBar(toolbar => toolbar.Create())
.ToolBar(toolBar =>
toolBar.Custom()
.Text("Export To CSV")
.HtmlAttributes(new { id = "export" })
.Url(Url.Action("Export", "Index", new { page = 1, pageSize = "~", filter = "~", sort = "~" }))
)
.Editable(editable => editable.Mode(GridEditMode.InLine))
.Selectable(s => s.Mode(Kendo.Mvc.UI.GridSelectionMode.Single))
.Pageable()
.Scrollable()
.DataSource(dataSource => dataSource
.Ajax()//bind with Ajax instead server bind
.PageSize(10)
.ServerOperation(true)
.Model(model => model.Id(p => p.ReftID))
.
.Events(ev => ev.DataBound("onDataBound").Change("onChange"))
)
function onChange(e) {
var grid = $('#List').data('kendoGrid'); //get a reference to the grid data
var record = grid.dataItem(grid.select()); //get a reference to the currently selected row
var GuidID = record.ReftID;
$.ajax({
type: 'POST',
url: "@(Url.Content("~/Test/Export/"))",
data: {
"ID": GuidID
}
});
}
function onDataBound(e) {
var grid = $('#List').data('kendoGrid');
// ask the parameterMap to create the request object for you
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 = $('#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._pageSize);
//update filter descriptor with the filters applied
href = href.replace(/filter=([^&]*)/, 'filter=' + (requestObject.filter || '~'));
// Update the 'href' attribute
$exportLink.attr('href', href);
}
Controller:
public FileResult Export([DataSourceRequest]DataSourceRequest request)
{
IEnumerable list = db.GetAll();
MemoryStream output = new MemoryStream();
StreamWriter writer = new StreamWriter(output, Encoding.UTF8);
writer.Write("Ref ID,");
writer.Write("Create Date,");
writer.Write("Create By,");
writer.Write("List Description,");
writer.Write("List Status,");
writer.WriteLine();
foreach (vwListDetail lists in list)
{
writer.Write(lists .RefID);
writer.Write(",");
writer.Write(lists .CreateDate.Value.ToShortDateString());
writer.Write(",");
writer.Write(lists .CreatedBy);
writer.Write(",");
writer.Write(lists .ListDescription);
writer.Write(",");
writer.WriteLine();
}
writer.Flush();
output.Position = 0;
return File(output, "text/comma-separated-values", "List.csv");
}
答案 0 :(得分:0)
你可以试试这个:
IEnumerable list = db.GetAll.ToDataSourceResult(request).Data;
此代码应进行任何分页,排序或过滤。