ArrayCollection with duplicate elements

463 views
Skip to first unread message

Kiran Kumar Vasireddy

unread,
Dec 26, 2009, 5:30:40 AM12/26/09
to Flex India Community
Dear All,

I am getting the data in following format . Is there any way to load it into comboxbox avoiding duplicates? First combobox I want to just load Company Name removing duplicates.Second combox box based on the section I have to load  Models and In the Thrid combox box based on company and Model  I have to load Years.Or if not Arraycollection , I can also get the data in XMLCollection . Could some body please give me best solution?

<ArrayCollection>

          <mx:Object Company="GM" count="85"  Model ='Saab" ModelCount=20 Year="2009" YearCount="10" />
          <mx:Object Company="GM" count="85"  Model ='Saab" ModelCount=20 Year="2008" YearCount="5" />
          <mx:Object Company="GM" count="85"  Model ='Saab" ModelCount=20 Year="2007" YearCount="5" />
           <mx:Object Company="GM" count="85"  Model ='Chevi" ModelCount=20 Year="2009" YearCount="20" />

         <mx:Object Company="FORD" count="43"  Model ='Focus" ModelCount=10 Year="2009" YearCount="5" />
          <mx:Object Company="FORD" count="43"  Model ='Focus" ModelCount=10 Year="2008" YearCount="3" />
          <mx:Object Company="FORD" count="43"  Model ='Focs" ModelCount=10  Year="2007" YearCount="2" />      

</ArrayCollection>

sridhar valiveti

unread,
Dec 27, 2009, 2:29:41 AM12/27/09
to flex_...@googlegroups.com
Hi   Kiran,

Please try out this link, should be able to solve your  issue...        http://flexoop.com/tag/combobox/


--

You received this message because you are subscribed to the Google Groups "Flex India Community" group.
To post to this group, send email to flex_...@googlegroups.com.
To unsubscribe from this group, send email to flex_india+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/flex_india?hl=en.



--
Sreedhar.V

saanaa safi

unread,
Dec 27, 2009, 10:50:24 PM12/27/09
to flex_...@googlegroups.com
hi kiran,
i hope below example will help u. try this,i think this is what u r expecting.

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
    <mx:ArrayCollection id="myColl">

          <mx:Object Company="GM" count="85"  Model ="Saab" ModelCount="20" Year="2009" YearCount="10" />
          <mx:Object Company="GM" count="85"  Model ="Saab" ModelCount="20" Year="2008" YearCount="5" />
          <mx:Object Company="GM" count="85"  Model ="Saab" ModelCount="20" Year="2007" YearCount="5" />
           <mx:Object Company="GM" count="85"  Model ="Chevi" ModelCount="20" Year="2009" YearCount="20" />

         <mx:Object Company="FORD" count="43"  Model ="Focus" ModelCount="20" Year="2009" YearCount="5" />
          <mx:Object Company="FORD" count="43"  Model ="Focus" ModelCount="20" Year="2008" YearCount="3" />
          <mx:Object Company="FORD" count="43"  Model ="Focs" ModelCount="20"  Year="2007" YearCount="2" />     

    </mx:ArrayCollection>
    <mx:ComboBox prompt="Company" dataProvider="{myColl}" labelField="Company" x="44" y="21" width="100"/>
    <mx:ComboBox prompt="Model" dataProvider="{myColl}" labelField="Model" x="153" y="21" width="100"/>
    <mx:ComboBox prompt="Year" dataProvider="{myColl}" labelField="Year" x="275" y="21" width="100"/>
   
</mx:Application>

On Sat, Dec 26, 2009 at 4:00 PM, Kiran Kumar Vasireddy <kira...@gmail.com> wrote:

--

You received this message because you are subscribed to the Google Groups "Flex India Community" group.
To post to this group, send email to flex_...@googlegroups.com.
To unsubscribe from this group, send email to flex_india+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/flex_india?hl=en.



--
Warm Regards
Saanaa....

Kiran Kumar Vasireddy

unread,
Dec 29, 2009, 10:53:13 AM12/29/09
to flex_...@googlegroups.com
Thanks Sridhar for the Link.

I am working on it . Only problem here is whole node is not duplicated . So to avoid duplicates for a Company I only have to work on Company attribute  and populate first combobox .

Second combobox will be populated again based on the Company selected again I have to filter on whole XML and get the Company related Models and again I have to avoid duplicates in the XML based on Model( Again I should not consider Year attribute here) and populate second combobox

Third combobox will be populated based on Company and selected Model , Get the model again and have to apply remove duplicates.

