Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

179
Views
Symfony 5 Typeahead search

I need help for my javascript, I'm trying to create a search form with autocompletion with typeahead, but for the moment I'm getting "undefined" when I write something in the input : Undefined

This is my code :

$(document).ready(function () {
            var term = $("#tags").val();
            var datas = new Bloodhound({
                datumTokenizer: Bloodhound.tokenizers.whitespace,
                queryTokenizer: Bloodhound.tokenizers.whitespace,
                remote: {
                    url: "{{ path('autocomplete') }}/%QUERY%",
                    wildcard: "%QUERY%",
                    filter: function (datas) {
                        return $.map(datas, function (data) {
                            return $.map(data, function (item) {
                                console.log(item);
                                return {
                                    item_id: item.id,
                                    item_name: item.name,
                                    item_image: item.image,
                                };
                            })
                        });
                    },
                },
            });
            datas.initialize();
            $("#tags").typeahead(
                { hint: true, },
                {
                    name: "datas",
                    source: datas.ttAdapter(),
                    templates: {
                        suggestion: function (item) {
                            return `<div class='datafetch'><ul><li>${item.name}</li></ul></div>`;
                        },
                    },
                }
            );
        });

And you can see my controller here : Controller

The console log when I write something in the input : console.log

Thanks for your help !! :)

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

As seen in the screen of your console.log neither the property item.id nor item.name is defined, only item.title and item.image is defined.

thats why you get the undefined error.

about 4 years ago · Juan Pablo Isaza Report

0

I changed my code to :

 $(document).ready(function () {
            var term = $("#tags").val();
            var datas = new Bloodhound({
                datumTokenizer: Bloodhound.tokenizers.whitespace,
                queryTokenizer: Bloodhound.tokenizers.whitespace,
                remote: {
                    url: "{{ path('autocomplete') }}/%QUERY%",
                    wildcard: "%QUERY%",
                    filter: function (datas) {
                        return $.map(datas, function (data) {
                            return $.map(data, function (item) {
                                console.log(item);
                                return item;
                            })
                        });
                    },
                },
            });
            datas.initialize();
            $("#tags").typeahead(
                { hint: true, },
                {
                    name: "datas",
                    source: datas.ttAdapter(),
                    templates: {
                        suggestion: function (item) {
                            return `<div class='datafetch'><ul><li>${item.title}</li></ul></div>`;
                        },
                    },
                }
            );
        });

And now it's working ! :D

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!