How to resize an ExtJS Panel, Grid, Component on Window Resize without using Ext.Viewport
Join the DZone community and get the full member experience.
Join For FreeThis post will walk through how to resize an ExtJS Panel, Grid, Component on Window Resize without using Ext.Viewport.
Problem:
You have a legacy page and you want to change an html grid for an ExtJS DataGrid, because it has so many cool features. Or you have a page with some design and you are going to use only one ExtJS Component. In both cases, you also want to render your ExtJS Component to a specific DIV. Also, you want you component to be resized in case you resize the browser window.
How can you do that if resize a single component in an HTML page it is not the default behavior of an ExtJS Component (except if you use Ext.Viewport)?
Solution:
Condor (from ExtJS Community Support Team) developed a plugin that can do that for you.
I had to spend some time to understand how the plugin works, and I finally got it working as I wanted.
Well, I recommend you to spend some time reading this thread: http://www.sencha.com/forum/showthread.php?28318 (if you have any issues or questions, please publish it on the thread, so other members can give you the support you need).
Requirements to make the plugin work:
Your have to apply the following style to the DIV (the width is up to you, the other styles are mandatory, otherwise it will not work):
<div id="reportTabContent" style="overflow: hidden; position:absolute; width:98%;"></div>
If you have any border around your ExtJS component, you have to set a HEIGHT. And you will also have to set a height to your ExtJS component. In this case, autoHeight will not work. If you DO NOT have any border or other design on the ExtJS component side, you do not need to set height and you can use autoHeight. In my case, I put a border on the external DIV, so I have to set Height:
<div style="border:3px solid #000000; padding:1px; width:99%; height:500px;">
HTML code (all DIVs):
<div style="border:3px solid #000000; padding:1px; width:99%; height:500px;"> <table> <tr> <td style="overflow: hidden; width:100%;"> <div id="reportTabContent" style="overflow: hidden; position:absolute; width:98%;"></div> </td> </tr> </table> </div>
And you need to add the plugin to the component (In this case, I’m using an ExtJS DataGrid):
var grid = new Ext.grid.GridPanel({
store: store,
columns: [
{header: 'Company', width: 160, sortable: true, dataIndex: 'company'},
{header: 'Price', width: 75, sortable: true, renderer: 'usMoney', dataIndex: 'price'},
{header: 'Change', width: 75, sortable: true, renderer: change, dataIndex: 'change'},
{header: '% Change', width: 75, sortable: true, renderer: pctChange, dataIndex: 'pctChange'},
{header: 'Last Updated', width: 85, sortable: true, renderer: Ext.util.Format.dateRenderer('m/d/Y'), dataIndex: 'lastChange'}
],
stripeRows: true,
autoExpandColumn: 'company',
height: 490,
autoWidth:true,
title: 'Array Grid',
// config options for stateful behavior
stateful: true,
stateId: 'grid'
,viewConfig:{forceFit:true}
,renderTo: 'reportTabContent' // render the grid to the specified div in the page
,plugins: [new Ext.ux.FitToParent("reportTabContent")]
});
And done! Now you can resize the browser and the component will resize itself!
I tested it on Firefox, Chrome and IE6.
You can download my sample project from my GitHub: http://github.com/loiane/extjs-fit-to-parent
PS.: If you want to use the full browser window, use a Viewport.
Happy coding!
Published at DZone with permission of Loiane Groner, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments