Dropdown List + Text Input

Hi,

I’m stuck with this problem for weeks now.

I have a dropdown that loads the names from my model and it works great:


return ArrayHelper::map($locations, 'location_id', 'name');

but in my same model I have other fields such as address and I have a text input that when you select the name in the dropdown list it should load the address of the selected dropdown in the text input.

I’m not sure how to do that, I already tried to return:


return ArrayHelper::map($locations, 'location_id', 'name', 'address');

but it doesn’t work.

Any clue? Thanks for the help.

edit see below answer

Thanks for your prompt response.

This is my database - https://www.dropbox.com/s/4a4r6lm8x9os56o/Screenshot%202015-05-16%2022.02.26.png?dl=0

So I’m getting the name already and the address is associated with it. My dropdown is loading the name but I can’t get the id in order to request the model the address of that location.

Do you have any sample code to help me?

I just fixed my solution but it’s a not beautiful solution.

This is my model:




return ArrayHelper::map(

    			Location::find()->select(

    			['location_id', 'name', 'address', 'suburb', 'state', 'postalcode'])

    			->where(['location_id' => $usersMasterLocation])

    			->where(['visibility' => 'on'])

    			->asArray()->all(),

    			'location_id',

    			function($element){

    				$states = States::find()

    						->where(['state_id' => $element['state']])

    						->asArray()->all();

    				

    				return $element['name'] ." - ADDRESS: ". $element['address'] .", ". $element['suburb']

    				.", ". $states[0]['state'] .", ". $element['postalcode'];

    				

    			});



and in my view I did this:




'$("document").ready(function(){ 

		var locationName;

		jQuery("#job-pickup_location").bind("typeahead:selected", function(ev, suggestion) {

			var split = (suggestion.toSource()).split(" - ADDRESS: ");

			var address = split[1].slice(0, -3);

			$("#pickup_address").val(address);

			var location = split[0].substring(9, address.lenght);

			locationName = location;

			$("#job-pickup_location").val(location);

		});

		

		$("#job-pickup_location").blur(function(value) {

			$("#job-pickup_location").val(locationName);

		});


	});'	



It’s not elegant at all but gets the job done.

So you have a dropdown list that you want to show the name and the address together… i was way off. But you can do that much more simply

In your model


public function getAddress() {

    	return $this->name . '  - ADDRESS: ' . $this->address  . ', ' . $this->suburb  . ', ' . $this->stateRelation->name . ',  ' . $this->postalcode;

	}

in your form




   <?= $form->field($model, 'location_id')->dropDownList(

ArrayHelper::map(Location::find()->where(['location_id' => $usersMasterLocation,'visibility' => 'on'])->all(),'location_id','address')); ?>



Thank you so much. Do can you send me your email? I want to hire someone to be my consultant when I have questions like that. Posting on FOrum sometimes takes too long for people to answer.

glad that worked.

I don’t have the availability to be on retainer with you as I already code 60+ hours a week for my company. I answer questions when I have a break or just randomly for free. I also don’t take money from people to do coding help only for actual projects. You can message me if you have a question that isn’t getting answered and I’ll try my best to answer it for you.

Thank you, your help if very well appreciated. I just posted another thing on Forum related to dynamic input LOL