user2511599
user2511599

Reputation: 852

Yii2 AutoComplete in GridView

I've installed yii2-jui via composer.

I would like to use AutoComplete in 2 different models, once in mother, once in a foreign model. It's about Lands.

Now in mother (views/land/index):

use yii\jui\AutoComplete;
use app\models\Land;

[
    'attribute' => 'name',
    'filter' => AutoComplete::widget([
        'model' => $searchModel,
        'attribute' => 'name',
        'clientOptions' => [
            'source' => ArrayHelper::map(Land::find()->select('id, name')->orderBy('name')->all(), 'id', 'name'),
            'autoFill' => true,
            'minLength' => 2
        ],
    ]),
],

The problem is, that it doesn't work, because of orderBy of course the keys are not from zero upwards. As soon as I reset keys, it begins to work. Now in mother it's not "really" a problem (actually it IS still a problem), but as soon as I want to use it in a "foreign" model, where I have to search actually for a key (id), instead of the name of the Land, it wont work. What am I missing? Can you please point me to the right direction? Many thanks!

Upvotes: 1

Views: 1177

Answers (1)

user2511599
user2511599

Reputation: 852

I've figured it out:

in mother view:

use yii\jui\AutoComplete;
use app\models\Land;

[
    'attribute' => 'name',
    'filter' => AutoComplete::widget([
        'model' => $searchModel,
        'attribute' => 'name',
        'clientOptions' => [
            'source' => Land::find()->select(['name AS value', 'name AS label'])->orderBy('name')->asArray()->all(),
        ],
    ]),
],

in foreign view:

use yii\jui\AutoComplete;
use app\models\Land;

[
    'attribute' => 'land_id',
    'filter' => AutoComplete::widget([
        'model' => $searchModel,
        'attribute' => 'land_id',
        'clientOptions' => [
            'source' => Land::find()->select(['id AS value', 'name AS label'])->orderBy('name')->asArray()->all(),
        ],
    ]),
],

Upvotes: 1

Related Questions