How to split an embedded collection via a discriminator
In a UI view, it is sometimes useful to divide a collection into several sub-collections depending on the value of a property. For example, you might have a collection of products and want to divide it into two separate collections: one for products in stock and one for products that are out of stock.
There are several ways of achieving this, but in our example, we have chosen to divide the collection from the entity view. It is therefore necessary for the collections to be embedded.
Note
You will find a full implementation in technical demos in the menu References and collections > Referencial > Warehouse or by typing Warehouse directly in the quick search.
Step 1: Create the embedded collections in the entity view
In the entity view, define as many collections as needed in the entity view using the collection filter by the discriminant.
In our product example, create two embedded collections: one for products in stock and one for products that are out of stock. The discriminator is the InStock boolean property.
- Name: ProductsInStock
Caption: Product in stock
CollectionFilter: e.InStock
RelatedEntityViewName: ProductListView
Source: Products
Type: Collection
- Name: ProductsOutOfStock
Caption: Product out of stock
CollectionFilter: !e.InStock
RelatedEntityViewName: ProductListView
Source: Products
Type: Collection
Step 2: Create the sub views in the UI view
In the UI view, define as many collections as needed mapped to filtered collections (you can use the same UI view or different UI views).
In our product example, create two sub views: one for products in stock and one for products that are out of stock.
- Name: ProductsInStock
RelationPropertyName: ProductsInStock
SubUIViewName: ProductListUI
- Name: ProductsOutOfStock
RelationPropertyName: ProductsOutOfStock
SubUIViewName: ProductListUI
Step 3: Create and use a field in the sub UI view
If you are using the same UI view to display different collections, you need to add a field to store the context of the collection displayed, the discriminant value.
- Name: InStock
DotNetDataType: bool
To fill this field, you can use the Initialized event rule of the parent UI view.
SubViews.ProductsInStock.Fields.InStock = true;
SubViews.ProductsOutOfStock.Fields.InStock = false;
If the sub UI views allow the creation, the discriminant property needs to be populated automatically. To do this, use the ItemAdding event rule in the sub view's UI view.
Item.InStock = Fields.InStock;