A
A
Anton2016-07-29 15:27:36
Yii
Anton, 2016-07-29 15:27:36

How to limit the number of output items in yii2 autocomplete?

There is an autocomplete widget on the page

<?= $form->field($model,'city')->label(false)->widget(\yii\jui\AutoComplete::className(),[
        'clientOptions' => [
            'source' => ArrayHelper::getColumn(\app\models\City::find()->select('city_name_ru')->all(),'city_name_ru'),
        ],
        'options' => [
            'class' => 'form-control',
            'placeholder' => 'Город',
            'value' => $city
        ]
    ])?>

How can I limit the number of results displayed?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Maxim Fedorov, 2016-07-29
Reytarovsky @Antonchik

what do you mean by

limit the number of output results

if you mean the number of characters after which the dropdown list will be displayed, use the minLength parameter, for example:
'clientOptions' => [
    'minLength' => 3
],

if it means the number of displayed entries in the drop-down list, then for this widget this can only be done at the CSS level (correspondingly, a scroll will appear), because widget does not provide such setting
.ui-autocomplete {
  max-height: 100px;
  verflow-y: auto;
  overflow-x: hidden;
}

S
Slava Kryvel, 2016-07-29
@kryvel

If the method proposed by Maxim Fedorov does not suit you, then you can also try this stackoverflow.com/a/7617637/4828205
'clientOptions' => [
'source' => new \yii\web\JsExpression("function(request, response) {
var results = $.ui.autocomplete.filter(myarray, request.term);
response(results.slice(0, 10));
}"
]
where you really have to pervert with a request to the database from the js function. but this can be implemented with using ajax, the widget does the same

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question