L
L
Lander2015-10-19 13:22:06
Yii
Lander, 2015-10-19 13:22:06

Why is the field filled with value and not label when using the Autocomplete plugin?

Good afternoon. I use yii/jui/Autocomplete.

<?= AutoComplete::widget([
  'name' => 'client',
  'id' => 'client-find',
  'options' => [
    'class' => 'form-control',
  ],
  'clientOptions' => [
    'source' => MemberUser::find()
          ->select(['concat( lastname, " ", firstname ) as label', 'id as value'])
          ->asArray()
          ->all(),
  'autoFill' => true,
  'minLength' => 2,
  'select' => new yii\web\JsExpression("function( event, ui ) {
    console.log(ui.item);
    document.getElementById('eventsuser-client').value = ui.item.value;
    document.getElementById('client-find').value = ui.item.label;
  }")
  ]
]); ?>

When I select an autocomplete element, it is not the value from label that is inserted into the text field, but the value from value. How to fix? Doc read - everything is kosher there, but it does not work for me.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
Lander, 2015-10-19
@usdglander

Everything. Understood. Corrected version:

<?= AutoComplete::widget([
  'name' => 'client',
  'id' => 'client-find',
  'options' => [
    'class' => 'form-control',
  ],
  'clientOptions' => [
    'source' => MemberUser::find()
            ->select(['concat( lastname, " ", firstname ) as label', 'concat( lastname, " ", firstname ) as value', 'id as id'])
            ->asArray()
            ->all(),
    'autoFill' => false,
    'minLength' => 2,
    'select' => new yii\web\JsExpression("function( event, ui ) {
      console.log(ui.item);
      document.getElementById('eventsuser-client').value = ui.item.id;
      document.getElementById('client-find').value = ui.item.label;
     }")
  ]
]); ?>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question