All individual subscriptions are 50% off this week only!

jbowman99
3 years ago
233
5
Laravel

Select Box changed to a Input text search

Posted 3 years ago by jbowman99

I have a select box that is populated by an ajax call to a get method, I would like to change the select box to an Input and autocomplete textbox.

Ajax Call

$(document).ready(function () {
            $(".js-search-items").select2({
                placeholder: 'Find an Item',
                allowClear: true,
                ajax: {
                    type: 'POST',
                    url: '{{ action('\Modules\Admin\Http\Controllers\Restaurant\Menus\ItemsController@postGetItems') }}',
                    dataType: 'json',
                    delay: 250,
                    data: function (params) {
                        return {
                            q: params.term,
                            page: params.page
                        };
                    },
                    processResults: function (data, params) {

                        items = [];

                        $.each($.parseJSON(data), function () {
                            items.push({
                                id: this.ItemID,
                                text: this.ItemName
                            });
                        });

                        return {
                            results: items,
                            pagination: {
                                more: (params.page * 30) < items.length
                            }
                        };
                    },
                    cache: true
                },
                minimumInputLength: 3
            });
        });

current select box


<div class="form-group">
                            {!! Form::label('itemID', 'Name', array('class' => 'col-md-2 control-label')) !!}
                            <select name="itemID" class="noSelect2 js-search-items col-md-4"></select>
                        </div>

can i change the select2 to autocomplete and salvage the script function to grab my data?

I want to do something like

{!! Form::text('itemID', null, ['class' => 'col-md-4', 'placeholder' => 'Search for an Item']) !!}

instead of the select input

Please sign in or create an account to participate in this conversation.