Autocomplete from database ?

i am new in yii2… I’m trying to make an autocomplete , but can not find a good example

these are my tables :

ge_tciud:

ciud_codi

ciud_name

ge_tpers:

pers_codi

pers_name

pers_ciur --> here i need autoplete


this is my code :





     <?php

     use yii\web\JsExpression;

   


    $data = GeTciud::find()

    ->select(['ciud_nomb', 'ciud_nomb','ciud_nomb as id'])

    ->asArray()

    ->all();




 echo AutoComplete::widget([

    'name' => 'pers_ciur',

    'id' => 'ddd',

    'clientOptions' => [

    'source' => $data,

    'autoFill'=>true,

    'minLength'=>'4',

    'select' => new JsExpression("function( event, ui ) {

        $('#getpers-pers_ciur').val(ui.item.id);

     }")],

     ]);

     ?>


    <?= Html::activeHiddenInput($model, 'pers_ciur')?>




but this does not work.

might be wrong ?

plis help me to fix this thx … (sorry if my English is bad, I 'm learning. )




$data = GeTciud::find()

    ->select(['ciud_nomb', 'ciud_nomb','ciud_nomb as id'])

    ->asArray()

    ->all();



I’m think, you’ve forgotten where expression. And you should to use LIKE in where condition.

thx for reply … buddy can u make example for me pls :D

Hi,erickMartinez ,

First of all, in your form write the following:




use yii\jui\AutoComplete;

use yii\web\JsExpression;

use app\models\Country;




<?php

   


    $data = Country::find()

    ->select(['name as value', 'name as  label','id as id'])

    ->asArray()

    ->all();




print_r($data);exit;

    echo AutoComplete::widget([

    'name' => 'Country',

    'id' => 'ddd',

    'clientOptions' => [

    'source' => $data,

    'autoFill'=>true,

    'minLength'=>'1',

     ],

     ]);

     ?>


    <?= Html::activeHiddenInput($model, 'name')?>



use this field as a hidden field.

This code is working…

Hope its helpful to you…

No Need to use where () in above query…

With Regards,

Janvi

thx this work !!! … mmm i have other question… How do I can make the box be equal to the rest of the form ? , is very small and square .

Hola Erick,creo que te falto agregar ‘class’ => ‘form-control’, saludos.

Algo así:





 <?= $form->field($model, 'municipio')->widget(\yii\jui\AutoComplete::classname(), [

    		'options' => ['placeholder' => 'Select a color ...', 'class' => 'form-control'],

    'clientOptions' => [

        'source' => ['USA', 'RUS'],

    ],

]) ?>



I am using this autocomplete - this works fine, but the field shows blank on update.

I have done exactly as per above post.

Am I missing anything?

I modified this a little bit and now its working fine and accessing values from the DB

<?php

    &#036;data = Designation::find()


    -&gt;select(['name'])


    -&gt;asArray()


    -&gt;all();	

?>

<?= $form->field($modelFacultySignup, ‘autofill’)->widget(AutoComplete::classname(), [

    'options' =&gt; ['placeholder' =&gt; 'Select a color ...', 'class' =&gt; 'form-control'],


    'clientOptions' =&gt; [


    'source' =&gt; ArrayHelper::getColumn(&#036;data, 'name'),							],

]) ?>