Find out the selected item in the spark list with multiple choices

In the spark list, I can use the event changeto find out which item was selected or canceled. The submitted object IndexChangeEventhas properties newIndexand oldIndexcontaining this information.

But with multiple choice of this no longer works, because newIndex, and oldIndexcan refer to the index yet selected items.

The solution would be to copy the vector selectedIndicesinto another variable and compare this variable with selectedIndicesafter changing the selection, but this seems a bit complicated.

Does anyone know if there is an easy way to get the index / element that the user deselects while the other elements are still selected?

+3
source share
2 answers

You will need to save the selected Indices and compare the difference.

private static function findMissing(ar1:Array, ar2:Array):Array
{
    var missing:Array = [];
    for each(var item:Object in ar1)
    {
        if(ar2.indexOf(item) < 0)
            missing.push(item);
    }

    return missing;
}
0
source

You can also expand your custom data object with

  • connecting logical property: _selectedwhich is updated through interactions with the list.
  • and an event that can be launched from IT: selectionChanged.

In the installer selected- if it really has changed - you can fire your own bbbling event selectionChangedand grab it where you need it.

SelectableItem.as - a custom data structure that represents row data from a list

[Bindable]
[Event(name="selectionChanged",type="flash.events.Event")]
public class SelectableItem
{
    public static const EVENT_SELECTION_CHANGED:String = 'selectionChanged';
    private var _selected:Boolean;
    private var _name:String;

    public function SelectableItem()
    {
    }

    public function get selected():Boolean
    {
        return _selected;
    }

    public function set selected(value:Boolean):void
    {
        if (_selected != value)
        {
            _selected = value;
            dispatchEvent(new Event(EVENT_SELECTION_CHANGED, true));
        }
    }

    public function get name():String
    {
        return _name;
    }

    public function set name(value:String):void
    {
        _name = value;
    }
}

MyDataGrid.mxml

<?xml version="1.0" encoding="utf-8"?>
<mx:DataGrid xmlns:fx="http://ns.adobe.com/mxml/2009" 
    xmlns:s="library://ns.adobe.com/flex/spark" 
    xmlns:mx="library://ns.adobe.com/flex/mx" 
    allowMultipleSelection="true">
    <fx:Script>
        <![CDATA[
            import mx.controls.listClasses.IListItemRenderer;

            protected override function updateRendererDisplayList(r:IListItemRenderer):void
            {
                super.updateRendererDisplayList(r);
                if (r && r.data)
                    r.data.selected = DataGrid(r.owner).isItemSelected(r.data);
            }

        ]]>
    </fx:Script>
</mx:DataGrid>

TestApplication.mxml

<fx:Script>
    <![CDATA[
        import mx.collections.ArrayCollection;
        import mx.events.FlexEvent;

        [Bindable]
        private var list:ArrayCollection = new ArrayCollection();

        protected function onCreationComplete(event:FlexEvent):void
        {
            for (var i:int = 0; i < 20; i++)
            {
                var item:SelectableItem= new SelectableItem();
                item.name = 'Item ' + i;
                item.addEventListener(SelectableItem.EVENT_SELECTION_CHANGED, onItemSelectionChanged);
                list.addItem(item);
            }
        }

        private function onItemSelectionChanged(event:Event):void
        {
            trace(event);
        }

    ]]>
</fx:Script>
<my:MyDataGrid dataProvider="{list}" width="100%" height="100%" />

, .

0

Source: https://habr.com/ru/post/1757813/


All Articles