Some notes I took while RTFM’ing. One begins to take for granted how sorting and filtering works after working with ArrayCollections for so long. Such write ups help me as a reference down the line. May be of help to you too.
Working With Data Views
===================
ArrayCollection/XMLListCollection have a ‘view’ property provided by the ICollectionView interface.
Data views feature:
- Modify the data view to show the data as sorted; without affecting the underlying data.
- Modify the data view to show a subset of items in the data; without affecting the underlying data.
- Traverse a collection using a ‘cursor’ pointer
- Save ‘bookmark’ the cursor positions
- Insert and delete items in the collection; which directly impacts the data.
- Access parts of data set that might become available at different times.
* Best practices is to work with the collection rather than a control’s dataprovider property that maps to that collection
Sorting and filtering data for viewing
===========================
[Sorting]
- “Data view operations have no effect on the underlying data object content, only on the subset of data that the collection view represents”
- Sort objects: Specify fields to be sorted (Using SortField); require results to be unique, or store custom sort functions to be used for ordering sorted output.
- Sort objects can be used to find items in a collection
* .refresh() is always required to see the results of the application of the sort.
[Filtering]
- Use filter function to filter a subset of the source data
- The function signature must take a single Object which will be a collection item. The functions return is Boolean, true if if the item in question is to be included in the view.
* .refresh() is always required to see the results of the application of the filtering.
Using a view cursor
===============
- A cursor indicates the current position of a particular target object in a data collection.
- To access a cursor, the collection has an createCursor() method that returns a view cursor of type IViewCursor.
seek(bookmark:CursorBookmark, offset:int = 0, prefetch:int = 0):void
Moves the cursor to a location at an offset from the specified bookmark.
“When you use standard Flex collection classes, ArrayCollection and XMLListCollection, you use the IViewCursor interface directly; as the following code snippet shows, you do not reference an object instance:
public var myAC:ICollectionView = new ArrayCollection(myArray);
public var myCursor:IViewCursor;
myCursor=myAC.createCursor();
Manipulating the view cursor
=====================
- Traversal forward: Always do a IViewCursor.afterLast:Boolean check to see if there exists an item in the collection to perform IViewCursor.moveNext().
/*******************************************************/
moveNext():Boolean
Moves the cursor to the next item within the collection.
/*******************************************************/
- Traversal backwards: Always do a IViewCursor.beforeFirst:Boolean check to see if there exists an item in the collection to perform IViewCursor.movePrevious().
/*******************************************************/
movePrevious():Boolean
Moves the cursor to the previous item within the collection.
/*******************************************************/
* The collection must have a Sort Object defined and .refresh declared before using the following search methods
/*******************************************************/
findAny(values:Object):Boolean
Finds an item with the specified properties within the collection and positions the cursor to that item.
/*******************************************************/
/*******************************************************/
findFirst(values:Object):Boolean
Finds the first item with the specified properties within the collection and positions the cursor to that item.
/*******************************************************/
/*******************************************************/
findLast(values:Object):Boolean
Finds the last item with the specified properties within the collection and positions the cursor on that item.
/*******************************************************/
- findAny() executes faster than findFirst() or findFast()
- “If the associated data is from a remote source, and not all of the items are cached locally, the find methods begin an asynchronous fetch from the remote source; if a fetch is already in progress, they wait for it to complete before making another fetch request.”
Getting, adding, and removing data items
===============================
/*******************************************************/
IViewCursor.current : Object
[read-only] Provides access the object at the location in the source collection referenced by this cursor.
/*******************************************************/
/*******************************************************/
IViewCursor.insert(item:Object):void
Inserts the specified item –BEFORE– the cursor’s current position.
/*******************************************************/
Note: If the collection has a sort applied, the object is inserted in a way that maintains the sort. Otherwise, the object is inserted where the cursor is in the collection.
/*******************************************************/
IViewCursor.remove():Object
Removes the current item and returns it.
/*******************************************************/
Removes the item the cursor is referencing then refers to the location after the removed item provided it is not the last item.
Using bookmarks
=============
1) Move the cursor to a location in the collection using findAny(), findFirst(), findLast(), moveNext(), movePrevious(), remove(), or seek()
2) Bookmark the current position
var myBookmark:CursorBookmark = myViewCursor.bookmark;
3) Do step 1) to move cursor around
4) To get back to the bookmark position, tell the cursor to seek the bookmark.
myViewCursor.seek(myBookmark);
Recent Comments