Automatically set index for dropdown (Lookup field)

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
pfrumkin
Veteran Member
Posts: 157
Joined: 2020-02-18 17:58
Location: Albuquerque, New Mexico USA

Automatically set index for dropdown (Lookup field)

Post by pfrumkin » 2021-01-21 23:57

Use Case
Edit and save a record, then click a button to create a new record based on a handful of values in this original record.

One of the fields I want to prepopulate from the original record to this new record is a Lookup field. So click the button to create the new record, I want this dropdown to be preselected to match the original record. I am trying to do this in the tablename-dv hook. I have this code <snipped> in the -dv hook

Code: Select all

ob_start(); ?>
<script>
	$j(function() {
		$j('#Donor-container').select2('val','<?php echo $donor?>');
	})
</script>
<?php
$new_layout = ob_get_contents();
ob_end_clean();
I can see by page View Source that the $donor value is the index I want, so that value is getting set in the hook PHP and sent to the page. I can't figure out how to trick the Donor-container to set the value. One approach I have not looked at is doing a record copy, then clearing values I want to clear, so maybe that's better. But I don't want to save the record to the database immediately, I want the user to do edits prior to save. Here is the generated code for the Donor field I'm trying to set. I don't know enough JQuery to understand these ins and outs.

Code: Select all

function Donor_reload() {
	
	$j("#Donor-container").select2({
		/* initial default value */
		initSelection: function(e, c) {
			$j.ajax({
				url: 'ajax_combo.php',
				dataType: 'json',
				data: { id: AppGini.current_Donor.value, t: 'Vehicles', f: 'Donor' },
				success: function(resp) {
					c({
						id: resp.results[0].id,
						text: resp.results[0].text
					});
					$j('[name="Donor"]').val(resp.results[0].id);
					$j('[id=Donor-container-readonly]').html('<span id="Donor-match-text">' + resp.results[0].text + '</span>');
					if(resp.results[0].id == '{empty_value}') { $j('.btn[id=Donors_view_parent]').hide(); }else{ $j('.btn[id=Donors_view_parent]').show(); }

					if(typeof(Donor_update_autofills) == 'function') Donor_update_autofills();
				}
			});
		},
		width: '100%',
		formatNoMatches: function(term) { /* */ return 'No matches found!'; },
		minimumResultsForSearch: 5,
		loadMorePadding: 200,
		ajax: {
			url: 'ajax_combo.php',
			dataType: 'json',
			cache: true,
			data: function(term, page) { /* */ return { s: term, p: page, t: 'Vehicles', f: 'Donor' }; },
			results: function(resp, page) { /* */ return resp; }
		},
		escapeMarkup: function(str) { /* */ return str; }
	}).on('change', function(e) {
		AppGini.current_Donor.value = e.added.id;
		AppGini.current_Donor.text = e.added.text;
		$j('[name="Donor"]').val(e.added.id);
		if(e.added.id == '{empty_value}') { $j('.btn[id=Donors_view_parent]').hide(); }else{ $j('.btn[id=Donors_view_parent]').show(); }
		if(typeof(Donor_update_autofills) == 'function') Donor_update_autofills();
	});
	if(!$j("#Donor-container").length) {
		$j.ajax({
			url: 'ajax_combo.php',
			dataType: 'json',
			data: { id: AppGini.current_Donor.value, t: 'Vehicles', f: 'Donor' },
			success: function(resp) {
				$j('[name="Donor"]').val(resp.results[0].id);
				$j('[id=Donor-container-readonly]').html('<span id="Donor-match-text">' + resp.results[0].text + '</span>');
				if(resp.results[0].id == '{empty_value}') { $j('.btn[id=Donors_view_parent]').hide(); }else{ $j('.btn[id=Donors_view_parent]').show(); }

				if(typeof(Donor_update_autofills) == 'function') Donor_update_autofills();
			}
		});
	}
}
Thanks in advance!

~Paul
AppGini 5.84

pfrumkin
Veteran Member
Posts: 157
Joined: 2020-02-18 17:58
Location: Albuquerque, New Mexico USA

Re: Automatically set index for dropdown (Lookup field)

Post by pfrumkin » 2021-01-26 21:24

I solved this by adding a parameter to the querystring when creating the new record. Inspiration was from the AppGini Helper Library documentation (thanks Jan!) https://appgini.bizzworxx.de/appgini/ho ... able-view/.

On the page with the original record, I have a button to create the new record. This is an intermediate step, going to a PHP script to do some other processing, then redirecting to the new record in detail view (with the add_new=1 parameter to create a new record). The magic is that when I do the redirect, to add &filterer_Donor=5399, where Donor is the field in the table and 5399 is the index of the Donor record I want to assign to my new record.

~Paul

pfrumkin
Veteran Member
Posts: 157
Joined: 2020-02-18 17:58
Location: Albuquerque, New Mexico USA

Re: Automatically set index for dropdown (Lookup field)

Post by pfrumkin » 2021-01-27 14:05

One last point, I found this very helpful explanation for why I couldn't get this to work, https://select2.org/programmatic-contro ... lear-items. Basically the AJAX had not run yet to get the data and populate the control, so the control wasn't there for me to set.

~Paul

Post Reply