-4

I just started working with Flex. I know it's pathetic but it's a long story. Now, the problem I am facing is that I have a list component which has a dataprovider on it. What I would like to do is when an item on the list is clicked I would like to have a check sign right next to the label.

Below is the component:

<s:List id="tabList" width="100%"
                        borderVisible="false" click="tabList_clickHandler(event)"
                        selectedIndex="{this.hostComponent.selectedIndex}"
                        itemRenderer="MultiTabListRenderer" />

Below is the Itemrenderer code:

protected function AddCheck_clickHandler(event:MouseEvent):void {
        // TODO Auto-generated method stub
        var checkLabel:Label;
        checkLabel = new Label();
        checkLabel.text = "checkMark";

        var e: ItemClickEvent = new ItemClickEvent(ItemClickEvent.ITEM_CLICK, true);
        e.item = data;
        e.index = itemIndex;
        dispatchEvent(e);
        this.checkRectGroup.addElementAt(checkLabel, e.index);
    }

<s:Label id="customMultitabList" text="{data.label}"
             left="10" right="0" top="6" bottom="6" click="AddCheck_clickHandler(event)"/>

My code inside the function is wrong which is mainly due to the fact that I do not understand each and everything in flex. I am not in a mood to learn the language in detail because it's not a long term work for me. Also, in the renderer file when I use s:List instead of s:label I do not see the labels anymore. Of course I replace the attribute text with dataprovider={data.selectedItem}.

Bytekoder
  • 192
  • 1
  • 7
  • 23
  • 4
    Did you just call working with Flex pathetic? Perhaps that isn't the proper way to frame a question when asking for help? – JeffryHouser Feb 25 '13 at 23:47
  • Well, the community is so small that I couldn't find a better word. It's not like I hate it but whoever I know they hate it. – Bytekoder Feb 25 '13 at 23:52
  • 5
    In my version of the world; A small community does not equate a pathetic technology. I'm sorry everyone you know hates Flex. The Apache Flex team is doing some pretty cool things with Flex, including some great experiments around "HTML5" output. If you want help with Flex, I suggest you don't start by 'bashing' those who work with it. – JeffryHouser Feb 26 '13 at 01:50
  • 2
    I've posted one solution for doing this, that outlines a typical strategy. Not saying you need to do it exactly this way, just an example of the strategy I recommend. There's more than one way to skin this cat. To be fair to @www.Flextras.com, I down voted this question (I don't think he did). Flex is pretty awesome, I know there are still a lot of smart people moving it forward. Please comment if you need help tailoring the strategy below to your implementation. – Sunil D. Feb 26 '13 at 02:22

1 Answers1

2

One way to approach this is to add a field to the objects in your dataProvider that tracks whether or not the item has been selected.

Then, in your item renderer, you inspect this field and decide whether or not to display the checkmark. Here's a working example app and renderer:

Application:

<?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" xmlns:local="*"
               creationComplete="application1_creationCompleteHandler(event)">
    <fx:Script>
        <![CDATA[
            import mx.collections.ArrayCollection;
            import mx.events.CollectionEvent;
            import mx.events.CollectionEventKind;
            import mx.events.FlexEvent;
            import mx.events.PropertyChangeEvent;
            import mx.events.PropertyChangeEventKind;

            private var collection:ArrayCollection;

            protected function application1_creationCompleteHandler(event:FlexEvent):void
            {
                collection = new ArrayCollection([
                    { label: 1, selected: false },
                    { label: 2, selected: false },
                    { label: 3, selected: false }]);

                listbert.dataProvider=collection;
            }

            protected function listbert_clickHandler(event:MouseEvent):void
            {
                var index:int = listbert.selectedIndex;
                var item:Object = listbert.selectedItem;
                item.selected = !item.selected;
                // Create these events because the items in the ArrayCollection
                // are generic objects. It shouldn't be necessary if items in
                // your collection are a Class that extends EventDispatcher
                // see ArrayList::startTrackUpdates()
                var e:PropertyChangeEvent = new PropertyChangeEvent(
                    PropertyChangeEvent.PROPERTY_CHANGE, false,false,
                    PropertyChangeEventKind.UPDATE, 'selected', !item.selected,
                    item.selected, item);

                collection.dispatchEvent(new CollectionEvent(
                    CollectionEvent.COLLECTION_CHANGE, false,false,
                    CollectionEventKind.UPDATE, index, index, [e]));
            }

        ]]>
    </fx:Script>

        <s:List id="listbert" click="listbert_clickHandler(event)" itemRenderer="TestRenderer"/>
</s:Application>

Item Renderer:

<?xml version="1.0" encoding="utf-8"?>
<s:ItemRenderer xmlns:fx="http://ns.adobe.com/mxml/2009" 
                xmlns:s="library://ns.adobe.com/flex/spark" >
    <fx:Script>
        <![CDATA[

            override public function set data(value:Object):void
            {
                super.data = value;
                labelDisplay.text = value.label;
                if (value.selected)
                    checkMarkLabel.text = "✓";
                else
                    checkMarkLabel.text = "";
            }
        ]]>
    </fx:Script>

    <s:layout>
        <s:HorizontalLayout/>
    </s:layout>
    <s:Label id="labelDisplay" />
    <s:Label id="checkMarkLabel" />
</s:ItemRenderer>
Sunil D.
  • 17,983
  • 6
  • 53
  • 65
  • +1; as this is similar to the approach I use too. The biggest argument against this is that "Data/Model" objects should not know about their "UI/Display" state. However, I have yet to find a method of separating the data and the UI Logic in this situation that is simple to implement and easy to maintain. – JeffryHouser Feb 26 '13 at 03:17