Looks like bit process intensive work..

1) Removing duplicates for company ( Only have to Check company attribute)
2) Filtering models based on Selected Company
3) Removing duplicates of Models( Only have to Check Model attribute)
4) Filtering Year based on the selected Company and Model
5)Removing duplicates of Year ( Only have to Check Year attribute)

Is there any easy way to get the XMl file and compose it in below format ?


<Company="GM" count="85">
   < Model ='Saab" ModelCount=20 >
        < Year="2009" YearCount="10"/>
        <Year="2008" YearCount="5"/>
         <Year="2007" YearCount="5"/>
   </Model>
</Company>
    
I tried in PHP and in Java and writing the file at server end . But again  timing issues came up

Regards
Kiran

Kiran Kumar Vasireddy

unread,
Dec 30, 2009, 10:46:42 AM12/30/09
to flex_...@googlegroups.com
Thanks Safi for your response . If I do this I get duplicate elements in Mycombox , I want to avoid them.

Regards
Kiran

Aviral Sharma

unread,
Jan 21, 2010, 6:09:46 PM1/21/10
to Flex India Community
Now, you can provide a dataprovider for combobox (using an
arraycollection is better than hardcoding the data).
Use filter function to remove duplicates as described in the link:
http://blog.flexexamples.com/2007/08/05/removing-duplicate-items-from-an-array-using-the-arrayfilter-method/

Here is the code (visit the website link above to see a working
example)

<!-- Start Code -->

<?xml version="1.0" encoding="utf-8"?>

<!-- http://blog.flexexamples.com/2007/08/05/removing-duplicate-items-from-an-array-using-the-arrayfilter-method/
-->


<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"

layout="vertical"
verticalAlign="middle"
backgroundColor="white"
creationComplete="init()">

<mx:Script>
<![CDATA[
private var keys:Object = {};

/**
* Called by the Application container's creationComplete
* event handler. This method creates a new Array object
* which will be used as a data provider as well as a
* filtered view of that array which does not contain
* duplicated items.
*/
private function init():void {
/* Create a dummy data source with some semi-random
data. */
var arr:Array = [];
arr.push({data:1, label:"one"});
arr.push({data:1, label:"one"});
arr.push({data:1, label:"one"});
arr.push({data:1, label:"one"});
arr.push({data:2, label:"two"});
arr.push({data:2, label:"two"});
arr.push({data:2, label:"two"});
arr.push({data:1, label:"one"});
arr.push({data:3, label:"three"});
arr.push({data:3, label:"three"});

/* Filter the original array and call the
removeDuplicates() function on each item
in the array. */
var filteredArr:Array = arr.filter(removedDuplicates);

arrColl.source = arr;
dedupedArrColl.source = filteredArr;
}

/**
* This method is used to filter an array so that no
* duplicate items are created. It works by first
* checking to see if a keys object already contains
* a key equal to the current value of the item.data
* value. If the key already exists, the current item
* will not be readded to the data provider. If the key
* does not already exist, add the key to the keys
* object and add this item to the data provider.
*/
private function removedDuplicates(item:Object, idx:uint,
arr:Array):Boolean {
if (keys.hasOwnProperty(item.data)) {
/* If the keys Object already has this property,
return false and discard this item. */
return false;
} else {
/* Else the keys Object does *NOT* already have
this key, so add this item to the new data
provider. */
keys[item.data] = item;
return true;
}
}
]]>
</mx:Script>

<mx:ArrayCollection id="arrColl" />
<mx:ArrayCollection id="dedupedArrColl" />

<mx:HBox>
<mx:VBox>
<mx:Label text="Original ({arrColl.length} items):" />
<mx:List dataProvider="{arrColl}" />
</mx:VBox>
<mx:VBox>
<mx:Label text="Filtered ({dedupedArrColl.length}
items):" />
<mx:List dataProvider="{dedupedArrColl}" />
</mx:VBox>
</mx:HBox>

</mx:Application>

<!-- End Code -->

On Dec 30 2009, 10:46 am, Kiran Kumar Vasireddy <kiranv...@gmail.com>
wrote:

> >> flex_india+...@googlegroups.com<flex_india%2Bunsubscribe@googlegrou ps.com>


> >> .
> >> For more options, visit this group at
> >>http://groups.google.com/group/flex_india?hl=en.
>
> > --
> > Warm Regards
> > Saanaa....
>
> >  --
> > You received this message because you are subscribed to the Google Groups
> > "Flex India Community" group.
> > To post to this group, send email to flex_...@googlegroups.com.
> > To unsubscribe from this group, send email to

