Exporting Filtered Data Only

17 Feb 20223 minutes to read

You can export the filtered data by defining the resulted data in dataSource property of PdfExportProperties before export.

In the below Pdf exporting demo, We have gotten the filtered data by applying filter query to the grid data and then defines the resulted data in dataSource property and pass it to PdfExport method.

@Html.EJS().Grid("Grid").DataSource((IEnumerable<object>)ViewBag.DataSource).AllowPdfExport().ToolbarClick("toolbarClick").Columns(col =>
{
   col.Field("OrderID").HeaderText("Order ID").Width("120").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Add();
   col.Field("CustomerID").HeaderText("Customer Name").Width("150").Add();
   col.Field("Freight").HeaderText("Freight").Width("120").Format("C2").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Add();
   col.Field("ShipCountry").HeaderText("Ship Country").Width("120").Add();

}).AllowPaging().PageSettings(page => page.PageSize(5).PageCount(5)).AllowFiltering().Toolbar(new List<string>() { "PdfExport" }).Render()
   
<script>
    function toolbarClick(args) {
        var gridObj = document.getElementById("Grid").ej2_instances[0];
        if (args.item.id === 'Grid_pdfexport') {
            var pdfdata;
            var query = gridObj.renderModule.data.generateQuery(); // get grid corresponding query  
            for (var i = 0; i < query.queries.length; i++) {
                if (query.queries[i].fn == 'onPage') {
                    query.queries.splice(i, 1);     // remove page query to get all records
                    break;
                }
            }
            var data = @Html.Raw(Json.Encode(ViewBag.DataSource));
            var fdata = new ej.data.DataManager({ json: data }).executeQuery(query).then((e) => {
                pdfdata = e.result;   // get all filtered records
                var exportProperties = {
                    dataSource: pdfdata,
                };
                gridObj.pdfExport(exportProperties);
            }).catch((e) => true);
        }
    }
</script>
public IActionResult Index()
{
    ViewBag.DataSource = OrderDetails.GetAllRecords();
    return View();
}