Monday, March 15, 2010

Flex Datagrid to ComboBox

I'm using my combobox inside a custom panel where I display combos upside their related datagrid headers in this case ServicesListPanel. Call prepareFilter function when your data you want to filter is ready. I've an e4x XML reply(ServiceList) from the webservice to display all services in a datagrid. There are duplicate entried so I remove duplicates and create a new array collection so I can set it to filter's combobox dataprovider. 

I've also added setFilter function which is called when the filter's combobox selected changes. I send the selected item of the combobox and list to apply filter to this function. After filter applied I set the resultgrid's dataprovider to filtered array collection.

channel_id and channel_name are from my e4x XML, modify them according to your needs.


//PREPARE FILTER FUNCTION
private function prepareFilter():void
{
ServicesListPanel.channelCombo.dataProvider = prepareComboBox(ServiceList.server_reply.services.service);
}

// REMOVE DUPLICATES AND RETURN ARRAY COLLECTION
private function prepareComboBox(XMLData:XMLList):ArrayCollection
{
ComboList = new ArrayCollection;
ComboList.addItem({
data:0, 
label:'All'
});
for (var i:Number=0;i
{
canAdd = true;
if(ComboList.length > 0)
{
for (var x:Number=0;x
{
if(ComboList[x].label == XMLData[i].channel_name)
{
canAdd = false;
break;
}
}
}
if(canAdd)
ComboList.addItem({
data:XMLData[i].channel_id, 
label:XMLData[i].channel_name
});
}
return ComboList;
}

//SET FILTER
private function setFilter(selectedObject:Object,XMLData:XMLList):void
{
FilteredList = new ArrayCollection;
for (var i:Number=0;i
{
if(XMLData[i].channel_name == selectedObject.label || selectedObject.data == 0)
{
FilteredList.addItem(XMLData[i]);
}
}
resultsGrid.dataProvider = FilteredList;
}

No comments:

Post a Comment