Professional thumbnails in seconds - completely free, no signup
Start creating freeAI-powered work documentation for career advancement
Start free demoGrids are one of the most widely used components in Ext JS, and often represent data that the user would like to print. As the grid is usually part of a wider application, simply printing the page isn't often a good solution.
You could attach a stylesheet with media="print", which hides all of the other items on the page, though this is rather application-specific, and a pain to update. It would be far better to have a reusable way of printing the data from any grid.
The way I went about this was to open up a new window, build a table containing the grid data into the new window, then print it and close. It's actually pretty simple, and with a bit of CSS we can even get the printable view looking like it does in the grid.
Here's how you use it (this is a slightly modified version of the Array Grid Example):
So we've just set up a simple grid with a print button in the top toolbar. The button just calls Ext.ux.GridPrinter.print, which does all the rest. The full source code that this example was based upon can be found at http://extjs.com/deploy/dev/examples/grid/array-grid.js.
The source for the extension itself is pretty simple (download it here):
If you look at the source above you'll see it includes a 'print.css' stylesheet, which can be used to style the printable markup. The GridPrinter expects this stylesheet to be available at /stylesheets/print.css, but this is easy to change:
Finally, here is some CSS I've used to achieve a grid-like display on the printable page:
This technique could easily be adapted to print any component that uses a store - DataViews, ComboBoxes, Charts - whatever. It just requires changing the generated markup and stylesheet.
Explore how the Ext.ux.Printer extends printing functionality to other components like trees, enhancing versatility in Ext JS applications. Additionally, learn how to export your grid data to Excel or CSV using Ext.ux.Exporter, offering users flexible data handling options.
Sometimes you'll be using a Paging Toolbar on a grid and need to give the user the ability to change