This article explains you below things
- How to create a button using data label with JavaScript.
- Trigger a Button Click event to run smart form rules from JavaScript.
- Update a Data label value from JavaScript.
Create a simple button using data label and java script and show a sample alert on its click.
Step 1: Drag and Drop a data label on smart form.
Step 2: Now in Properties Set Literal property to true
Step 3: Navigate to Expression property click on ellipse button to add a new expression, copy below js script and click ok
<button onclick="javascript:alert('You Clicked a DataLabel Button!!!');">This is a Data Label Button</button>
That’s it, Now Run the form and you see a button created from a data label and JavaScript. Screen shot below.
Now let’s see How to Trigger a Button Click event to run smart form rules from JavaScript.
Step 1: Drag and Drop a new Button Control.
Step 2: Add click event to the button added in Step 1, and add a rule to show message “Hello from Button Control”
Step 3: Now run the form and open developer mode (hit F12) and inspect the button element, copy the id of button that you have added in Step 1
Step 4: once you have the id, you need to modify the script of data label button as below.
<button onclick="javascript:document.getElementById('00000000-0000-0000-0000-000000000000_79af447e-1e21-9933-8152-588f18d59d72').click()">This is a Data Label Button </button>
**00000000-0000-0000-0000-000000000000_79af447e-1e21-9933-8152-588f18d59d72 has to be replaced with id you copied from step 3
That’s it, now when you click Data Label Button, you’ll see Button Control’s rules getting executed.
Now Update a Data label value from JavaScript.
This is pretty much similar as above
Drag and drop a new dataLabel find its id using developer mode and replace the datalabel button script as below.
<button onclick="javascript:document.getElementById('00000000-0000-0000-0000-000000000000_f62b3928-a6e5-e16d-794a-9e9f4588bb2b').innerHTML=’Dynamically Set Text’” ()">This is a Data Label Button </button>
That’s it when you click data label button, you see the text of data label getting changed to “’Dynamically Set Text”.
This is how we can play around with jQuery/javascript in smartforms.
Awesome Vijay, I would add that at the form level. The control’s GUID (00000000-0000-0000-0000-000000000000_ part) changes when you include the view into a form.
LikeLike
Thanks Manju,
You are right.
we need to find the id of the control once we get the view added in a form.
LikeLike
Hi Vijay,
Am new to the K2 smartform and workflow, Am trying to create close button in Sharepoint document library edit form.
If the user click the cancel button, uploaded document need to erased and the window also should be closed.
How can i acheive that???
LikeLike
Are you trying to do this sharepoint?
LikeLike
Hi Vijay,
Will this technique work in List view ??? I need to keep buttons for each row with same rule
LikeLike
Hello Joe, You can do that using the same technique.. Add a new column to the List View and drag data label into that column and check literal checkbox. and uncheck prevent xss .. in expression write the HTML..
LikeLike
Yes .. We can use the same technique in List View as well.
LikeLike