> > flex_india+...@googlegroups.com<flex_india%2Bunsubscribe@googlegrou ps.com>

Jitendra Jain

unread,
Jan 22, 2010, 4:38:34 AM1/22/10
to flex_...@googlegroups.com
Read about filter function it will be quite helpful.

To unsubscribe from this group, send email to flex_india+...@googlegroups.com.

Varun Bajaj

unread,
Jan 22, 2010, 7:02:42 AM1/22/10
to flex_...@googlegroups.com
I think if you use xmllist for dataprovider for your comboboxes then you can easily have chain effect as you said.

Hope this code snippet may help you.
(replace the values according your requirements)

 var dp1:xmllist = new xmllist(dp0.data.(@field == val));
dp1.refresh();

Varun Bajaj

Akki

unread,
Jan 22, 2010, 11:57:44 PM1/22/10
to Flex India Community
Hi

First create six array collections for six comboboxes which will be
data providers for them. I am assuming that there can be change in the
collection (sourceCollection) e.g. add/remove etc in mean time and you
would like to reflect that too. So first add an event listener on that
collecion like:

sourceCollection.addEventListener(CollectionEvent.COLLECTION_CHANGE,
collectionDataChanged);
Then in that case a fucntion collectionDataChanged will be called
upon.In that function if change is collection.refresh dont do
anything, else update individual combobox data providers:
updateOrganizationDataProvider(organizationCollection,
newOrganization)
updateModelDataProvider()
so on.....

updateOrganizationDataProvider(ac:ArrayColleciton, obj:object)
{

if (ac.contains(obj)
return
esle
ac.add(obj)
}

for other comboboxes addition/deletion will be done same way but that
will depend upon selection in previous comboboxes. Also FIlter
function can be written keeping in mind the selections in all
comboboxes.

I hope it helps.


companyAC, ModelAC,


<mx:Object Company="GM" count="85" Model ='Saab" ModelCount=20
Year="2009" YearCount="10" />
<mx:Object Company="GM" count="85" Model ='Saab"
ModelCount=20
Year="2008" YearCount="5" />
<mx:Object Company="GM" count="85" Model ='Saab"
ModelCount=20
Year="2007" YearCount="5" />
<mx:Object Company="GM" count="85" Model ='Chevi"
ModelCount=20
Year="2009" YearCount="20" />


<mx:Object Company="FORD" count="43" Model ='Focus"
ModelCount=10
Year="2009" YearCount="5" />
<mx:Object Company="FORD" count="43" Model ='Focus"
ModelCount=10
Year="2008" YearCount="3" />
<mx:Object Company="FORD" count="43" Model ='Focs"
ModelCount=10
Year="2007" YearCount="2" />


On Jan 22, 5:02 pm, Varun Bajaj <vb.j...@gmail.com> wrote:
> I think if you use xmllist for dataprovider for your comboboxes then you can
> easily have chain effect as you said.
>
> Hope this code snippet may help you.
> (replace the values according your requirements)
>
>  var dp1:xmllist = new xmllist(dp0.data.(@field == val));
> dp1.refresh();
>
> Varun Bajaj
>
> On Fri, Jan 22, 2010 at 3:08 PM, Jitendra Jain <
>
>
>
> warriorofheavens...@gmail.com> wrote:
> > Read about filter function it will be quite helpful.
>

> > On Fri, Jan 22, 2010 at 4:39 AM, Aviral Sharma <aviralmsha...@gmail.com>wrote:
>
> >> Now, you can provide a dataprovider for combobox (using an
> >> arraycollection is better than hardcoding the data).
> >> Use filter function to remove duplicates as described in the link:
>

> >>http://blog.flexexamples.com/2007/08/05/removing-duplicate-items-from...


>
> >> Here is the code (visit the website link above to see a working
> >> example)
>
> >> <!-- Start Code -->
>
> >> <?xml version="1.0" encoding="utf-8"?>
> >> <!--

> >>http://blog.flexexamples.com/2007/08/05/removing-duplicate-items-from...

> >http://groups.google.com/group/flex_india?hl=en.- Hide quoted text -
>
> - Show quoted text -

Kiran Kumar Vasireddy

unread,
Jan 25, 2010, 9:57:05 AM1/25/10
to flex_...@googlegroups.com
Thanks every one for their valuable suggestions and instructions , I am working on this right now and I will let you guys know the result.

Regards
Kiran
Reply all
Reply to author
Forward
0 new messages