Printing grids with Ext JS

Grids 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):

var grid = new Ext.grid.GridPanel({
  store  : store,
  columns: [
      {header: "Company",      width: 160, dataIndex: 'company'},
      {header: "Price",        width: 75,  dataIndex: 'price', renderer: 'usMoney'},
      {header: "Change",       width: 75,  dataIndex: 'change'},
      {header: "% Change",     width: 75,  dataIndex: 'pctChange'}
      {header: "Last Updated", width: 85,  dataIndex: 'lastChange', renderer: Ext.util.Format.dateRenderer('m/d/Y')}
  ],
  title:'Array Grid',
  tbar : [
    {
      text   : 'Print',
      iconCls: 'print',
      handler: function() {
        Ext.ux.GridPrinter.print(grid);
      }
    }
  ]
});

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:

  //add this before you call Ext.ux.GridPrinter.print
  Ext.ux.GridPrinter.stylesheetPath = '/some/other/path/gridPrint.css';

Finally, here is some CSS I’ve used to achieve a grid-like display on the printable page:

html,body,div,dl,dt,dd,ul,ol,li,h1,h2,h3,h4,h5,h6,pre,form,fieldset,input,p,blockquote,th,td{margin:0;padding:0;}
img,body,html{border:0;}
address,caption,cite,code,dfn,em,strong,th,var{font-style:normal;font-weight:normal;}
ol,ul {list-style:none;}caption,th {text-align:left;}h1,h2,h3,h4,h5,h6{font-size:100%;}q:before,q:after{content:'';}

table {
  width: 100%;
  text-align: left;
  font-size: 11px;
  font-family: arial;
  border-collapse: collapse;
}

table th {
  padding: 4px 3px 4px 5px;
  border: 1px solid #d0d0d0;
  border-left-color: #eee;
  background-color: #ededed;
}

table td {
  padding: 4px 3px 4px 5px;
  border-style: none solid solid;
  border-width: 1px;
  border-color: #ededed;
}

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.

60 Responses to Printing grids with Ext JS

  1. Pingback: ExtJS 4: Plugin para Impressão de Grids - GridPrinter | Loiane Groner

  2. Andrés Serrón says:

    Clean, well made.
    I was about coding something that builds a printable markup and then you come with this clean solution.

    Some ideas.
    – Open the solution so we can contribute with more renderer extensions.
    – Think about a plug in version.
    – Meta tags parsing in template: model external html, include it and compile it out of script.

    what you think?

    Pd: thanks for your work.

  3. Mir says:

    Hello,

    Is it possible to print exact structure if grid contains grouping.

    I mean that I need to print after I have grouped the data by certain columns.

  4. masoud says:

    i used it , but it doesn’t work for mine . error is :
    me.owner.up(“tablepanel”) is undefined

  5. James says:

    Thank you for this!

    I commented out these 2 lines so our editor can print or copy/paste big table to excel.

    win.print();
    win.close();

    One drawback of my change is that the page keeps loading.

    James

  6. Beto Lima says:

    Pessoal, usando este plugin foram encontrados alguns bugs que acredito estar ok com algumas correções que foram feitas:
    https://github.com/betolima/extjs4-ux-gridprinter

  7. Is there a way to print Grouped grids with summary ?

    The example above is for normal grids, which works awesome.

  8. jacobs says:

    In a grid I have 10 columns.but i only want to print any five how can i do this

  9. Sascha says:

    Is it possible to change the outpu of a date? for example: dd.mm.YYYY

  10. NISH says:

    is it possible to print only grid view in extjs?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: