2015-05-03 56 views
3

我使用的是yii2自動完成的小部件,它工作正常,除非當我更新表單時字段顯示空白。Yii2自動完成 - 在更新的字段中缺少值

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

$data = app\models\Doctor::find() 
    ->select(['doctor_name as value', 'doctor_name as label','id as id']) 
    ->asArray() 
    ->all(); 

,之後自動完成這樣的代碼

echo 'Doctor' .'<br>'; 
    echo AutoComplete::widget([ 
    'name' => 'Doctor',  
    'id' => 'ddd', 
    'clientOptions' => [ 
     'source' => $data, 
     'autoFill'=>true, 
     'select' => new JsExpression("function(event, ui) { 
     $('#appoinment-doctor_name').val(ui.item.id); 
    }")], 
    ]); 
<?= Html::activeHiddenInput($model, 'doctor_name')?> 

我是缺少在這裏?我如何獲得更新的價值?

回答

2

你必須明確地設置value

echo AutoComplete::widget([ 
    'name' => 'Doctor',  
    'id' => 'ddd', 
    'value' => $model->doctor_name, 
    ... 

如果設置modelattribute雖然這是沒有必要的。

+1

Topher - 謝謝它工作正常,但我必須修改它像 - ''value'=>(!$ model-> isNewRecord?$ model-> doctorName-> doctor_name:''),'。再次感謝。 – Joshi