Custom Export

You can add additional rendering engines if you want to be able to export in different formats.

Here are the steps:

  1. Use AddRenderingEngine on the GridConfig or GridDefaults like so:
    .AddRenderingEngine("tabs", typeof(TabDelimitedRenderingEngine))
  2. Create the export url using the javascript api:
    $('#tabExportButton').click(function () {
        location.href = MVCGrid.getEngineExportUrl('CustomExport', 'tabs');
    });
        
Id First Name Last Name Status
370PatrickAdamsInactive
493KarenAdamsActive
570NancyAdamsInactive
599EricAdamsInactive
635ChristineAdamsInactive
663BonnieAdamsInactive
778RonaldAdamsInactive
820SeanAdamsInactive
897JudithAdamsActive
857MichaelAlexanderActive
652HowardAlexanderInactive
339JerryAlexanderInactive
55SusanAlexanderActive
190AmyAlexanderActive
250NicholasAllenActive
472DeniseAllenActive
171JessicaAlvarezInactive
127JanetAlvarezInactive
661MildredAlvarezInactive
855MarieAlvarezInactive
Showing 1 to 20 of 1000 entries

If you want to replace the default export, simply remove the RenderingEngine named export and add a different one of the same name, like this:

.RemoveRenderingEngine("export")
.AddRenderingEngine("export", typeof(TabDelimitedRenderingEngine))

Code

Javascript in View
$(function () {
	$('#csvExportButton').click(function () {
		location.href = MVCGrid.getExportUrl('CustomExport');
	});
	$('#tabExportButton').click(function () {
		location.href = MVCGrid.getEngineExportUrl('CustomExport', 'tabs');
	});
});
        
Inside MVCGridConfig.cs
MVCGridDefinitionTable.Add("CustomExport", new MVCGridBuilder<Person>(colDefauls)
    .AddRenderingEngine("tabs", typeof(TabDelimitedRenderingEngine))
    .WithAuthorizationType(AuthorizationType.AllowAnonymous)
    .AddColumns(cols =>
    {
        cols.Add("Id").WithSorting(false)
            .WithHtmlEncoding(false)
            .WithValueExpression((p, c) => c.UrlHelper.Action("detail", "demo", new { id = p.Id }))
            .WithValueTemplate("<a href='{Value}'>{Model.Id}</a>")
            .WithPlainTextValueExpression(p => p.Id.ToString());
        cols.Add("FirstName").WithHeaderText("First Name")
            .WithValueExpression(p => p.FirstName);
        cols.Add("LastName").WithHeaderText("Last Name")
            .WithValueExpression(p => p.LastName);
        cols.Add("Status").WithSortColumnData("Active")
            .WithHeaderText("Status")
            .WithValueExpression(p => p.Active ? "Active" : "Inactive");
    })
    .WithSorting(true, "LastName")
    .WithPaging(true, 20)
    .WithRetrieveDataMethod((context) =>
    {
        var options = context.QueryOptions;
        int totalRecords;
        var repo = DependencyResolver.Current.GetService<IPersonRepository>();
        string sortColumn = options.GetSortColumnData<string>();
        var items = repo.GetData(out totalRecords,
            options.GetLimitOffset(), options.GetLimitRowcount(),
            sortColumn, options.SortDirection == SortDirection.Dsc);
        return new QueryResult<Person>()
        {
            Items = items,
            TotalRecords = totalRecords
        };
    })
);