Setting the requested row count on a Spark VGroup container in Flex 4

The following example shows how you can set the requested row count on a Spark VGroup container in Flex 4 by setting the requestedRowCount property.

Full code after the jump.

The following example(s) require Flash Player 10 and the Adobe Flex 4 SDK. To download the Adobe Flash Builder 4 trial, see www.adobe.com/products/flex/. To download the latest nightly build of the Flex 4 SDK, see opensource.adobe.com/wiki/display/flexsdk/Download+Flex+4.

For more information on getting started with Flex 4 and Flash Builder 4, see the official Adobe Flex Team blog.

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2009/09/10/setting-the-requested-row-count-on-a-spark-vgroup-container-in-flex-4/ -->
<s:Application name="Spark_VGroup_requestedRowCount_test"
        xmlns:fx="http://ns.adobe.com/mxml/2009"
        xmlns:s="library://ns.adobe.com/flex/spark"
        xmlns:mx="library://ns.adobe.com/flex/halo">
    <s:controlBarContent>
        <mx:Form>
            <mx:FormItem label="requestedRowCount:">
                <s:HSlider id="sldr"
                        minimum="0"
                        maximum="7"
                        value="{vGr.numElements}" />
            </mx:FormItem>
        </mx:Form>
    </s:controlBarContent>
 
    <s:Panel id="pnl"
            title="rowCount={vGr.rowCount}"
            width="250"
            horizontalCenter="0" verticalCenter="0">
        <s:VGroup id="vGr"
                requestedRowCount="{sldr.value}"
                clipAndEnableScrolling="true"
                width="100%">
            <s:Border backgroundColor="red" width="100%" height="30" />
            <s:Border backgroundColor="haloOrange" width="100%" height="30" />
            <s:Border backgroundColor="yellow" width="100%" height="30" />
            <s:Border backgroundColor="haloGreen" width="100%" height="30" />
            <s:Border backgroundColor="haloBlue" width="100%" height="30" />
        </s:VGroup>
        <s:controlBarContent>
            <mx:Form styleName="plain" backgroundAlpha="0.0">
                <mx:FormItem label="firstIndexInView:">
                    <s:Label text="{vGr.firstIndexInView}" />
                </mx:FormItem>
                <mx:FormItem label="lastIndexInView:">
                    <s:Label text="{vGr.lastIndexInView}" />
                </mx:FormItem>
            </mx:Form>
        </s:controlBarContent>
    </s:Panel>
 
</s:Application>

You can also set the requestedRowCount property using ActionScript, as seen in the following example:

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2009/09/10/setting-the-requested-row-count-on-a-spark-vgroup-container-in-flex-4/ -->
<s:Application name="Spark_VGroup_requestedRowCount_test"
        xmlns:fx="http://ns.adobe.com/mxml/2009"
        xmlns:s="library://ns.adobe.com/flex/spark"
        xmlns:mx="library://ns.adobe.com/flex/halo">
    <s:controlBarContent>
        <mx:Form>
            <mx:FormItem label="requestedRowCount:">
                <s:HSlider id="sldr"
                        minimum="0"
                        maximum="7"
                        value="{vGr.numElements}"
                        change="sldr_change(event);"/>
            </mx:FormItem>
        </mx:Form>
    </s:controlBarContent>
 
    <fx:Script>
        <![CDATA[
            protected function sldr_change(evt:Event):void {
                vGr.requestedRowCount = sldr.value;
            }
        ]]>
    </fx:Script>
 
    <s:Panel id="pnl"
            title="rowCount={vGr.rowCount}"
            width="250"
            horizontalCenter="0" verticalCenter="0">
        <s:VGroup id="vGr"
                clipAndEnableScrolling="true"
                width="100%">
            <s:Border backgroundColor="red" width="100%" height="30" />
            <s:Border backgroundColor="haloOrange" width="100%" height="30" />
            <s:Border backgroundColor="yellow" width="100%" height="30" />
            <s:Border backgroundColor="haloGreen" width="100%" height="30" />
            <s:Border backgroundColor="haloBlue" width="100%" height="30" />
        </s:VGroup>
        <s:controlBarContent>
            <mx:Form styleName="plain" backgroundAlpha="0.0">
                <mx:FormItem label="firstIndexInView:">
                    <s:Label text="{vGr.firstIndexInView}" />
                </mx:FormItem>
                <mx:FormItem label="lastIndexInView:">
                    <s:Label text="{vGr.lastIndexInView}" />
                </mx:FormItem>
            </mx:Form>
        </s:controlBarContent>
    </s:Panel>
 
