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

Pass parameter to Read Method during Exports

2 Answers 229 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Darron
Top achievements
Rank 1
Darron asked on 08 Oct 2019, 10:35 PM

A common problem with exporting to excel is you often may have columns that contain HTML or other content that doesn't work well with Excel.

A GENIUS friend of mine suggested that we just change the grid read when exporting so that it only provides the HTML or other non-friendly-to-Excel content during reads that aren't for an excel export.  Thank you, Crankshaw!  Here's how it works.

Just create a global parameter in javascript to identify when you're doing an excel export.  Pass that parameter to the grid's Read.

Then give your standard excel export button an ID, so that you can tie events to it other than the normal excel export. 

// toolbar.Excel().HtmlAttributes(new { id = "WIPDetailToXL" })

Then create two events in javascript. 

NOTE:  MouseDown happens prior to the Click event, but so does MouseUp.  So use MouseDown and MouseOut, both on the ExcelExport button.

So when you Click to export, the mousedown will set the variable to let your read know that this is an "Excel Read".  This happens BEFORE the export.  And when you move your cursor off the Export button, the variable will change back so that subsequent reads will render your html correctly within your grid.

<script>
    var exportExcelFlag = 0;

    $("#WIPDetailToXL").mousedown(function (e) {
        exportExcelFlag = 1;
    });

    $("#WIPDetailToXL").mouseout(function (e) {
        exportExcelFlag = 0;
    });

</script>

Hope this helps someone as much as it did me!

2 Answers, 1 is accepted

Sort by
0
Martin
Telerik team
answered on 10 Oct 2019, 02:56 PM

Hello Darron,

Thank you very much for sharing this information! I really hope it will be useful to others as well. We are always happy when users exchange ideas about different scenarios. 

Feel free to contact us whenever you have Kendo related questions.

Regards,
Martin
Progress Telerik

Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
0
Darron
Top achievements
Rank 1
answered on 10 Oct 2019, 04:22 PM

UPDATE:  

Some browsers are now preferring "pointerdown" to "mousedown".  Go figure.  So check for this in your onload function and add event listeners for either pointerdown or mousedown, whichever is preferred by the current browser.

if (window.PointerEvent) { myButton.addEventListener('pointerdown', RunsThisFunction); } else { myButton.addEventListener('mousedown', RunsThisFunction); }

Tags
Grid
Asked by
Darron
Top achievements
Rank 1
Answers by
Martin
Telerik team
Darron
Top achievements
Rank 1
Share this question
or