Export React FlexGrid with cell templates

Posted by: jschnurer on 30 October 2024, 1:56 pm EST

    • Post Options:
    • Link

    Posted 30 October 2024, 1:56 pm EST

    I am trying to export a FlexGrid to Excel. I found this example from a previous request:

    https://developer.mescius.com/forums/wijmo/export-excel-questions

    Which links to this example code:

    https://stackblitz.com/edit/angular-faqpxm?file=src%2Fapp%2Fgrid-exporter.ts%3AL132

    Using this seems to work mostly OK but it throws an exception when calling _instantiateTemplate(…) in the grid-exporter.ts file on line 142.

    let insTemp = template._instantiateTemplate(e.cell, context);

    The error thrown is template._instantiateTemplate is not a function.

    When this fails, the grid is still exported to a spreadsheet but the custom cell templates are not handled so the raw data is exported instead.

    If this function was removed in the newer version of the flex grid, what is the new alternative way to instantiate templates for use while exporting the grid?

  • Posted 31 October 2024, 7:54 am EST

    Hi,

    We are not able to replicate the issue on our end, it might be possible that there have been some internal changes in the product due to which the console errors are observed in the Wijmo version you are using as the sample you are referring was prepared for a very old Wijmo version. However, here’s the same sample updated with the latest Wijmo version for your reference - https://stackblitz.com/edit/angular-ivy-qfky8h?file=src%2Fapp%2Fapp.component.ts

    Could you please share a small sample in which the issue can be replicated, so that we can test it on our end and assist you accordingly? You can also modify the above sample to replicate the issue and share it with us. Also, please share some more information about your project-specific requirements, if any, for example, what type of data is to be exported or if you have any custom implementations in grid with cell templates or anything that we should know about.

    Here’s another demo sample for Excel Export on our demo site, please take a look as that could be useful - https://developer.mescius.com/wijmo/demos/Grid/ImportExportPrint/Excel/Async/angular

    In case, if there is something we missed, please let us know.

    Regards

  • Posted 31 October 2024, 9:53 am EST

    I have created a small app to replicate the issue using the my code (slightly modified). Normally, the grids in my app contact an OData endpoint but in this example they simply load an array of data. The sample application is attached here:

    wijmo-grid-example.zip

    To run it, execute npm run dev in the folder which will run the vite app.

    In App.tsx, I set up the configuration for the grid by defining a list of columns, etc. My sample data are objects that match this interface:

    interface IItem {
      id: number,
      title: string,
      assignedTo: string,
      openIssues: number,
      closedIssues: number,
      totalIssues: number,
      hasOpenIssues: boolean,
    }

    When you check the App.tsx grid column configuration, you will see that I have defined some cell templates:

    • The title column renders the title and adds an asterisk at the end if hasOpenIssues is true.
    • The assignedTo column adds “(User)” to the end of the assignedTo value.
    • The issues column is not bound to any of the fields and instead renders a span that contains “openIssues / closedIssues (totalIssues)”. This span is pink if there are any open issues.
    • The hasOpenIssues column is of type boolean. Inside WijmoGrid.tsx, the component automatically adds a cell template which changes the displayed value to either Yes or No depending on the boolean value.

    Above the grid you will find a button labeled “Export Grid”. Clicking this dispatches a redux action which triggers the WijmoGridExporter component. This component then executes the “export to Excel” functionality of the grid. When it does so, an error is thrown for every item when it tries to instantiate the cell template (WijmoGridExporter.ts line 134). The code will log to the output console each of these errors.

    The file is exported successfully regardless of the errors. However, all of the cell templates were ignored. The title does not have the extra asterisk, the assignedTo is missing the “(User)”, and the issues column is completely blank. However, the hasOpenIssues column correctly used its template to output “Yes” and “No”.

    What must I do to my sample app to have the export to Excel functionality respect my cell templates, correctly instantiate them, and include them in the output?

  • Posted 4 November 2024, 8:35 am EST

    Hi Justin,

    Thank you for sharing the sample. On investigating the sample we found that, the ‘_instantiateTemplate’ method is only available for CellTemplates in Wijmo angular interops, and not available for React packages. The internal processing of cell templates in React is different from angular templates. Hence, this issue occurs.

    We are looking for a suitable solution for your use case. It may take some time, as we have to go through the internal functions and rendering process of cell templates in React. We will share an update with you as soon as possible.

    Regards

  • Posted 5 November 2024, 3:07 am EST

    Hi,

    Instead of using the ‘_instantiateTemplate’ method, you can use the ‘renderToString’ method in React to extract the rendered HTML from the cell templates and assign it directly to the ‘innerHTML’ property of the cell. Please refer to the attached sample demonstrating the same.

    Please note that, you’ll also have to use a custom formatItem function for export to add cellTempltes data in the exported file, as by default cell templates will not be exported to the Excel file. We have modified the above sample to export the applied template and style for the ‘Issues’ column in the grid. You can make adjustments as per your needs.

    In case, you face any issues, please let us know.

    Regards

    wijmo-grid-example.zip

Need extra support?

Upgrade your support plan and get personal unlimited phone support with our customer engagement team

Learn More

Forum Channels