沃梦达 / 编程问答 / php问题 / 正文

Yii2:Jui Auto Complete Widget 怎么做?

Yii2: Jui Auto Complete Widget how to?(Yii2:Jui Auto Complete Widget 怎么做?)

本文介绍了Yii2:Jui Auto Complete Widget 怎么做?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 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.

这篇关于Yii2:Jui Auto Complete Widget 怎么做?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:Yii2:Jui Auto Complete Widget 怎么做?

基础教程推荐