Flex项呈示器 - 如果为null,则不包含元素?

时间:2011-04-07 04:34:46

标签: flex formatting mxml itemrenderer

我有像这样的项目渲染器代码:

<s:HGroup>
    <s:Label text="{data.DateTime}"/>

    <s:VGroup>
        <s:Label text="{data.Description}"/>
        <s:Label text="{data.Amount}"/>
    </s:VGroup>
</s:HGroup>

描述是一个可选字段..我想如果Amount字段的Description字段为空,则向上移动,但是现在只有一个空格。有没有办法在mxml中实现这一点?我希望它们位于不同的字段中,因为我打算使“描述”可编辑,但数量已修复。

1 个答案:

答案 0 :(得分:3)

我编写了一个模拟Item渲染器行为的简单应用程序。 诀窍是使用可见 includeInLayout 属性:

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
               xmlns:s="library://ns.adobe.com/flex/spark" 
               xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600"
               creationComplete="application1_creationCompleteHandler(event)"
               >

    <fx:Script>
        <![CDATA[
            import mx.charts.DateTimeAxis;
            import mx.events.FlexEvent;

            [Bindable]
            private var data:Object;

            protected function application1_creationCompleteHandler(event:FlexEvent):void
            {
                var newData:Object = new Object();
                newData.DateTime = new Date();
                newData.Description = "Description";
                newData.Amount = 12345;
                data = newData;
            }           


            protected function setNull_clickHandler(event:MouseEvent):void
            {
                var newData:Object = new Object();
                newData.DateTime = new Date();
                newData.Description = null;
                newData.Amount = 12345;
                data = newData;

            }


            protected function setValue_clickHandler(event:MouseEvent):void
            {
                var newData:Object = new Object();
                newData.DateTime = new Date();
                newData.Description = "Description";
                newData.Amount = 12345;
                data = newData;
            }

        ]]>
    </fx:Script>


    <fx:Declarations>
        <!-- Place non-visual elements (e.g., services, value objects) here -->
    </fx:Declarations>


    <s:VGroup>
        <s:HGroup>
            <s:Button id="setNull" label="Set Null" click="setNull_clickHandler(event)"/>
            <s:Button id="setValue" label="Set Description" click="setValue_clickHandler(event)"/>
        </s:HGroup>
        <s:Label text="Renderer"/>      
        <s:HGroup>
            <s:Label text="{data.DateTime}"/>
            <s:VGroup>
                <s:Label text="{data.Description}"
                         visible="{data.Description != null}"
                         includeInLayout="{data.Description != null}"
                         />
                <s:Label text="{data.Amount}" />
            </s:VGroup>
        </s:HGroup> 
    </s:VGroup> 
</s:Application>