简体   繁体   English

Yii2:Jui Auto Complete Widget怎么样?

[英]Yii2: Jui Auto Complete Widget how to?

I am trying to use yii2 Jui autocomplete widget. 我正在尝试使用yii2 Jui自动完成小部件。

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. 谢谢。

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. 谢谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM