Yii2:Jui Auto Complete Widget 怎么做?

2022-01-07 00:00:00 autocomplete jquery php yii2

我正在尝试使用 yii2 Jui 自动完成小部件.

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);

然后

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 yiijuiAutoComplete;
use yiiwebJsExpression;

那么:

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

然后

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.
     }")],
     ]);

最后:

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

仅此而已.希望有人会发现它有用.谢谢.

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

相关文章