flex Dictionary dataProvider? - apache-flex

I have a Dictionary that I'd like to bind as the dataProvider for an mx:ComboBox. e.g., when I do this:
mydict[somenewkey]= somenewval;
I'd like the combobox to update its contents.
The problem is that Dictionary doesn't seem to be Bindable. If I were using an Array, I'd use ArrayCollection. But there doesn't seem to be a corresponding DictionaryCollection or HashCollection. What to do?

A Dictionary is not the appropriate object for a dataProvider of a list based class.
I suspect your display problems have nothing to do with data binding, but rather other issues, such as a dictionary does not have a length property.
I suspect the ComboBox will treat your dictionary as a single object, not as a collection of multiple objects.

Try using an ObjectProxy:
http://www.adobe.com/livedocs/flex/3/langref/mx/utils/ObjectProxy.html

Isn't what you're looking for just a combination of the setItemAt and getItemIndex methods of the ArrayCollection?
_myAC.setItemAt( somenewval, _myAC.getItemIndex( somenewkey ) );

Related

data vs listData in Flex itemRenderer

I was wondering what the difference between data and listData in itemRenderers in flex. I have worked with data in all of my itemRenderers.
Basically I want to know when to use which, where each gets set and if I can use them together?
Note that I am asking from a Flex3 point of view.
data is the data that the renderer should display. Use it to work with the original data currently assigned to the renderer.
listData is an additional object to provide you with information about the role of the renderer in the list (rowIndex, columnIndex, list component, uid, ...). Use it to perform some UI related operations such as formatting the first row differently or rows alternating depending on their vertical index, calling the list view component, etc.
Each item of your dataProvider collection is passed to data variable. You entirely define, what is passed to data by defining dataProvider content.
Information about the cell of datagrid/list (such as row/column index, label) is passed to listData (see BaseListData). To use this variable your itemrenderer should implement IDropInListItemRenderer interface.
See details about listData here. The main point is:
The list classes will pass more information to the renderer so that it
can determine which field to use at run-time.
So listData is for advanced usage for more complicated item renderers.
See this:
http://livedocs.adobe.com/flex/3/html/help.html?content=cellrenderer_4.html
And this:
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/mx/controls/listClasses/IDropInListItemRenderer.html

flex linkbar seaching for index of a given label

I need to find the index of a linkbar, only got the label. Like array.indexOf(value) ?
You can iterate through the objects in the LinkBar's dataProvider and check for a match. This is dependent upon the structure of the objects in the dataProvider. If they are simply strings in an array, you can do pretty much what you stated in your question:
(myLinkBar.dataProvider as Array).indexOf(value);
Hope that helps.

Single model - multiple filtered views

I have data model (dataProvider as ArrayCollection) i want to display in few views , each view should show filtered data.
As you probably know, filterFunction is property of ArrayCollection,so I can't use this solution (unless creating new instance of ArrayCollection for each view on top original and impementing filterFunction).
Are there better approaches ?
Thanks
you can use ListCollectionView for each view that use your array, and every time you can pass to it the same array but filtered differently as source...
You're using the same ArrayCollection as dataProvider across multiple views, and setting the filterFunction in view1 persists into view2?
You could probably put code to change the filterFunction for each view in that view's "show" event. If you wanted to default each view to be unfiltered, pass in a function that just returns true.

Flex AdvancedDataGrid AdvancedDataGridRendererProvider childrenField ArrayCollection - Question about behaviour

I have a main class ClassA that has a bunch of "normal" properties that are simple datatype like ints, strings, etc. It also has one property ("childItems") that is an ArrayCollection of ClassB.
I am using an ArrayCollection of ClassA as the source for an hierarchical data provider for an AdvancedDataGrid. I set the childrenField to "childItems".
I want to display some information about the list of ClassB objects in a nested table and pie chart, so I configure an AdvancedDataGridItemRendererProvider (columnIndex = 0, columnSpan = 0, depth = 2) and point it to my custom renderer which is an HBox with the table and the pie chart in it.
In order to see what is being set, I override the "set data" function in my custom renderer and what I see is that each instance of ClassB in the ArrayCollection is passed to the renderer separately.
Here is my question: I expected the whole ArrayCollection of ClassB instances to be passed to the custom renderprovider once and not each item in the child list individually. How do I make the ADG understand that the whole property is supposed to be passed as the data to the renderer and not each entry separately?
Btw, when I change the data type of "childItems" from ArrayCollection to ArrayList, the whole list gets passed and I can easily do what I want to do. But based on my understanding, ArrayList is not really supposed to be used and ArrayCollection is better or at least more common.
Any insights on that would be appreciated.
Thanks!
I pretty much stuck with the ArrayList instead of ArrayCollection as property data type. Then the whole ArrayList gets passed one time to one renderer instead of one renderer per item in the ArrayCollection.
That is not particularly nice, since the source data structure is an ArrayCollection and all my other lists are ArrayCollections, but that worked for me and I never bothered trying to find a different solution for that.

Flex datagrid sorting not preserved across dataprovider changes

I have a flex datagrid. it is bound to an array collection. if the user sorts on column X it works fine. then, if the user causes the array collection to change, the datagrid forgets that it was sorted on column X.
what do I need to do to preserve this sort preference so that the new array data will appear sorted by column X?
I posted this question on another forum and got a good answer that worked well. Here it is:
If your data is in ArrayCollections you can assign a ListCollectionView to the dataProvider property of your child AdvancedDataGrid and assign your ArrayCollection data to the 'list' property of the ListCollectionView. When you want to change the data in the child grid, reassign the list property of the ListCollectionView. This way you avoid reassigning the dataProvider of the child grid directly, which is what causes the grid to reset.
here's the link to the post:
http://forums.adobe.com/message/2206736#2206736
That really helped, thanks. But don't forget to Refresh() the dataProvider once you have reset the sort:
dataGrid.dataProvider.refresh();
I have same kind of problem but didnt knw why it was happening so did some tweaking
selectedItems=dataGrid.selectedItems;
postionGrid=dataGrid.verticalScrollPosition;
sortPostionGrid=dataGrid.dataProvider.sort;
store them in a variable and when data is changed just put the again on the grid it is not a good approach but it worked for me
This post "Keeping the sort on a datagrid when you change the dataProvider" gives
more relevant and efficient solution what you looking for
Hopes that helps

Resources