我正在尝试使用yii2 Jui自动完成小部件.
$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,],]);
任何解决方案将不胜感激.
谢谢.
好的,我找到了解决方案,它是这样的:
原文链接:https://www.f2er.com/php/137184.htmluse 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','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')?>
就这些.希望有人会觉得它很有用.谢谢.