Setting an Options list drop down value dynamically

The recommended method of customizing your AppGini-generated application is through hooks. But sometimes you might need to add functionality not accessible through hooks. You can discuss this here.
Post Reply
dkano
Posts: 10
Joined: 2023-09-27 09:48

Setting an Options list drop down value dynamically

Post by dkano » 2023-10-06 16:36

I have a speed_distance_units field in a sessions table, set with an options list to one of three different values. The exact same field and options are set for each user in a rider table, so I can display charts, reports etc. in the units they prefer. When they create a new session record, I'd like to set this field in the session record automatically to the preference they set in the rider record.

I know how to do this with a regular html table select widget with javascript, but the option list select elements are being set by appgini to use some fancier code:

Code: Select all

<script>jQuery(function() { 
					jQuery("#speed_distance_units")
						.addClass('option_list')
						.select2({ 
							minimumResultsForSearch: 5,
							sortResults: AppGini.sortSelect2ByRelevence
						}); 
				})</script>
I suspect that fact is stopping my fairly simple javascript from working:

Code: Select all

$j(function(){
	$j('#speed_distance_units').val('Knots');
});
I've tried variations on the code above, including not using jQuery with the same result.

As I wrote this question, I discovered that the value of the drop down had changed, but the value displayed in the widget had not! When I click on the widget and it drops down, the selection set by javascript is highlighted. But when I click elsewhere to remove the focus, the drop down continues to display the top item:
appgini forum dropdown question.png
appgini forum dropdown question.png (15.88 KiB) Viewed 2587 times
Any help will be greatly appreciated!

BTW, I know there are other ways to manage user unit preferences, including saving them all in the same units and displaying their preference on the various user interfaces. However that seemed difficult in appgini and still have the units displayed in the table view what they prefer.

dkano
Posts: 10
Joined: 2023-09-27 09:48

Re: Setting an Options list drop down value dynamically

Post by dkano » 2023-10-06 16:42

As I sat rereading the above question post, I realize one simple solution I could try would be to hide the units selection altogether! The users would probably just assume that the units used were the ones they prefer and enter them without a second thought. Then I can set the units in the (already written) custom code that sets some calculated fields for them. I guess I'll use that approach for now at least. But I would love to know how to do this nonetheless!

Post Reply