Get Picker Control’s SmartObject Value Correctly

Get Picker Control’s SmartObject Value Correctly

In this post, I’m going to show you how to get Picker control’s SmartObject value correctly.

Get Picker Control’s SmartObject Value Correctly

When K2 smartforms Picker control executes the “On Changed” event, you will be able to extract the selected item’s values via the underlying SmartObject properties.

The Transfer rule
The Transfer rule
Picker control's SmartObject properties
Picker control’s SmartObject properties

It is a great way of populating the form data with more information regarding the selected item and you do not need to execute another SmartObject to do so as well (Yeah! No extra trips!). So in my case, my form allows the user to select the staff and display their relevant information.

How my view looked like
How my view looked like

Do Not Remove the View Initialise Rule!

Why? It is the “good practice” to remove empty “View Initialise” rules from the “Form Initialising” rule. This is to avoid executing unnecessary actions on the client browser. If you look at my demo View here, there’s no Initialising rule.

No Initialise Rule in View
No Initialise Rule in View

So naturally, my first reaction will be to remove the “View initialise” rule in my “Form Initialising” Rule. By the way, if you are wondering why it was there in the first place, K2 smartforms automatically adds the event to the “Form Initialising” rule when you add a new View to the Form.

Form Initialise Rule
Form Initialise Rule with the View Initialise event

With the View Initialise rule removed, I tested the Form but the fields did not get populated.

Picker content not populated
Picker content not populated

But if I try to search for another User Id, it populates correctly this time!

Picker functioning on 2nd try
Picker functioning on 2nd try

If you refresh the Form and try again, you will get the exact same behaviour. The Picker always fails on the first execution. Why?

Well, simple enough reason is that the “View Initialise” rule somehow helps setup the JavaScript events on the browser end, so it is crucial that the Picker controls get initialised first before use. So go on and put back the “View Initialise” rule and everything will be good to go.

Form Initialise Rule
Form Initialise Rule

 

Have Fun!

 

Ji Kai

Leave a Reply