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.
skworden:
you need JS / jQuery with Ajax to do this as it is a client side function.
To get this clear you want to do the following:
[list=1][]Have a model / view / controller action that creates locations with address. ($loctions)[ ]Have another model / view /controller action that will:
Have a dropdown that list locations from your database.
You use the dropdown to select a related location_id (name is shown).
You will use the data from the location selected to fill address text input fields on the same page instantaneously?
[/list]If this is what you want to do, does the location (the one you select in the dropdown) have an address associated with it i.e. city state zip?
or
You have a location dropdown with addresses then you will fill another dropdown with the location addresses?
either way…
[list=1][]Post model name and fields for $location.[ ]Post controller name for $location.[]Post model name and fields for the related one that needs an address[ ]Post form for related model that needs the address fields to be fields.[/list]
I have done this numerous times and it is easy to do you just need to post more info and answer all of the questions. if not all i can do is assume and give a general reference that wont really help you or others that would stumble upon this.
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')); ?>
skworden:
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