Yii2: Jui Auto Complete Widget how to?

筅森魡賤 提交于 2019-12-20 20:26:13

问题


I am trying to use yii2 Jui autocomplete widget.

I have this code which is showing the auto-complete date correctly, but I am not able to save the data.

$data=ArrayHelper::map(State::find()->all(), 'id', 'state_name' );
$data=array_merge($data);

And then

echo 'State' .'<br>';
  echo AutoComplete::widget([
    'model'=>$model,
    'attribute' => 'state_id',     
    'clientOptions' => [
        'source' => $data,        
    ],
]);

Any solution will be greatly appreciated. Thanks.


回答1:


Ok I found the solution, it goes like this:

use yii\jui\AutoComplete;
use yii\web\JsExpression;

Then:

$data = State::find()
    ->select(['state_name as value', 'state_name as  label','id as id'])
    ->asArray()
    ->all();

Then

echo 'State' .'<br>';
  echo AutoComplete::widget([
    'name' => 'State',    
    'id' => 'ddd',
    'clientOptions' => [
        'source' => $data, 
        'autoFill'=>true,
         'select' => new JsExpression("function( event, ui ) {
        $('#city-state_name').val(ui.item.id);//#City-state_name is the id of hiddenInput.
     }")],
     ]);

and Finally:

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

That is all. Hope some one will find it useful. Thanks.



来源:https://stackoverflow.com/questions/28385855/yii2-jui-auto-complete-widget-how-to

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!