Sharp Kid
Sharp Kid

Reputation: 135

ExtJs:How to set a width in percentage?

My test code shows below:

<script type="text/javascript">
    Ext.onReady(function() {
        Ext.create('Ext.button.Button', {
            id: "Button",
            renderTo: Layer
        });
    });

    function SetPixelWidth() {
        var button = Ext.getCmp("Button");
        button.setWidth(200);
    }

    function SetPercentageWidth() {
        var button = Ext.getCmp("Button");
        button.setWidth("50%");
    }
</script>

<button onclick="SetPixelWidth()">Set Width(Pixel)</button>
<button onclick="SetPercentageWidth()">Set Width(Percentage)</button>
<div id="Layer" style="width:100%;background:black"></div>

setWidth(200) works fine, but "setWidth("50%")" don't take effect.

How to set a width in percentage or other units over pixel?

Thank you for any help in advance!

Upvotes: 4

Views: 4054

Answers (1)

Cavyn VonDeylen
Cavyn VonDeylen

Reputation: 4249

See this post on the Sencha forums

The best you can do in this situation is grab the width of the containing object. So something like

function SetPercentageWidth()
{
    var button = Ext.getCmp("Button");
    button.setWidth(button.ownerCt.getWidth() * .5);
}

Upvotes: 1

Related Questions