通过复选框传递多个数据项

时间:2010-12-18 16:37:06

标签: flex actionscript-3

我有2个复选框转发器:第一个使用ArrayCollection&第二个填充w /从第一个复选框中选择“标签”和“数据”字段....如何将另一个项目传递给第二个复选框?在下面的代码中,我目前只传递“key”和“val”字段&想通过“另一个”领域。

   <?xml version="1.0" encoding="utf-8"?>
    <mx: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" 
            creationComplete="updateOlder();"
            >
<fx:Script>
    <![CDATA[
        import mx.collections.ArrayCollection;
        import flash.events.MouseEvent;
        import mx.controls.CheckBox;
        import mx.core.Repeater;
        [Bindable]
        public var older:ArrayCollection  = new ArrayCollection ([]);
        [Bindable]
        public var newer:ArrayCollection  = new ArrayCollection ([]);

        private function updateOlder():void{
            older.addItem({key:'item1',val:{ name:'desc1' ,another:'another1'}});
            older.addItem({key:'item2',val:{ name:'desc2' ,another:'another2'}});
            older.addItem({key:'item3',val:{ name:'desc3' ,another:'another3'}});
        }

        private function updateNewer(evt:MouseEvent):void {
            var tmpBox:CheckBox = evt.target as CheckBox;
            if(tmpBox.selected) {
                // add item to array   
                newer.addItem({key:tmpBox.label, val:tmpBox.data.val, another:tmpBox.data.another});
            } 
            else {
                // remove item from array
                var newArrIndex: int = getArrayElementIndex(newer, older[tmpBox.instanceIndex]);
                if(newArrIndex != -1){
                    newer.removeItemAt(newArrIndex);
                }
            }
            newer.refresh();
        }

        private function getArrayElementIndex(arr:ArrayCollection, elementValue:Object):int{
            for (var retInd: int = 0; retInd < arr.length; retInd++) {
                if (arr[retInd]['key'] == elementValue['key'] && arr[retInd]['val']['name'] == elementValue['val']['name']
                    && arr[retInd]['val']['another'] == elementValue['val']['another']) {
                    return retInd;
                }
            }
            return -1;
        }       
    ]]>
</fx:Script>

<mx:HBox width="100%" height="100%">
    <mx:VBox width="100%" height="100%">
        <mx:Repeater id="oldRepeater" dataProvider="{older}">
            <mx:CheckBox color="black" fontFamily="Arial" fontSize="14"
                         label="{oldRepeater.currentItem.key}" 
                         data="{oldRepeater.currentItem.val}"
                         click="updateNewer(event);"
                         />
        </mx:Repeater>
    </mx:VBox> 
    <mx:VBox width="100%" height="100%">
        <mx:Repeater id="newRepeater" dataProvider="{newer}">
            <mx:CheckBox color="red" fontFamily="Arial" fontSize="14"
                         label="{newRepeater.currentItem.key}"
                         data="{newRepeater.currentItem.val}" 
                         />
        </mx:Repeater>
    </mx:VBox> 
</mx:HBox>
</mx:Application>

1 个答案:

答案 0 :(得分:0)

我不确定你想要完成什么,但在我看来,最简单的方法是让你分配给每个ArrayCollections上的“val”字段的Object包含两个变量,即

older.addItem({ key:'item1', val:{ name:'desc1' ,another:'another1'} });

并稍后检索这些值:

private function getArrayElementIndex(arr:ArrayCollection, elementValue:Object):int{
            for (var retInd: int = 0; retInd < arr.length; retInd++) {
                if (arr[retInd]['key'] == elementValue['key'] && arr[retInd]['val']['name'] == elementValue['val']['name']
                    && arr[retInd]['val']['another'] == elementValue['val']['another']) {
                    return retInd;
                }
            }
            return -1;