This is a migrated thread and some comments may be shown as answers.

Export to excel with a filtered datasource

1 Answer 969 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Thomas
Top achievements
Rank 1
Veteran
Thomas asked on 26 Nov 2020, 09:28 AM

Hi there,

I have a grid with an excel export. I'm filtering the data source of the grid in javascript via some drop down lists. The filtering is done via the server. To be clear the when the grid's data source has been instructed to read it scrapes values from the filters using the .data() method and sends the filters values with the read request.

When I export to excel I noticed that it always returns the initial amount of data bound to the grid i.e. I have three rows when the page loads, I filter down to one row then export to excel and it returns all three rows. I noticed that when the export was happening it seemed to be exporting whatever was in the datasource options.data. I've found a workaround which is 

var settings = {};
$(me.selectors.excelExport).on('click', function() {
    var $this = $(this),
        $grid = $this.closest(me.selectors.role),
        grid = $grid.data(me.name);
    if (grid) {
        settings = grid.dataSource.options.data;
        grid.dataSource.options.data = grid.dataSource.view();
    }
});

on clicking export to excel i change the grid's datasource.options.data to be the current "view" of data and i store the original data.options.data.

var grid = $(me.selectors.role).data(me.name);
if (grid) {
    grid.bind(me.events.excelExport, function(e) {
        grid.dataSource.options.data = settings;
    });
}

then after the export has finished I  then restore the datasource.options.data with the value i stored before.

Should I need to do this or am I misunderstanding something? 
Please let me know if you need additional information or if I've not been clear enough.

 

Thanks

Tom

1 Answer, 1 is accepted

Sort by
0
Anton Mironov
Telerik team
answered on 27 Nov 2020, 04:13 PM

Hi Thomas,

Thank you for the provided code snippets.

The used approach is a working decision. Thank you for sharing it!

In general, I would recommend using the Kendo built-in filtering, as it will work out of the box.

In the case of the pointed server-side filtering, the export functionality is looking for the entities in the current DataSource. This is expected and is the reason for the faulty behavior. 

As a workaround, I would recommend using a Custom Binding. This approach will give you the opportunity to filter the data as required in the Back End. And the export action will be applied as expected. Here is a demo of the custom Ajax binding:

Let me know if the provided information is helpful enough, or I should look for another solution.

Regards,
Anton Mironov
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Tags
Grid
Asked by
Thomas
Top achievements
Rank 1
Veteran
Answers by
Anton Mironov
Telerik team
Share this question
or