Why do I need to add scripts to the Form?
Some of the reasons will be:
- To change the style of a specific View(s) or control(s) on the Form without amendment the Theme css.
How can I add scripts to K2 smartforms?
If you notice, the Data Label control has a Literal option. This option determines whether the Value provided will be printed on screen or will be evaluated like normal Html. So let’s test this out.
- Start your K2 Designer website.
- Create a new Form, give it a name and add a Data Label to the design canvas.
- Now, set the Data Label control on focus and review the Properties panel.
- In the Properties panel, add the following script to the Text value.
- Scroll down further to find the Literal option. Make sure it is unchecked for now and click Finish.
- Go back and edit the Form. This time, check the Literal option on the Data Label and click on Finish.
Wait! It’s not a good idea to have script in your Data Label. Huh??
Yes, you heard it right. I just showed you how to execute scripts using Data Label, but it is not a good idea to do so. Why?
In K2 smartforms before 4.6.11,
- You may find that the Data Label will be missing when you return to edit the Form. This is because the Data Label value will be evaluated during design time and it ends up un-selectable on the design canvas.
The correct way will be to use an Expression. The reason being, Expression will be evaluated during runtime, but not in design time mode. So any bad scripting will not affect your design canvas and you won’t end up with a un-repairable Form. So let’s see how to use an Expression to print our scripts.
- Create a new Form and add a Data Label to the design canvas.
- Check the Literal option.
- Click on the Expression button.
- Click on Add.
- Give the Expression a Name and set the value with the following text and click OK.
- Ensure that the new Expression is selected and click OK again.
Latest posts by Ji Kai (see all)
- Multiple records inserted with RuntimeListViewRowCount option - 30 May 2017
- Friendly Error Message on K2 smartforms - 04 May 2017
- Add company logo and change K2 login form descriptions - 02 Aug 2016