Javascript from Form to View or between View to View

  • 17 September 2019
  • 2 replies
  • 30 views

I've been able to run Javascript on controls in a view where the javascript is injected in the same view but I haven't been able to execute Javascript that is at the form level on controls in a view or between views.  Has anyone been successful in doing this?


2 replies

Badge +3

Hi Disa,



Would it be possible to elaborate a bit more on what you're trying to do?

Otherwise I'd recommend having a look at some of the current documenation we have:

https://help.k2.com/onlinehelp/K2Five/DevRef/5.3/default.htm#Controls/Add-Custom-JScript.html


 


Kind regards,



Hendrix

Userlevel 4
Badge +13

Hi Disa


 


You can most definitely make coded changes to controls residing on a view even from a form level if the view were to be present on a form, you merely need to ensure that your code is precisely targeted to the relevant destination.


I found it very helpful by providing my view with a unique name on form level and providing controls with unique name on view level (this can be done from the view/control properties). The same can also be achieved when calling the execution on form level but allowing the execution to occur on view level in order to alter a views control.


 


Doing this between view to view would unfortunately not work as the first view does not have scope visibility to any of the other views located ont he form, however the form does scope visibility to all views residing in it therefore this works with from execution to alter view or view controls and form calling code to be executed on view to alter view controls.


 


See below instructions by means of example:



Example 
Scenario:
Let say I have a data label on a view, the view is on a form. My requirement is to change the data labels background color by executing a piece of code on the form level when a button is pressed. To do this I would go about it in the following manner.

Steps:


1) Edit the view and navigate to the control.


2) Select the control and change the controls Name property to be unique (for this example used Data Label 123).


3) Save, finish and check in the view.


4) Navigate to the form


5) Select the view and change the views Name property to be unique (for this example used Single Control View 123).


6) Add a additional data label to the form and provide it with a unique Name (for this example used Data Label Code).
Note: This control will be used to execute the code that get provided to it so you have to ensure that this controls "Literal" property is enabled then ensure that the "Prevent XSS" property is disabled.


7) Add your button, add a rule "when button is clicked".


8) Under the "When button is clicked" rule add a transfer data and configure the transfer data.


9) In the transfer data configuration, locate the control you specified in step 6 (during this example, this control would be the Data Label Code control) and paste your code in its field.


Note: This will also work if you pass a expresison containing the code into the transfer data rule instead of passing the code into the transfer data rule directly.
10) Save, finish, check in, run the form and press the button.


 


The code used in step 9 for this example was as follows:


Code explained: This code specifically targets the view called "Single Control View 123" and finds the control contained in that view called "Data Label 123" and then sets the controls background color to red.


Note: The css can be altered to your requirements.


<script>$('[name*="Single Control View 123"]').find('[name*="Data Label 123"]').css({'background-color':'blue'})</script>


 


Regards


 

Reply