Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Full json in searchfield after selecting item. #1616

Open
duikb00t opened this issue Feb 22, 2017 · 1 comment
Open

Full json in searchfield after selecting item. #1616

duikb00t opened this issue Feb 22, 2017 · 1 comment

Comments

@duikb00t
Copy link

duikb00t commented Feb 22, 2017

First of all:
The dropdown and AJAX call is all working fine. Thanks Typeahead.js.

Issue
When I select an item from my dropdown, the full JSON object is being parsed in the textfield/searchfield.

My data is returned from PHP like: return response()->json($clients);

What am I doing wrong? Do I have to attach somewhere a json.parse?


function generateAjaxAutoCompleteTypeAhead() {
             var client_id  = $('input[name="client_id"]').val();

             var engine = new Bloodhound({
                remote: {
                    url: '/lookupclient?typehead_lookup_client=%QUERY%',
                    wildcard: '%QUERY%'
                },
                
                queryTokenizer: Bloodhound.tokenizers.whitespace,
                datumTokenizer: Bloodhound.tokenizers.whitespace
            });

             $("input[name='typehead_lookup_client']").on('typeahead:selected', function (e, ui) {

            });
            $("input[name='typehead_lookup_client']").typeahead({
                hint: true,
                highlight: true,
                minLength: 3,

                },{
                source: engine.ttAdapter(),
                name: 'resultList',
                limit: 20,


                templates: {
                    empty: [
                        '<div class="list-group search-results-dropdown"><div class="list-group-item">Nothing found.</div></div>'
                    ],
                    header: [
                        '<div class="list-group search-results-dropdown">'
                    ],
                    suggestion: function (data) {
                        return '<li class="list-group-item">' + data.city + ' - ' + data.zip_code + ' - ' +data.name + ' - ' + data.street_no + '</li>'
                    } 
                    }
                }
            );
        }
        generateAjaxAutoCompleteTypeAhead()
@samolsen
Copy link

I think you may need to define the display function for your dataset:

$("input").typeahead({ /*hint:, highlight:, etc. */ },  {
  source: engine.ttAdapter(),
  display: function (obj) {
    // Return your desired string
  }
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants