Press "Enter" to skip to content

Best Practices: Working with subviews or subforms

0

I decided to start a series of the posts, that are focused on Best Practices, when working with K2. I would like to point out, that all what I am and will be writing here is mostly based on my personal experience and the projects I delivered. The approaches might not be suitable for all the use cases, but they are definitely good to pay attention to. So let’s start with the best practices for subviews or subforms.

The current post is mostly about List views, which you do not want to edit inline due to other requirements or validation complexity. And therefore you need a subview to be able to create/edit/view List view items.

The approach seems straightforward. You would normally create a list view with a number of buttons (e.g. Add/Edit/View etc.).

K2 Smartforms: Add Edit buttons

Then you would create 1 Editable Item view, which will behave as:

  • New Item view – if no ItemID Parameter is provided, i.e. the view will load empty and will create an item, when you click Save button.
  • Edit Item view – if ItemID is provided, i.e. the view will get populated with the Item fields and will Update the existing item, when you click Save button.
K2 Initialize and Save rules

And then you would create the following set of rules:

  • When Add button is clicked -> Open Editable SubView without any input parameters;
  • When Edit button is clicked -> Open Editbale Subview with the ID of the selected item.
K2 Smartforms: set of rules

There is nothing wrong about this approach, apart from the fact, that all the rules that you have on the Editable item view are copied twice into the List View. Moreover, if you want to refresh your list view upon subview close, you will need to create 2 separate rules for every subview instance. Shortly speaking, you need to maintain 2 sets of rules, if you do some changes. Instead, you can do the following:

1. Create a parameter or a hidden data label on the List View (e.g. SelectedItemID):

Selected Item ID parameter

2. Create an unbound rule, which will open the subview, sending SelectedItemID as an input parameter for the Edit Item subview

K2 unbound rule

3. Modify your Button rules in the following way:

  • Add button:
    • Clear SelectedItemID
    • Execute @veiw_Open Edit Item Rule
K2 rule: Add button
  • Edit button:
    • Transfer selected Item Id into the SelectedItemID parameter
    • Execute @veiw_Open Edit Item Rule
K2 rule: Edit button

And that’s all. Now you have only 1 set of rules in the List View. If you need, you can add additional validation and additional checks, so that Edit button is not clicked when nothing is selected. But at least you should get the idea where to start. The same approach works fine with subforms. I hope this article, devoted to Best Practices for subviews and subforms, can be useful for K2 developers.

Leave a Reply

Your email address will not be published. Required fields are marked *

three + 13 =