Hi guys,
I have this pretty interesting question for you.
I have created two inputs, the first one is country input which is a drop down widget allowing the user to select his country.
The second one is an CJuiAutoComplete widget in which the user should start writing the name of a city(term) and the auto complete should show a list with cities which are within the borders of the country input.
I have in my DB a table containing countries and a table containing cities where each city is identified by the country id in which it resides.
I have implemented it to “work” so far without filtering the country(filtering only by a fixed string) but the problem is that I do not see a way to access the the country drop down input from the auto complete callback function thus rendering it impossible for me to filter the auto complete results by country. What I have done so far is to compare the city country name to the ‘Netherlands’ so if I start writing Amsterdam by typing “Ams”, I would only get cities in the Netherlands.
Here is the code:
The view:
<div class="row">
// Drop down country input
<?php
$this->widget('application.extensions.countrySelectorWidget', array(
'useCountryCode' => false,
'name' => Chtml::activeName($locationModel, 'country'),
'id' => Chtml::activeId($locationModel, 'country'),
'firstEmpty' => true,
));
?>
</div>
<div class="row">
// Auto complete city input
<?php
$this->widget('zii.widgets.jui.CJuiAutoComplete', array(
'attribute'=>'name',
'name'=>'city',
'model'=>Cities::model(),
'sourceUrl'=>array('autocompletecitylookup'),
'options'=>array(
'minLength'=>'3',
),
));
?>
</div>
If you take a look at my callback function you should see what I want to do.
public function actionAutoCompleteCityLookup () {
$results = array();
if(Yii::app()->request->isAjaxRequest && isset($_GET['term'])) {
$criteria = new CDbCriteria();
$criteria->compare('name', $_GET['term'], true);
$model = new Cities;
foreach($model->findAll($criteria) as $m)
{
// Here I check whether the city is within the country I want
// What I want to accomplish is to replace 'Netherlands' with the input from the countries drop down list
if ($m->country->name == 'Netherlands')
$results[] = $m->name;
}
sort($results);
echo CJSON::encode($results);
}
}
I am not sure if this is possible since the function is a callback called from jquery autocomplete (because CJuiAutocomplete is a wrapper of this function) so basically the only input I have for the callback is the $_GET[‘term’] so there is no way for me to get the data from the form and the countries drop down respectively.
Cheers!