Auto-size Ext JS Window based on content, up to maxHeight
Asked Answered
D

8

15

Using Ext JS 4.0.2, I'm trying to open a window that automatically sizes itself big enough to fit its content, until it hits a height limit, at which point it stops getting bigger and shows a scroll bar.

Here's what I'm doing

Ext.create('widget.window', {
    maxHeight: 300,
    width: 250,
    html: someReallyBigContent,
    autoScroll: true,
    autoShow: true
});

When the window is first rendered, it's sized big enough for the really big content--bigger than the maxHeight should allow. If I attempt to resize it, then snaps down to the maxHeight of 300px.

How do I constrain the window to its maxHeight when it's initially rendered?

Discrimination answered 6/7, 2011 at 19:47 Comment(0)
T
9

I have exactly the same problem and for now I'm doing a litle dirty hack :)

this.on('afterrender', function() {
    if (this.getHeight() > this.maxHeight) {
        this.setHeight(this.maxHeight);
    }
    this.center();
}, this);

Depending on the content of the window, you must use the afterlayout event. Instead of using this.maxHeight, to use the whole viewport, use Ext.getBody().getViewSize().height or in vanilla JS use window.innerHeight.

This version will work even if the windows contains other components and not only huge html:

listeners: {afterlayout: function() {
    var height = Ext.getBody().getViewSize().height;
    if (this.getHeight() > height) {
        this.setHeight(height);
    }
    this.center();
}}
Toolmaker answered 15/12, 2011 at 10:40 Comment(2)
It looks like a setSize(null, null) does the job.Hovis
@Hovis setSize(null, null) does not work if the window does contain other components instead of simply html.Phippen
C
2

This can be better :

bodyStyle: { maxHeight: '100px' }, autoScroll: true,
Capsulize answered 7/12, 2015 at 19:58 Comment(0)
A
1

I don't see an out-of-the-box way to do this. However, you might try this approach:

Place the contents of the window into a container inside the window (i.e. make someReallyBigContent be inside a container.) On afterrender, get the height of that inner container and then proceed to set the height of the outer window based on that.

Adenectomy answered 15/12, 2011 at 19:42 Comment(0)
F
1

How I ended up displaying a window with an unknown amount of fields on a form (constrain = body.el):

prefForm.itemId = 'prefForm';
win = Ext.create('Ext.window.Window', {
    layout : {
        type : 'vbox',
        align : 'center'
    },
    buttons : buttons,
    maxHeight : constrain.dom.clientHeight - 50,
    title : title,
    items : prefForm,
    listeners : {
        afterrender : {
            fn : function(win) {
                var f = win.down('#prefForm');
                f.doLayout();
                var h = f.body.dom.scrollHeight;
                if (f.getHeight() > h)
                    h = f.getHeight();
                win.setHeight(h + 61);
                win.center();
            },
            single : true
        }
    }
});
Forbore answered 30/12, 2011 at 0:48 Comment(0)
F
0

You can add this config on your window

maximizable: true

if you want you could programmatically 'click' that button :)

Furunculosis answered 8/7, 2011 at 23:43 Comment(1)
Thanks. That's an interesting solution. I'd have to "click" the maximize button twice in this case: Once to maximize, then again to restore. And then I'd have to deal with the side-effect of having a maximize button that I may not have wanted. If I were to programmatically work around this issue, then something like win.setHeight(win.maxHeight); would be a little more straightforward.Discrimination
F
0

Now I see what you are trying to do. I think the only thing missing from your config is the height parameter. Set it to the same number as maxheight. That should do it, you won't need to call setHeight().

Furunculosis answered 18/7, 2011 at 6:35 Comment(1)
That works if the content is always too big. But what if sometimes it's smaller, and doesn't need the full height. In that case, I'd like the window to "shrink to fit".Discrimination
O
0

This is just like Ivan Novakov answer, except I prefer it when you override the onRender class for these types of classes.

This is for a couple of reasons. Removes an additional event listener, and in the case you have multiple things that need to occur at afterrender time. You can control the synchronization of these tasks.

onRender: function(ct,pos) {
    //Call superclass
    this.callParent(arguments);
    if (this.getHeight() > this.maxHeight) {
        this.setHeight(this.maxHeight);
    }
    this.center();
}
Orthoscope answered 4/2, 2014 at 20:4 Comment(0)
U
0

I had the little bit different problem. In my case ExtJS code there inside the HTML popup windows. And I had to achieve:
change the size of panel when we change the size of popup windows. Ivan Novakov's solution worked for me.

Ext.EventManager.onWindowResize(function () {

       var width = Ext.getBody().getViewSize().width - 20;
       var height = Ext.getBody().getViewSize().height - 20;
        myPanel.setSize(width, height);

});
Unto answered 18/3, 2015 at 12:8 Comment(1)
Welcome to Stack Overflow! This is really a comment and not an answer to the original question. To critique or request clarification from an author, leave a comment below their post - you can always comment on your own posts, and once you have sufficient reputation you will be able to comment on any post.Quincunx

© 2022 - 2024 — McMap. All rights reserved.