現在、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");
}