</s:Application>

Due to popular demand, here is the “same” example in a more ActionScript friendly format:

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2009/09/10/setting-the-requested-row-count-on-a-spark-vgroup-container-in-flex-4/ -->
<s:Application name="Spark_VGroup_requestedRowCount_test"
        xmlns:fx="http://ns.adobe.com/mxml/2009"
        xmlns:s="library://ns.adobe.com/flex/spark"
        xmlns:mx="library://ns.adobe.com/flex/halo"
        initialize="init();">
 
    <fx:Script>
        <![CDATA[
            import mx.containers.Form;
            import mx.containers.FormItem;
            import mx.core.FlexGlobals;
 
            import spark.components.Border;
            import spark.components.HSlider;
            import spark.components.Label;
            import spark.components.Panel;
            import spark.components.VGroup;
 
            protected var pnl:Panel;
            protected var sldr:HSlider;
            protected var vGr:VGroup;
            protected var firstIndexLabel:Label;
            protected var lastIndexLabel:Label;
 
            private function init():void {
                sldr = new HSlider();
                sldr.minimum = 0;
                sldr.maximum = 7;
                sldr.addEventListener(Event.CHANGE, sldr_change);
 
                var formItem:FormItem = new FormItem();
                formItem.label = "requestedRowCount:";
                formItem.addElement(sldr);
 
                var form1:Form = new Form();
                form1.addElement(formItem);
 
                Application(FlexGlobals.topLevelApplication).controlBarContent = [form1];
 
                var brdr1:Border = new Border();
                brdr1.setStyle("backgroundColor", "red");
                brdr1.percentWidth = 100;
                brdr1.height = 30;
 
                var brdr2:Border = new Border();
                brdr2.setStyle("backgroundColor", "haloOrange");
                brdr2.percentWidth = 100;
                brdr2.height = 30;
 
                var brdr3:Border = new Border();
                brdr3.setStyle("backgroundColor", "yellow");
                brdr3.percentWidth = 100;
                brdr3.height = 30;
 
                var brdr4:Border = new Border();
                brdr4.setStyle("backgroundColor", "haloGreen");
                brdr4.percentWidth = 100;
                brdr4.height = 30;
 
                var brdr5:Border = new Border();
                brdr5.setStyle("backgroundColor", "haloBlue");
                brdr5.percentWidth = 100;
                brdr5.height = 30;
 
                vGr = new VGroup();
                vGr.clipAndEnableScrolling = true;
                vGr.percentWidth = 100;
                vGr.addElement(brdr1);
                vGr.addElement(brdr2);
                vGr.addElement(brdr3);
                vGr.addElement(brdr4);
                vGr.addElement(brdr5);
 
                firstIndexLabel = new Label();
 
                lastIndexLabel = new Label();
 
                var formItem1:FormItem = new FormItem();
                formItem1.label = "firstIndexInView:";
                formItem1.addElement(firstIndexLabel);
 
                var formItem2:FormItem = new FormItem();
                formItem2.label = "lastIndexInView:";
                formItem2.addElement(lastIndexLabel);
 
                var form2:Form = new Form();
                form2.styleName = "plain";
                form2.setStyle("backgroundAlpha", 0.0);
                form2.addElement(formItem1);
                form2.addElement(formItem2);
 
                pnl = new Panel();
                pnl.width = 250;
                pnl.horizontalCenter = 0;
                pnl.verticalCenter = 0;
                pnl.controlBarContent = [form2];
                pnl.addElement(vGr);
                addElement(pnl);
 
                callLater(updateLabels);
            }
 
            protected function sldr_change(evt:Event):void {
                vGr.requestedRowCount = sldr.value;
                callLater(updateLabels);
            }
 
            protected function updateLabels():void {
                pnl.title = "rowCount=" + vGr.rowCount.toString();
                firstIndexLabel.text = vGr.firstIndexInView.toString();
                lastIndexLabel.text = vGr.lastIndexInView.toString();
            }
        ]]>
    </fx:Script>
 
</s:Application>

This entry is based on a beta version of the Flex 4 SDK and therefore is very likely to change as development of the Flex SDK continues. The API can (and will) change causing examples to possibly not compile in newer versions of the Flex 4 SDK.