Jeni Vasileva
Jeni Vasileva

Reputation: 788

dropDownList Autocomplete in yii2

In Yii2 I want one of my multiselect field to be autocomplete when user starts to type. I have load all products, but they are more than 1000 and loading is very very slow. So I need a dropdown list that will allow me to type some title of product before it suggests me an options list.

 <?php
     $cats = Product::find()->where('active = 1')->all();
     $prArr = array();
     if ($cats) {
       foreach ($cats as $cat) {
          $prArr[$cat->id] = $cat->title;
       }
     }
     $selectedProducts = '';
     if (isset($_POST['RelProducts']) and ! empty($_POST['RelProducts'])) {
         $selectedProducts = array();
         foreach ($_POST['RelProducts'] as $cat) {
               $selectedProducts[$cat] = $cat;
         }
     }
     ?>
     <?= Html::dropDownList('RelProducts[]', $selectedProducts, $prArr, ['multiple' => 'multiple', 'style' => 'width:300px;', 'rows' => 10, 'id' => 'relProductSelect']); ?>

here is the script:

$('#relProductSelect').multiSelect({
    selectableHeader: "<a href='#' id='select-all-rel-prod'>Избери всички</a><br /><input type='text' class='search-input' autocomplete='off' placeholder='търси...' style=\"width:100%;margin-bottom:5px;\">",
    selectionHeader: "<a href='#' id='deselect-all-rel-prod'>Премахни всички</a><br /><input type='text' class='search-input' autocomplete='off' placeholder='търси...' style=\"width:100%;margin-bottom:5px;\">",
    afterInit: function (ms) {
        var that = this,
                $selectableSearch = that.$selectableUl.prev(),
                $selectionSearch = that.$selectionUl.prev(),
                selectableSearchString = '#' + that.$container.attr('id') + ' .ms-elem-selectable:not(.ms-selected)',
                selectionSearchString = '#' + that.$container.attr('id') + ' .ms-elem-selection.ms-selected';

        that.qs1 = $selectableSearch.quicksearch(selectableSearchString)
                .on('keydown', function (e) {
                    if (e.which === 40) {
                        that.$selectableUl.focus();
                        return false;
                    }
                });

        that.qs2 = $selectionSearch.quicksearch(selectionSearchString)
                .on('keydown', function (e) {
                    if (e.which == 40) {
                        that.$selectionUl.focus();
                        return false;
                    }
                });
    },
    afterSelect: function () {
        this.qs1.cache();
        this.qs2.cache();
    },
    afterDeselect: function () {
        this.qs1.cache();
        this.qs2.cache();
    }
});

how to hurry the request?

Upvotes: 2

Views: 2410

Answers (1)

Muhammad Omer Aslam
Muhammad Omer Aslam

Reputation: 23738

Well one of the solutions that would require minimum effort could be to use the option

minimumInputLength: Minimum number of characters required to start a search.

it is more efficient to start filtering the results only when the search term is a certain length. In your case, it can create a difference.

As per your comment, you are using the KartikSelect2-multiselect but you haven't added the source code for it instead the js code is for the jquery muti-select but I will provide you the code for server-side implementation

Select2::widget([
    'name' => 'my-dropdown',
    'data' => $data,
    'options' => ['placeholder' => 'Select a product ...', 'multiple' => true],
    'pluginOptions' => [
        'allowClear' => true,
        'minimumInputLength':3
    ],
])

You can adjust the value for the input depending on your requirements. Hope it helps you out.

Upvotes: 2

Related Questions