Set dropdown SELECTED option based on db read?

kavalec 02 Dec, 2016
Chrono nOOb here

I can successfully use dynamic dropdowns - from a multi-read - and write the user selection to my data table.

Next up I need to read that row and mark the relevant option as SELECTED.

I have both dbreads but I don't see any obvious hook back into the form.

If this were just native PHP I could wing it, but echo \GCore\Helpers\Html::input($field["name"], $field); doesn't leave me any control. Is my only way in writing my own loop?

Thanks!
GreyHead 03 Dec, 2016
Hi kavalec,

If the drop-down exists in your form and has the same name as the data you are loading then the value will automatically e selected. The problem with a dynamic drop-down is that the drop-down may not exist - or not with the correct set of options added. In that case your On Load event needs to check the value of the first drop-down, and load the corresponding option set for the second one. Then the automatic selection will work OK.

Bob
kavalec 03 Dec, 2016
Hi Bob

If the drop-down exists in your form and has the same name as the data you are loading then the value will automatically be selected.

That didn't seem to be happening, so this is what I did in the custom code

Note: subjectlist is the multi-read from the subjects, StudentSubs is the dbread of the previously saved rec.

If there is/was an automatic way to make this work, is there an example?

Thanks!

<div class="form-group gcore-form-row" id="form-row-multi-13">
	<div class="gcore-subinput-container" id="fitem-SubjectSelected1">
		<label for="SubjectSelected1" class="control-label gcore-label-left required_label">Please select your first subject (required) <i class="fa fa-asterisk" style="color:#ff0000; font-size:9px; vertical-align:top;"></i></label>
		<div class="gcore-input pull-left gcore-sub-input gcore-display-table" id="fin-SubjectSelected1">
			<?php
			
			
			$keys = \GCore\Libs\Arr::getVal(\GCore\Libs\Arr::getVal($form->data, explode(".", "subjectlist")), explode(".", "[n].cf_id"));
			$values = \GCore\Libs\Arr::getVal(\GCore\Libs\Arr::getVal($form->data, explode(".", "subjectlist")), explode(".", "[n].Subject"));
			
			$myoptions = array_combine($keys, $values);
			
      		        echo '<select name="SubjectSelected1" id="SubjectSelected1" size="0" class="validate[\'required\'] form-control A" title="" style="" data-load-state="" data-tooltip="">';
			$thisSelect = $form->data['StudentSubs']['SubjectSelected1'];
			displaySelection($myoptions, $thisSelect );
			echo '</select>';
echo '<p>'.$thisSelect.'</p>'; 
			
			function displaySelection($options, $opt) {
                            echo  '<option value="">';
			    foreach($options as $key=>$data) {
			    	    if ($key == $opt) {
				        $selected = 'Selected';
				    } else {
				        $selected = '';
				    }
			            echo '<option value="'.$key.'"' .$selected. '>' .$data.'</option>';
			        }
			}
			?>
		</div>
	</div>

	<div class="gcore-subinput-container" id="fitem-SubjectSelected2">
		<label for="SubjectSelected1" class="control-label gcore-label-left required_label">Please select your second subject (optional)</label>
		<div class="gcore-input pull-left gcore-sub-input gcore-display-table" id="fin-SubjectSelected2">
			<?php

      		        echo '<select name="SubjectSelected1" id="SubjectSelected2" size="0"  title="" style="" data-load-state="" data-tooltip="">';
			$thisSelect = $form->data['StudentSubs']['SubjectSelected2'];
			displaySelection($myoptions, $thisSelect );
			echo '</select>';
			echo '<p>'.$thisSelect.'</p>'; 
			?>
		</div>
	</div>

	<div class="gcore-subinput-container" id="fitem-SubjectSelected2">
		<label for="SubjectSelected1" class="control-label gcore-label-left required_label">Please select your third subject (optional)</label>
		<div class="gcore-input pull-left gcore-sub-input gcore-display-table" id="fin-SubjectSelected3">
			<?php
			
      		        echo '<select name="SubjectSelected1" id="SubjectSelected3" size="0" title="" style="" data-load-state="" data-tooltip="">';
			$thisSelect = $form->data['StudentSubs']['SubjectSelected3'];
			displaySelection($myoptions, $thisSelect );
			echo '</select>';
			echo '<p>'.$thisSelect.'</p>'; 
			?>
		</div>
	</div>
</div>


<div class="form-group gcore-form-row" id="form-row-Submit"><div class="gcore-input gcore-display-table" id="fin-Submit"><input name="Submit" id="Submit" type="submit" value="Submit" class="btn btn-default form-control A" style="" data-load-state="" /></div></div>
kavalec 03 Dec, 2016
PS: obviously the above is an unholy hybrid of CF code and my own. ;-)
GreyHead 04 Dec, 2016
Hi kavalec,

I cant work out how this is set up. You appear to have three drop-downs with the same name (but different IDs). That won't work as only the value from the last one will be submitted.

If you give them different names, or an array name then they will work and all the results will be submitted.

I think that you don't need most of this custom code. You can use the Dynamic Data settings in the drop-down events to load the options set; and the values in the $form->data array will set the selections - just how depends on the the format the data is being loaded in.

Bob
kavalec 04 Dec, 2016
[attachment=1]create.jpg[/attachment][attachment=0]form.jpg[/attachment]OK

I am starting this form, and it's table, from scratch.
I add three dropdowns: SubjectSel1, SubjectSel2, and SubjectSel3
Save the form, delete the cache, and try to create table.

The added fields don't happen. And I suspect this is where my problems begin.

See attached screenshots. What am I doing wrong?
GreyHead 05 Dec, 2016
Hi kavalec,

I don't see why they aren't appearing automatically :-( . . . but that's no problem you can add them manually in the empty rows after 'modified'.

Bob
This topic is locked and no more replies can be posted.