Untitled

mail@pastecode.io avatar
unknown
plain_text
5 months ago
1.2 kB
1
Indexable
$(document).ready(function(){
    $('#category-input').on('keyup', function() {
        let query = $(this).val();

        if (query.length > 0) {
            $.ajax({
                url: "{{ route('autocomplete') }}",
                method: 'GET',
                data: { query: query },
                success: function(data) {
                    $('#suggestions').empty();
                    if (data.length > 0) {
                        data.forEach(function(category) {
                            $('#suggestions').append('<div class="autocomplete-suggestion" data-id="' + category.id + '">' + category.name + '</div>');
                        });
                    } else {
                        $('#suggestions').append('<div class="autocomplete-suggestion">No se encontraron resultados</div>');
                    }
                }
            });
        } else {
            $('#suggestions').empty();
        }
    });

    // Selección de una categoría desde las sugerencias
    $(document).on('click', '.autocomplete-suggestion', function() {
        let categoryName = $(this).text();
        $('#category-input').val(categoryName);
        $('#suggestions').empty();
    });
});
Leave a Comment