erick9024
(New Angel 24)
1
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. )
evgeniyrru
(Evgeniyrru)
2
$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.
erick9024
(New Angel 24)
3
thx for reply … buddy can u make example for me pls
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
erick9024
(New Angel 24)
5
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'],
],
]) ?>
pkjoshi
(Info)
7
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?
mohit196
(Mohit196thapar)
8
I modified this a little bit and now its working fine and accessing values from the DB
<?php
$data = Designation::find()
->select(['name'])
->asArray()
->all();
?>
<?= $form->field($modelFacultySignup, ‘autofill’)->widget(AutoComplete::classname(), [
'options' => ['placeholder' => 'Select a color ...', 'class' => 'form-control'],
'clientOptions' => [
'source' => ArrayHelper::getColumn($data, 'name'), ],
]) ?>