Typeahead::widget([
'name' => 'city',
'id' => 'cityTypeahead',
'scrollable' => true,
'dataset' => [
[
'display' => 'Наименование',
'remote' => [
'url' => Url::to(['/search/city']) . '?q=%QUERY',
'wildcard' => '%QUERY',
'rateLimitWait' => 1000
],
'limit' => 999,
'templates' => [
'notFound' => '<div class="text-danger" style="padding:0 8px">Ничего не найдено.</div>',
]
]
],
'pluginOptions' => [
'highlight' => true,
'minLength' => 3,
'val' => ''
],
'options' => [
'placeholder' => 'Введите название города...'
],
]);
public function actionCity($q = null)
{
$cities = Profile::find()
->filterWhere(['like', 'city_name', $q])
->asArray()
->all();
return ArrayHelper::getColumn($cities, function ($element) {
return $element['city_name'] . ', ' . $element['region_name'];
});
}
$cities = Profile::find()
->from(['city' => Profile::tableName()])
->leftJoin(Profile::tableName() . ' region', 'region.id=city.city_id')
->select([
'city.id',
'city.city_name AS name',
'region.id AS rid',
'region.region_name AS region_name',
'CONCAT(city.city_name, ", ",region.region_name) AS fullName'
])
->filterWhere(['like', 'city.city_name', $q])
//->andWhere(['>', 'city.parent_id', 0])
//->orWhere(['parent_id' => $q])
//->asArray()
->all();
$response = Yii::$app->getResponse();
$response->format = Response::FORMAT_JSON;
Typeahead::widget([
'name' => 'city',
'options' => ['placeholder' => 'Все города'],
'pluginOptions' => ['highlight' => true],
'dataset' => [
[
'datumTokenizer' => "Bloodhound.tokenizers.obj.whitespace('value')",
'display' => 'value',
//'prefetch' => $baseUrl . '/samples/countries.json',
'remote' => [
'url' => Url::to(['/search/city']) . '?q=%QUERY',
'wildcard' => '%QUERY'
],
'templates' => [
'notFound' => '<div class="text-danger" style="padding:0 8px">Ничего не найдено.</div>',
]
]
],
]);
public function actionCity($q = null)
{
$cities = Profile::find()
->select(['concat(city_name, ", ",region_name) as value'])
->filterWhere(['like', 'city_name', $q])
->distinct()
->asArray()
->all();
$out = [];
foreach ($cities as $city) {
$c = explode(", ", $city['value']);
$outs[] = $c[0];
$outs[] = $c[1];
$city_name = implode(", ", $outs);
$out[] = ['value' => $city_name];
}
echo Json::encode($out);
}