我正在开发一个自动完成小部件.到目前为止,这是我的代码.
我的看法:
    <?php
    $data = Company::find()
    ->select(['name as value', 'name as  label','c_id as id'])
    ->asArray()
    ->all();
    echo AutoComplete::widget([
    'name' => 'Company',
    'id' => 'ddd',
    'clientOptions' => [
    'source' => $data,
    'autoFill'=>true,
    'minLength'=>'4',
    'select' => new JsExpression("function( event, ui ) {
            console.log(ui);
            $('#user-company').val(ui.item.id);
     }")],
     ]);
     ?>
    <?= Html::activeHiddenInput($model, 'company')?>
选择选项时,自动填充选项将在我的文本字段中更新,但不会更新隐藏字段.
如何在选择选项时更新隐藏字段?
您可以使用以下属性:
labels - 显示在下拉列表中,value - 选择后跳转到输入字段,id - 用于hiden字段使用的附加参数.例如:
<input type="hidden" id="user_company" name="user_company" value="qwe">
<?php
  use yii\web\JsExpression;
  echo AutoComplete::widget([
    'name' => 'company',
    'id' => 'ddd',
    'clientOptions' => [
      'source' => [
          ['label'=>'color1', 'value'=>'key1', 'id'=>'c_id1'],
          ['label'=>'color2', 'value'=>'key2', 'id'=>'c_id2']
      ],
      'autoFill'=>true,
      'minLength'=>'0',
      'select' => new JsExpression("function( event, ui ) {
        console.log(ui);
        $('#user_company').val(ui.item.id);
      }")
    ],
  ]);?>