Flex: Separate objects into two datagrids based on a specific value
Introduction: Im using Oracle, BlazeDS, Java and Flex. I have a list of objects displayed in a datagrid(Lets call them Main-objects. Each of these objects have a set of UserComment-objects associated with them(One-to-Many relation in the DB). These comments may come from two different "types" of users, distuingished with either a 1 or 0 in the UserComment-object's "userType" field. Each time the user selects a Main-object in the datagrid, the UserComment's related to this object is displayed in a separate datagrid below.
Question: Based on the introduction, i want to separate the UserComment's into two separate datagrids (one for each user-type), instead of the one i have now. So my question is: What is the best approach for performing th开发者_StackOverflow中文版is task? What is the best way of separating the UserComment's into two separate data grids based on the value of the "userType" field?
Create two ArrayCollection objects and bind them, each to one of the DataGrids. Specify a filterFunction for each ArrayCollection. For example:
userTypeA(item:Object):Boolean
{
return UserComment(item).userType == 1;
}
userTypeB(item:Object):Boolean
{
return UserComment(item).userType == 0;
}
Then, whenever a new selection is made, remove all previous items from the ArracyCollections and add all the UserComments from the current selection. The filters will ensure that each DataGrid only shows one of the two types.
Just create two collection views of type ListCollectionView
and set a different filterFunction
for each collection. Then you can use those collections as dataProvider
for your grids.
// those are the comments from the selected "main object"
var comments:ArrayCollection;
var typeZeroComments:ListCollectionView = new ListCollectionView(comments);
typeZeroComments.filterFunction = function(comment:UserComment):Boolean {
return comment.userType == 0;
};
typeZeroComments.refresh();
var typeOneComments:ListCollectionView = new ListCollectionView(comments);
typeOneComments.filterFunction = function(comment:UserComment):Boolean {
return comment.userType == 1;
};
typeOneComments.refresh();
When a different "main object" gets selected just assign the comments of those object to the two filtered collections:
typeZeroComments.list = comments;
typeOneComments.list = comments;
each time the user selects a Main-object in the datagrid go over the UserComment set and arrange them in 2 different ArrayCollections: one for the userType 1 and second for the userType 0 then use this 2 arrays as data providers to the two separate datagrids that you want to create
精彩评论