how to compare two array collection in flex when it match check box gets checked

717 Views Asked by At

i am comparing two array collection suppose collection1 and collection2 and in collection 2 i have check box what i am doing is after click on modify button i am comparing these two array collection what i want is what are the element match the corresponding check box will select automatically..

enter code here



             for(var a:String in User360DegreePodCreationView.user360PodView.MyArray) 
        {
            //loop though the second collection
            for(var b:String in User360DegreePodCreation.user360PodCreation.fieldList.dataProvider)`

` { //compare collection if(User360DegreePodCreationView.user360PodView.MyArray[a] == User360DegreePodCreation.user360PodCreation.fieldList.dataProvider[b].columnName) {

                    var chkBox:CheckBox =new CheckBox;
                    chkBox = User360DegreePodCreation.user360PodCreation.fieldList.dataProvider[b].columnName as CheckBox;

                    chkBox.selected=true;

mxml file

                     <s:List id="fieldList"
                        width="100%"
                        height="170"
                        enabled="{isEnabled}"
                        dataProvider="{model.profileTableColumns}"
                        click="fieldList_clickHandler(event)">
                    <s:itemRenderer>
                        <fx:Component id="itemRender">
                            <s:MXItemRenderer>
                                <fx:Script>
                                    <![CDATA[

                                    ]]>
                                </fx:Script>

                                <s:CheckBox left="5"
                                            label="{data.columnName}" 
                                            id="chkColumn"
                                            />
                            </s:MXItemRenderer>
                        </fx:Component>
                    </s:itemRenderer>
                </s:List>

on click of button i am comparing one array collection of datagrid and one arraycollection of list field..`

               private function modifyBtn_clickHandler(event:MouseEvent):void
    {
        User360DegreePodCreation.user360PodCreation.isEnabled=true;
        User360DegreePodCreation.user360PodCreation.isEditable=true;
        User360DegreePodCreation.user360PodCreation.displayUpdateButton();  
        User360DegreePodCreation.user360PodCreation.fieldList.visible=true;
        User360DegreePodCreation.user360PodCreation.list.visible =false;
        var userProfile360TableChangeEvent:UserProfile360DegreeTableChangeEvent=new UserProfile360DegreeTableChangeEvent();
        userProfile360TableChangeEvent.data.idnId=User360DegreePodCreation.user360PodCreation.drpTable.selectedItem.idnId;
        userProfile360TableChangeEvent.data.dbPath=User360DegreePodCreation.user360PodCreation.drpTable.selectedItem.externalRefDBPath;
        userProfile360TableChangeEvent.data.tableName=User360DegreePodCreation.user360PodCreation.drpTable.selectedItem.tableName;
        userProfile360TableChangeEvent.successHandler=function():void
        {
            if (model.profileTableColumns != null && model.profileTableColumns.length != 0)
            {
                User360DegreePodCreation.user360PodCreation.onModify();
            }
        }
        userProfile360TableChangeEvent.dispatch();
        //Loop through the first collection
        for(var a:String in User360DegreePodCreationView.user360PodView.MyArray) 
        {
            //loop though the second collection
            for(var b:String in User360DegreePodCreation.user360PodCreation.fieldList.dataProvider)
            { 
                //compare collection
                if(User360DegreePodCreationView.user360PodView.MyArray[a] == User360DegreePodCreation.user360PodCreation.fieldList.dataProvider[b].columnName)
                {

                    var chkBox:CheckBox =new CheckBox;
                    chkBox = User360DegreePodCreation.user360PodCreation.fieldList.dataProvider[b].columnName as CheckBox;

                    //chkBox.selected=true;


                    //User360DegreePodCreation.user360PodCreation.fieldList.selectedItem


                }

            }
        }
    }

 here is my datgrid arraycollection


               var MyArray:ArrayCollection = new ArrayCollection(); //DATA ARRAY 
                trace(dgUser360View.selectedItem.podTitle);
                for each(var objAux:Object in model.userProfile360DegreeViewDetailsVO.userProfile360Details) //FOR  TYPE OBJECT
                {

                    if(objAux.podTitle==dgUser360View.selectedItem.podTitle)
                    MyArray.addItem(objAux.columnNames) //ADD DATA IN ARRAY 

thanks for ur reply @chris its is working fine for key value pair but what i need is suppose i have one array collection which dynamic type for example onle i am takeing some value like myArray{analysis,id,port} and in the second array collection Myaary2{analysis checkbox,id checkbox,port checkbox,nuber checkbox ,address checkbox} i want only the matching value of array collection gets and unmatched element will remain unchecked i am sharing with u what i did till date

   var modifiedProfileColumns:ArrayCollection = new ArrayCollection;
            /*for(var b:int=0;b<model.profileTableColumns.length;b++)
            {
                modifiedProfileColumns.addItem({columnName:model.profileTableColumns[b].columnName,selection:"Yes"});
            }*/



            //for(var a:String in User360DegreePodCreationView.user360PodView.MyArray) 
            for(var b:int=0;b<model.profileTableColumns.length;b++)
            {
                //loop though the second collection
                for(var a:String in User360DegreePodCreationView.user360PodView.MyArray)
                { 
                    //compare collection
                    if(User360DegreePodCreationView.user360PodView.MyArray[a] == model.profileTableColumns[b].columnName)
                    {
                        modifiedProfileColumns.addItem({columnName:model.profileTableColumns[b].columnName,selection:"true"});
                        break;
                        //modifiedProfileColumns.setItemAt({columnName:model.profileTableColumns[b].columnName,selection:"true"},b+1);

                    }
                    else
                    {

                        modifiedProfileColumns.addItem({columnName:model.profileTableColumns[b].columnName,selection:"false"});

                    }


                } 

            }


            fieldList.dataProvider = modifiedProfileColumns;
        }

here is the list codeing

            <s:List id="fieldList"
                        width="100%"
                        height="170"
                        enabled="{isEnabled}"
                        click="fieldList_clickHandler(event)">
                    <s:itemRenderer>
                        <fx:Component id="itemRender">
                            <s:MXItemRenderer>
                                <fx:Script>
                                    <![CDATA[


                                    ]]>
                                </fx:Script>



                                <s:CheckBox left="5"
                                            label="{data.columnName}"
                                            selected="{data.selection=='true'}"
                                            id="chkColumn"/>
                            </s:MXItemRenderer>
                        </fx:Component>
                    </s:itemRenderer>
                </s:List>

i am getting the match value as checked but the element in second collection array collection coming thrice ...as i guess the problem is with looping am not able to find it. i will be always grateful if anybody could help me to find out the solution...

1

There are 1 best solutions below

2
On

Assuming that the elements you are displaying are somehow identifyable (Have some sort of Id property or something unique). I would strongly suggest not to use a double for loop over all elements of your collections as they should have exponential runtime behaviour.

I would much more suggest to create an index of the elements of your collection. If your unique property (or set of properties), then you could use an Object object to create an index based upon this property.

Assuming you have a function that rerurns a string key for a giben item.

getItemKey(item:Object):String

Then I would suggest to create an index as follows:

var index:Object = {}; // This is a shorthand for "var index:Object = new Object();
for each(var item:Object in collection1) {
    var key:String getItemKey(item);
    index[key] = item;
}

After having the index you could get a reference to your clicked item like this:

var clickedItemKey:String = getItemKey(clickedItem);
if(index[clickedItemKey]) {
    var itemInCollection1:Object = index[clickedItemKey];
    // Do something with the clicked item
} else {
    // The item was not found in collection1
}

I guess this approach should be a lot faster and easier to read.