Hey everyone,
I'm experiencing something I have been using very manual work arounds for, but found that my workaround isn't foolproof either. The gist is that I can't find a way to get my dropdown fields fully flexible in it's environment. The values need to all be there regardless of lookup, and it should auto select only if it finds results - otherwise it should be blank.
My use case:
Form that mostly populates with data to assist user with historical references (if it exists), and if no historical data exists, the dropdowns should be blank and the user fills in with regular choices.
My issue:
For an example, let's boil this down to it's simplest form. A dropdown containing Yes or No values. Let's say the lookup rule pulls 'Yes' for this dropdown field. Without Append Results checked, that would be the only choice available for this field. If the user would want to then change this to 'No' it would have to come from the choices built within the field itself.
So far so good. We have a dropdown with Yes/No values inside it that also populates from a lookup rule that appends its values to the dropdown.
The first issue after appending values is it does not auto select the dropdown when it pulls in the results. Rather it appends it to Option 1 in the dropdown. So lets take a look at a few examples in action.
Case1: Lookup Rule finds 'No' for result. Choice for No moved to spot 1
Case2: No results found, dropdown list reverts to it's default choices within the field.
Case 3: Result it finds in lookup rule is empty (not null, but empty)
Going off these results I decided that it was best to do what others have done, and use javascript to selectindex 1 after lookup rule has run.
Snippet of code I'm using for a portion of these dropdowns:
$(document).on("lookupcomplete", function (event) { //Set baseLookup to ruleID of lookup 0 in api and it will update all rules appropriately var baseLookup = 7957; if (event.ruleId == baseLookup) { $("#Field6, #Field12").prop("selectedIndex", 1).trigger('change'); console.log("base " + event.ruleId); } //API Lookup 1 if (event.ruleId == baseLookup +1) { $("#Field86, #Field87, #Field95, #Field97, #Field94").prop("selectedIndex", 1).trigger('change'); console.log("1base " + event.ruleId); }
This makes it work in all cases where it will start selecting even with appended results; except, when there are no results, or the results found are null. When this is the case, it will always just auto select 'Yes' as it is the first value of the dropdown and that can be highly misleading.
Potential solutions:
JavaScript solutions
Is there a way in JavaScript to track lookup rule results? If I can find a selector to access that information I can update my JavaScript to only selects index 1 IF the lookup was not null. I was digging through the jsonForm in browser debugger and was not able to find the right selectors to use to see if a lookup rule had results or not.
There is also the jQuery EditableSelect library which turns single line fields into dropdowns; however, I tried this too and it was only populating with results from the database and did not have an obvious way to add line items to my database results.
Another thought I have not pursued is to maybe instead of choosing 'Append choices to lookup results', I add the options to each dropdown after the lookup rule has run through JavaScript? That way if it does find something it will be auto selected by default and the whole: select index 1 can be avoided. I'm assuming the root of the behavior not auto selecting is due to the fact that there are multiple choices. So if the lookup rule runs first and the choices are added after - then maybe that will work too?
SQL solutions
Forcing my views or stored procedures to pass an empty value instead of a null value in the cases where there are no results, or the results it finds are null?
Forms solutions
1. Add a single line text field for every dropdown
2. Switch the lookups to those single line fields.
3. Have JavaScript (since formulas dont exist on dropdown fields) read each value, and if it is not empty, update the associated dropdown's selected value to what matches in the single line text.
4. Somehow prevent JavaScript from changing user's input if the lookup rule gets triggered again (I believe I read something about changing the field's attribute to ('vo', 'k') to accomplish this?)
$('#q2 input').focusout(function(){ $(this).attr('vo','k'); });
Other solutions
There could be something glaringly obvious that is out there that maybe some of you know. If there are workarounds you've done or think will work, I would greatly appreciate any of your input. This seems like such a small and simple thing to have sunken so much time into.
Thank you!