V
V
Victor Umansky2017-05-28 21:58:44
Yii
Victor Umansky, 2017-05-28 21:58:44

Yii2 autocomplete kartik Typeahead?

Cheers to everyone!
Help me understand the request, using the widget from the Typeahead card. What do I need in the field I drive the city and in the tooltip appears, separated by a comma, the area related to the city
Table Profile
id | city_name | region_name | city_id | region_id
view

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' => 'Введите название города...'
            ],
        ]);

controller
first option
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'];
        });
    }

second option
$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;

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Maxim Fedorov, 2017-05-29
@Uman

oh you did something. If I understand the problem correctly, then something like this:

$cities = Profile::find()
    ->select('CONCAT(city_name, ", ",region_name) AS value')
    ->filterWhere(['like', 'city_name', $q])
    ->asArray()
    ->all();
return \yii\helpers\Json::encode($cities);

V
Viktor Umansky, 2017-05-30
@Uman

If anyone needs it like in my screenshot !!! Here we copy!
If you know how else to improve, then tell me!
view

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>',
                    ]
                ]
            ],

        ]);

controller
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);

    }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question