We have customers looking to use Forms to handle employment contracts. The form starts with a location where the employee id is entered which does a database lookup to find the employee's first name, last name, and job title and then puts those values in a table at the top of the form. Beneath that is a custom HTML field that contains all of the verbiage for the contract. The customer is needing the custom HTML to dynamically include the employee values that were returned from the lookup. We can include variables in the custom html but those do not update based on the values in the table. Is there a way to have the custom HTML dynamically update with the variable values from a lookup?
Question
Question
Replies
Hi Beau,
Here's how I worked around Forms to create the table.
- Create a view to put all values into a single comma delimited column
- Create a drop-down list for the values and give it a name in the CCS Class field in the Advanced tab, such as: forMyDatabase
- Create a field and rule to initiate the SQL lookup to populate the drop-down.
- Create a multi-line field to store all the returned SQL values if you need to pass them to Workflow or between forms. In the code below, I've called it hiddenTextBox.
So far, this should be run-of-the-mill. Next...
- Hide the drop-down field and multi-line box by setting the CSS: .forMyDatabase {display: none;} and .hiddenTextbox {display: none;}
- The next thing to do is take the comma delimited values from the drop-down, split them and load them into a table. The problem here is the code might run before the drop-down has completely populated, so a delay is required. Your code might look something like this:
function buildTable() { //alert('I am an alert box table!'); var resultNumber = $('.forMyDatabase').find('option').size(); var textArea =""; for (var i = 2; i <= (resultNumber); i++) { var xItem = $('.forMyDatabase').find('option:nth-child(' + (i) + ')').text(); textArea += chequeItem + '\n'; //code to split xItem into individual variables if (xItem.length > 0) { $('#q13 tr').last().after('<tr><td>' + var1 + '</td><td>' + var2 +'</td></tr>'); } } $('.hiddenTextBox textarea').val(textArea); } //and a delay... function delayedPopulate() { setTimeout(function(){ buildTable(); },1500); //finally, code to trigger delayedPopulate() such as on.click or on.blur or on.change. The delay is only required if you want the dynamic HTML to work without user intervention.
-Ben
Ben,
A little tip, I have found that when using a lookup it will change a field and cause javascript "Change" action. So I usually use a secondary field I do not care about to be filled by lookups so that when its filled I know the lookup performed and to initiate the next step in the process. This would eliminate the need to add a random delay that may or may not be enough time and only make it take however short amount of time it can complete.
Hi Kenneth,
That sounds like a fine idea. I'll gave it a whirl and it seems to work in my test environment with a small sample :)
-Ben
Turns out I still need a delay if using a "refresh" button. However, not on initial load so it's still a win. And the required delay isn't as long a previously required. (nor as reliable but that could be my coding skills...)
Glad to hear it, I am not sure what you mean by refresh, i guess you are having problems with timing when the page reloads?
Whatever the case, maybe in another situation it might be handy to know that this is the case. I havent gotten around to using javascript promises and stuff like that, but maybe thats another option for handling these types of things.
Kenneth,
You and your lack of mind-reading... Sheesh.
I'm using the script above to build a HTML table (based on an SQL view.)
Outside the Forms process, the contents of the view is subject to change. When this happens, the Forms operator need to click a "refresh" button to reload the tables. I don't want the whole page reloaded, just the lookups.
For the record, the view is based on LF metadata data and it's the Forms operator who uses Web Access to change the metadata. Which in turn populates the table on the forms.
-Ben
These are great ideas for building a dynamic table but I just need the custom HTML section to automatically update based on the new value of a field. Below is the test form I am using to build the functionality but it gives a good idea of what I am looking for.
When the user picks the department from the Department dropdown, it does a SQL database lookup and fills in the Approver field. The Approver field has a variable called Approver that then is called in the custom HTML field. I need the variable in the custom HTML field to properly update whenever the Approver field is updated. I understand how to use Javascript to call .on events but am unsure of how to force the custom HTML to refresh.
Beau, were you able to come up with a solution to this? We are wanting to do the same thing and have the same questions.
Unfortunately not. This customer was wanting this for contracts and they were able to update the verbiage to have the name in a field at the top and then reference it in the html as "the above mentioned...".
Hi, Blake and Beau.
I have the same need for replacing tokens in Custome HTML fields. I came up with a solution. Because I'm not an expert at javascript/jQuery, my solution is a little bit convoluted. So, if you can improve on this, I'd like to see what you come up with.
My solution uses 3 fields: 1) a field that triggers a database lookup, 2) a field that gets populated by the lookup, and 3) a Custom HTML field.
I modified the Custom HTML field to include the token that holds the value of field 2, and replaced that token string in the HTML with <span class=tokenname></span>(where tokenname is the name of my token without the brackets and "/dataset/"). (I added the span 3 times just to see if it would change in all three locations.)
Then on the CSS and Javascript tab, I used the code below. When I enter a value in field 3, the database lookup fires and fills in field 2, which is a multi-line field. That in turn triggers the function that replaces the string in the HTML field. The function searches for all <span> elements with the varTestMulti class and replaces them with the text in field 3.
I've done some testing, and so far it works. I now have to show it to the customer to get their sign off.
$(document).ready( function(){ // when choice selected, insert the correct paragraphs $('#q2 textarea').change(myStrReplace); //****************************************************** // FUNCTIONS //****************************************************** function myStrReplace() { var newText = $('#q2 textarea').val(); $(document).find("span.varTestMulti").replaceWith( newText ); } });
What about when it is saved to the repository? Does the Custom HTML field retain the values?
It should. The HTML was replaced. However, I haven't checked that out yet. I'll let you know when I do (hopefully today or tomorrow).
Bummer... no, it doesn't. However, I came across a post from Eric Anderson (https://answers.laserfiche.com/questions/64063/Answer-Using-DIV-to-place-storeable-input-fields-into-HTML-text-blocks ) that might work, though I worked around that issue, so I don't have the need anymore.
On to other issues .... !
Ben,
Have you had any luck getting this to commit values after the form is submitted and stored?
Kind Regards,
Hi Kris,
The values would need to be copied to form fields. Either a table or input box, for example. there are other forum posts on programmatically populating those. Good luck!