Yii2:瑞自动完成的Widget如何? [英] Yii2: Jui Auto Complete Widget how to?

查看:132
本文介绍了Yii2:瑞自动完成的Widget如何?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用yii2瑞自动完成构件。

I am trying to use yii2 Jui autocomplete widget.

我有这样的code是正确显示自动完成日期,但我不能够保存数据。

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

任何解决方案将大大AP preciated。
谢谢你。

Any solution will be greatly appreciated. Thanks.

推荐答案

好吧,我找到了解决方案,它是这样的:

Ok I found the solution, it goes like this:

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

然后:

$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.

这篇关于Yii2:瑞自动完成的Widget如何?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